conftest.py 2.6 KB

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