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 # 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 # 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 if key.startswith('BORG_')]
  26. for key in keys:
  27. monkeypatch.delenv(key, raising=False)
  28. def pytest_report_header(config, startdir):
  29. tests = {
  30. "BSD flags": has_lchflags,
  31. "fuse": has_llfuse,
  32. "root": not fakeroot_detected(),
  33. "symlinks": are_symlinks_supported(),
  34. "hardlinks": are_hardlinks_supported(),
  35. "atime/mtime": is_utime_fully_supported(),
  36. "modes": "BORG_TESTS_IGNORE_MODES" not in os.environ
  37. }
  38. enabled = []
  39. disabled = []
  40. for test in tests:
  41. if tests[test]:
  42. enabled.append(test)
  43. else:
  44. disabled.append(test)
  45. output = "Tests enabled: " + ", ".join(enabled) + "\n"
  46. output += "Tests disabled: " + ", ".join(disabled)
  47. return output
  48. class DefaultPatches:
  49. def __init__(self, request):
  50. self.org_cache_wipe_cache = borg.cache.LocalCache.wipe_cache
  51. def wipe_should_not_be_called(*a, **kw):
  52. raise AssertionError("Cache wipe was triggered, if this is part of the test add "
  53. "@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)