فهرست منبع

point XDG_*_HOME to temp dirs for tests, fixes #1714

otherwise it spoils the user's nonces and cache dirs with lots of files.

also: remove all BORG_* env vars from the outer environment

fix get_*_dir tests to use monkeypatch.
Thomas Waldmann 8 سال پیش
والد
کامیت
f3efcdbd2e
2فایلهای تغییر یافته به همراه22 افزوده شده و 22 حذف شده
  1. 8 22
      borg/testsuite/helpers.py
  2. 14 0
      conftest.py

+ 8 - 22
borg/testsuite/helpers.py

@@ -617,38 +617,24 @@ class TestParseTimestamp(BaseTestCase):
         self.assert_equal(parse_timestamp('2015-04-19T20:25:00'), datetime(2015, 4, 19, 20, 25, 0, 0, timezone.utc))
         self.assert_equal(parse_timestamp('2015-04-19T20:25:00'), datetime(2015, 4, 19, 20, 25, 0, 0, timezone.utc))
 
 
 
 
-def test_get_cache_dir():
+def test_get_cache_dir(monkeypatch):
     """test that get_cache_dir respects environment"""
     """test that get_cache_dir respects environment"""
-    # reset BORG_CACHE_DIR in order to test default
-    old_env = None
-    if os.environ.get('BORG_CACHE_DIR'):
-        old_env = os.environ['BORG_CACHE_DIR']
-        del(os.environ['BORG_CACHE_DIR'])
+    monkeypatch.delenv('XDG_CACHE_HOME', raising=False)
     assert get_cache_dir() == os.path.join(os.path.expanduser('~'), '.cache', 'borg')
     assert get_cache_dir() == os.path.join(os.path.expanduser('~'), '.cache', 'borg')
-    os.environ['XDG_CACHE_HOME'] = '/var/tmp/.cache'
+    monkeypatch.setenv('XDG_CACHE_HOME', '/var/tmp/.cache')
     assert get_cache_dir() == os.path.join('/var/tmp/.cache', 'borg')
     assert get_cache_dir() == os.path.join('/var/tmp/.cache', 'borg')
-    os.environ['BORG_CACHE_DIR'] = '/var/tmp'
+    monkeypatch.setenv('BORG_CACHE_DIR', '/var/tmp')
     assert get_cache_dir() == '/var/tmp'
     assert get_cache_dir() == '/var/tmp'
-    # reset old env
-    if old_env is not None:
-        os.environ['BORG_CACHE_DIR'] = old_env
 
 
 
 
-def test_get_keys_dir():
+def test_get_keys_dir(monkeypatch):
     """test that get_keys_dir respects environment"""
     """test that get_keys_dir respects environment"""
-    # reset BORG_KEYS_DIR in order to test default
-    old_env = None
-    if os.environ.get('BORG_KEYS_DIR'):
-        old_env = os.environ['BORG_KEYS_DIR']
-        del(os.environ['BORG_KEYS_DIR'])
+    monkeypatch.delenv('XDG_CONFIG_HOME', raising=False)
     assert get_keys_dir() == os.path.join(os.path.expanduser('~'), '.config', 'borg', 'keys')
     assert get_keys_dir() == os.path.join(os.path.expanduser('~'), '.config', 'borg', 'keys')
-    os.environ['XDG_CONFIG_HOME'] = '/var/tmp/.config'
+    monkeypatch.setenv('XDG_CONFIG_HOME', '/var/tmp/.config')
     assert get_keys_dir() == os.path.join('/var/tmp/.config', 'borg', 'keys')
     assert get_keys_dir() == os.path.join('/var/tmp/.config', 'borg', 'keys')
-    os.environ['BORG_KEYS_DIR'] = '/var/tmp'
+    monkeypatch.setenv('BORG_KEYS_DIR', '/var/tmp')
     assert get_keys_dir() == '/var/tmp'
     assert get_keys_dir() == '/var/tmp'
-    # reset old env
-    if old_env is not None:
-        os.environ['BORG_KEYS_DIR'] = old_env
 
 
 
 
 @pytest.fixture()
 @pytest.fixture()

+ 14 - 0
conftest.py

@@ -0,0 +1,14 @@
+import os
+
+import pytest
+
+
+@pytest.fixture(autouse=True)
+def clean_env(tmpdir_factory, monkeypatch):
+    # avoid that we access / modify the user's normal .config / .cache directory:
+    monkeypatch.setenv('XDG_CONFIG_HOME', tmpdir_factory.mktemp('xdg-config-home'))
+    monkeypatch.setenv('XDG_CACHE_HOME', tmpdir_factory.mktemp('xdg-cache-home'))
+    # also avoid to use anything from the outside environment:
+    keys = [key for key in os.environ if key.startswith('BORG_')]
+    for key in keys:
+        monkeypatch.delenv(key, raising=False)