conftest.py 1.2 KB

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