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
  13. from borg.logger import setup_logging
  14. # Ensure that the loggers exist for all tests
  15. setup_logging()
  16. from borg.testsuite import has_lchflags, has_llfuse, has_pyfuse3
  17. from borg.testsuite import are_symlinks_supported, are_hardlinks_supported, is_utime_fully_supported
  18. from borg.testsuite.platform import fakeroot_detected, are_acls_working
  19. from borg import xattr
  20. @pytest.fixture(autouse=True)
  21. def clean_env(tmpdir_factory, monkeypatch):
  22. # avoid that we access / modify the user's normal .config / .cache directory:
  23. monkeypatch.setenv('XDG_CONFIG_HOME', str(tmpdir_factory.mktemp('xdg-config-home')))
  24. monkeypatch.setenv('XDG_CACHE_HOME', str(tmpdir_factory.mktemp('xdg-cache-home')))
  25. # also avoid to use anything from the outside environment:
  26. keys = [key for key in os.environ
  27. if key.startswith('BORG_') and key not in ('BORG_FUSE_IMPL', )]
  28. for key in keys:
  29. monkeypatch.delenv(key, raising=False)
  30. def pytest_report_header(config, startdir):
  31. tests = {
  32. "BSD flags": has_lchflags,
  33. "fuse2": has_llfuse,
  34. "fuse3": has_pyfuse3,
  35. "root": not fakeroot_detected(),
  36. "symlinks": are_symlinks_supported(),
  37. "hardlinks": are_hardlinks_supported(),
  38. "atime/mtime": is_utime_fully_supported(),
  39. "modes": "BORG_TESTS_IGNORE_MODES" not in os.environ
  40. }
  41. enabled = []
  42. disabled = []
  43. for test in tests:
  44. if tests[test]:
  45. enabled.append(test)
  46. else:
  47. disabled.append(test)
  48. output = "Tests enabled: " + ", ".join(enabled) + "\n"
  49. output += "Tests disabled: " + ", ".join(disabled)
  50. return output
  51. class DefaultPatches:
  52. def __init__(self, request):
  53. self.org_cache_wipe_cache = borg.cache.LocalCache.wipe_cache
  54. def wipe_should_not_be_called(*a, **kw):
  55. raise AssertionError("Cache wipe was triggered, if this is part of the test add @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)