conftest.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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
  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', tmpdir_factory.mktemp('xdg-config-home'))
  24. monkeypatch.setenv('XDG_CACHE_HOME', tmpdir_factory.mktemp('xdg-cache-home'))
  25. # also avoid to use anything from the outside environment:
  26. keys = [key for key in os.environ if key.startswith('BORG_')]
  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. "fuse": has_llfuse,
  33. "root": not fakeroot_detected(),
  34. "symlinks": are_symlinks_supported(),
  35. "hardlinks": are_hardlinks_supported(),
  36. "atime/mtime": is_utime_fully_supported(),
  37. "modes": "BORG_TESTS_IGNORE_MODES" not in os.environ
  38. }
  39. enabled = []
  40. disabled = []
  41. for test in tests:
  42. if tests[test]:
  43. enabled.append(test)
  44. else:
  45. disabled.append(test)
  46. output = "Tests enabled: " + ", ".join(enabled) + "\n"
  47. output += "Tests disabled: " + ", ".join(disabled)
  48. return output
  49. class DefaultPatches:
  50. def __init__(self, request):
  51. self.org_cache_wipe_cache = borg.cache.LocalCache.wipe_cache
  52. def wipe_should_not_be_called(*a, **kw):
  53. raise AssertionError("Cache wipe was triggered, if this is part of the test add @pytest.mark.allow_cache_wipe")
  54. if 'allow_cache_wipe' not in request.keywords:
  55. borg.cache.LocalCache.wipe_cache = wipe_should_not_be_called
  56. request.addfinalizer(self.undo)
  57. def undo(self):
  58. borg.cache.LocalCache.wipe_cache = self.org_cache_wipe_cache
  59. @pytest.fixture(autouse=True)
  60. def default_patches(request):
  61. return DefaultPatches(request)