conftest.py 1.4 KB

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