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