Răsfoiți Sursa

use monkeypatch to set env vars

but only on pytest based tests.
Thomas Waldmann 8 ani în urmă
părinte
comite
2b27a06595
3 a modificat fișierele cu 23 adăugiri și 24 ștergeri
  1. 7 7
      borg/testsuite/benchmark.py
  2. 12 13
      borg/testsuite/helpers.py
  3. 4 4
      borg/testsuite/upgrader.py

+ 7 - 7
borg/testsuite/benchmark.py

@@ -14,13 +14,13 @@ from .archiver import changedir, cmd
 
 
 
 
 @pytest.yield_fixture
 @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'))
     yield str(tmpdir.join('repository'))
     tmpdir.remove(rec=1)
     tmpdir.remove(rec=1)
 
 

+ 12 - 13
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, \
     ProgressIndicatorPercent, ProgressIndicatorEndless, load_excludes, parse_pattern, \
     PatternMatcher, RegexPattern, PathPrefixPattern, FnmatchPattern, ShellPattern, \
     PatternMatcher, RegexPattern, PathPrefixPattern, FnmatchPattern, ShellPattern, \
     Buffer
     Buffer
-from . import BaseTestCase, environment_variable, FakeInputs
+from . import BaseTestCase, FakeInputs
 
 
 
 
 class BigIntTestCase(BaseTestCase):
 class BigIntTestCase(BaseTestCase):
@@ -653,8 +653,8 @@ def test_stats_basic(stats):
     assert stats.usize == 10
     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()
     out = StringIO()
     stats.show_progress(stream=out)
     stats.show_progress(stream=out)
     s = '20 B O 10 B C 10 B D 0 N '
     s = '20 B O 10 B C 10 B D 0 N '
@@ -811,21 +811,20 @@ def test_yes_input_custom():
     assert not yes(falsish=('NOPE', ), input=input)
     assert not yes(falsish=('NOPE', ), input=input)
 
 
 
 
-def test_yes_env():
+def test_yes_env(monkeypatch):
     for value in TRUISH:
     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:
     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:
     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():
 def test_yes_defaults():

+ 4 - 4
borg/testsuite/upgrader.py

@@ -97,7 +97,7 @@ class MockArgs:
 
 
 
 
 @pytest.fixture()
 @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
     create an attic key file from the given repo, in the keys
     subdirectory of the given tmpdir
     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
     # we use the repo dir for the created keyfile, because we do
     # not want to clutter existing keyfiles
     # 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
     # we use the same directory for the converted files, which
     # will clutter the previously created one, which we don't care
     # will clutter the previously created one, which we don't care
     # about anyways. in real runs, the original key will be retained.
     # 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,
     return attic.key.KeyfileKey.create(attic_repo,
                                        MockArgs(keys_dir))
                                        MockArgs(keys_dir))