conftest.py 2.6 KB

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