2
0
Эх сурвалжийг харах

Merge pull request #1719 from ThomasWaldmann/clean_env_10

Clean env in 1.0
enkore 8 жил өмнө
parent
commit
12a127ace1

+ 7 - 7
borg/testsuite/benchmark.py

@@ -14,13 +14,13 @@ from .archiver import changedir, cmd
 
 
 @pytest.yield_fixture
-def repo_url(request, tmpdir):
-    os.environ['BORG_PASSPHRASE'] = '123456'
-    os.environ['BORG_CHECK_I_KNOW_WHAT_I_AM_DOING'] = 'YES'
-    os.environ['BORG_DELETE_I_KNOW_WHAT_I_AM_DOING'] = 'YES'
-    os.environ['BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK'] = 'yes'
-    os.environ['BORG_KEYS_DIR'] = str(tmpdir.join('keys'))
-    os.environ['BORG_CACHE_DIR'] = str(tmpdir.join('cache'))
+def repo_url(request, tmpdir, monkeypatch):
+    monkeypatch.setenv('BORG_PASSPHRASE', '123456')
+    monkeypatch.setenv('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING', 'YES')
+    monkeypatch.setenv('BORG_DELETE_I_KNOW_WHAT_I_AM_DOING', 'YES')
+    monkeypatch.setenv('BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK', 'yes')
+    monkeypatch.setenv('BORG_KEYS_DIR', str(tmpdir.join('keys')))
+    monkeypatch.setenv('BORG_CACHE_DIR', str(tmpdir.join('cache')))
     yield str(tmpdir.join('repository'))
     tmpdir.remove(rec=1)
 

+ 20 - 35
borg/testsuite/helpers.py

@@ -17,7 +17,7 @@ from ..helpers import Location, format_file_size, format_timedelta, format_line,
     ProgressIndicatorPercent, ProgressIndicatorEndless, load_excludes, parse_pattern, \
     PatternMatcher, RegexPattern, PathPrefixPattern, FnmatchPattern, ShellPattern, \
     Buffer
-from . import BaseTestCase, environment_variable, FakeInputs
+from . import BaseTestCase, FakeInputs
 
 
 class BigIntTestCase(BaseTestCase):
@@ -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))
 
 
-def test_get_cache_dir():
+def test_get_cache_dir(monkeypatch):
     """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')
-    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')
-    os.environ['BORG_CACHE_DIR'] = '/var/tmp'
+    monkeypatch.setenv('BORG_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"""
-    # 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')
-    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')
-    os.environ['BORG_KEYS_DIR'] = '/var/tmp'
+    monkeypatch.setenv('BORG_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()
@@ -667,8 +653,8 @@ def test_stats_basic(stats):
     assert stats.usize == 10
 
 
-def tests_stats_progress(stats, columns=80):
-    os.environ['COLUMNS'] = str(columns)
+def tests_stats_progress(stats, monkeypatch, columns=80):
+    monkeypatch.setenv('COLUMNS', str(columns))
     out = StringIO()
     stats.show_progress(stream=out)
     s = '20 B O 10 B C 10 B D 0 N '
@@ -825,21 +811,20 @@ def test_yes_input_custom():
     assert not yes(falsish=('NOPE', ), input=input)
 
 
-def test_yes_env():
+def test_yes_env(monkeypatch):
     for value in TRUISH:
-        with environment_variable(OVERRIDE_THIS=value):
-            assert yes(env_var_override='OVERRIDE_THIS')
+        monkeypatch.setenv('OVERRIDE_THIS', value)
+        assert yes(env_var_override='OVERRIDE_THIS')
     for value in FALSISH:
-        with environment_variable(OVERRIDE_THIS=value):
-            assert not yes(env_var_override='OVERRIDE_THIS')
+        monkeypatch.setenv('OVERRIDE_THIS', value)
+        assert not yes(env_var_override='OVERRIDE_THIS')
 
 
-def test_yes_env_default():
+def test_yes_env_default(monkeypatch):
     for value in DEFAULTISH:
-        with environment_variable(OVERRIDE_THIS=value):
-            assert yes(env_var_override='OVERRIDE_THIS', default=True)
-        with environment_variable(OVERRIDE_THIS=value):
-            assert not yes(env_var_override='OVERRIDE_THIS', default=False)
+        monkeypatch.setenv('OVERRIDE_THIS', value)
+        assert yes(env_var_override='OVERRIDE_THIS', default=True)
+        assert not yes(env_var_override='OVERRIDE_THIS', default=False)
 
 
 def test_yes_defaults():

+ 4 - 4
borg/testsuite/upgrader.py

@@ -97,7 +97,7 @@ class MockArgs:
 
 
 @pytest.fixture()
-def attic_key_file(attic_repo, tmpdir):
+def attic_key_file(attic_repo, tmpdir, monkeypatch):
     """
     create an attic key file from the given repo, in the keys
     subdirectory of the given tmpdir
@@ -112,13 +112,13 @@ def attic_key_file(attic_repo, tmpdir):
 
     # we use the repo dir for the created keyfile, because we do
     # not want to clutter existing keyfiles
-    os.environ['ATTIC_KEYS_DIR'] = keys_dir
+    monkeypatch.setenv('ATTIC_KEYS_DIR', keys_dir)
 
     # we use the same directory for the converted files, which
     # will clutter the previously created one, which we don't care
     # about anyways. in real runs, the original key will be retained.
-    os.environ['BORG_KEYS_DIR'] = keys_dir
-    os.environ['ATTIC_PASSPHRASE'] = 'test'
+    monkeypatch.setenv('BORG_KEYS_DIR', keys_dir)
+    monkeypatch.setenv('ATTIC_PASSPHRASE', 'test')
     return attic.key.KeyfileKey.create(attic_repo,
                                        MockArgs(keys_dir))
 

+ 35 - 0
conftest.py

@@ -0,0 +1,35 @@
+import os
+import sys
+
+import pytest
+
+
+# This is a hack to fix path problems because "borg" (the package) is in the source root.
+# When importing the conftest an "import borg" can incorrectly import the borg from the
+# source root instead of the one installed in the environment.
+# The workaround is to remove entries pointing there from the path and check whether "borg"
+# is still importable. If it is not, then it has not been installed in the environment
+# and the entries are put back.
+#
+# TODO: After moving the package to src/: remove this.
+
+original_path = list(sys.path)
+for entry in original_path:
+    if entry == '' or entry.endswith('/borg'):
+        sys.path.remove(entry)
+
+try:
+    import borg
+except ImportError:
+    sys.path = original_path
+
+
+@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)