conftest.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os
  2. import pytest
  3. # IMPORTANT keep this above all other borg imports to avoid inconsistent values
  4. # for `from borg.constants import PBKDF2_ITERATIONS` (or star import) usages before
  5. # this is executed
  6. from borg import constants
  7. # no fixture-based monkey-patching since star-imports are used for the constants module
  8. constants.PBKDF2_ITERATIONS = 1
  9. # needed to get pretty assertion failures in unit tests:
  10. if hasattr(pytest, 'register_assert_rewrite'):
  11. pytest.register_assert_rewrite('borg.testsuite')
  12. import borg.cache # noqa: E402
  13. from borg.logger import setup_logging # noqa: E402
  14. # Ensure that the loggers exist for all tests
  15. setup_logging()
  16. from borg.testsuite import has_lchflags, has_llfuse, has_pyfuse3 # noqa: E402
  17. from borg.testsuite import are_symlinks_supported, are_hardlinks_supported, is_utime_fully_supported # noqa: E402
  18. from borg.testsuite.platform import fakeroot_detected # noqa: E402
  19. @pytest.fixture(autouse=True)
  20. def clean_env(tmpdir_factory, monkeypatch):
  21. # avoid that we access / modify the user's normal .config / .cache directory:
  22. monkeypatch.setenv('XDG_CONFIG_HOME', str(tmpdir_factory.mktemp('xdg-config-home')))
  23. monkeypatch.setenv('XDG_CACHE_HOME', str(tmpdir_factory.mktemp('xdg-cache-home')))
  24. # also avoid to use anything from the outside environment:
  25. keys = [key for key in os.environ
  26. if key.startswith('BORG_') and key not in ('BORG_FUSE_IMPL', )]
  27. for key in keys:
  28. monkeypatch.delenv(key, raising=False)
  29. def pytest_report_header(config, startdir):
  30. tests = {
  31. "BSD flags": has_lchflags,
  32. "fuse2": has_llfuse,
  33. "fuse3": has_pyfuse3,
  34. "root": not fakeroot_detected(),
  35. "symlinks": are_symlinks_supported(),
  36. "hardlinks": are_hardlinks_supported(),
  37. "atime/mtime": is_utime_fully_supported(),
  38. "modes": "BORG_TESTS_IGNORE_MODES" not in os.environ
  39. }
  40. enabled = []
  41. disabled = []
  42. for test in tests:
  43. if tests[test]:
  44. enabled.append(test)
  45. else:
  46. disabled.append(test)
  47. output = "Tests enabled: " + ", ".join(enabled) + "\n"
  48. output += "Tests disabled: " + ", ".join(disabled)
  49. return output
  50. class DefaultPatches:
  51. def __init__(self, request):
  52. self.org_cache_wipe_cache = borg.cache.LocalCache.wipe_cache
  53. def wipe_should_not_be_called(*a, **kw):
  54. raise AssertionError("Cache wipe was triggered, if this is part of the test add "
  55. "@pytest.mark.allow_cache_wipe")
  56. if 'allow_cache_wipe' not in request.keywords:
  57. borg.cache.LocalCache.wipe_cache = wipe_should_not_be_called
  58. request.addfinalizer(self.undo)
  59. def undo(self):
  60. borg.cache.LocalCache.wipe_cache = self.org_cache_wipe_cache
  61. @pytest.fixture(autouse=True)
  62. def default_patches(request):
  63. return DefaultPatches(request)