2
0

conftest.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. import os.path
  3. import sys
  4. import pytest
  5. # needed to get pretty assertion failures in unit tests:
  6. pytest.register_assert_rewrite('borg.testsuite')
  7. # This is a hack to fix path problems because "borg" (the package) is in the source root.
  8. # When importing the conftest an "import borg" can incorrectly import the borg from the
  9. # source root instead of the one installed in the environment.
  10. # The workaround is to remove entries pointing there from the path and check whether "borg"
  11. # is still importable. If it is not, then it has not been installed in the environment
  12. # and the entries are put back.
  13. original_path = list(sys.path)
  14. for entry in original_path:
  15. if entry == '' or entry == os.path.dirname(__file__):
  16. sys.path.remove(entry)
  17. try:
  18. import borg
  19. except ImportError:
  20. sys.path = original_path
  21. @pytest.fixture(autouse=True)
  22. def clean_env(tmpdir_factory, monkeypatch):
  23. # avoid that we access / modify the user's normal .config / .cache directory:
  24. monkeypatch.setenv('XDG_CONFIG_HOME', tmpdir_factory.mktemp('xdg-config-home'))
  25. monkeypatch.setenv('XDG_CACHE_HOME', tmpdir_factory.mktemp('xdg-cache-home'))
  26. # also avoid to use anything from the outside environment:
  27. keys = [key for key in os.environ if key.startswith('BORG_')]
  28. for key in keys:
  29. monkeypatch.delenv(key, raising=False)