conftest.py 1.2 KB

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