From f3efcdbd2eee1fe9b632630c12773a93c11df2b4 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 14 Oct 2016 00:46:43 +0200 Subject: [PATCH 1/3] 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. --- borg/testsuite/helpers.py | 30 ++++++++---------------------- conftest.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 conftest.py diff --git a/borg/testsuite/helpers.py b/borg/testsuite/helpers.py index 8c9c20e52..f7b53ad7c 100644 --- a/borg/testsuite/helpers.py +++ b/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)) -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() diff --git a/conftest.py b/conftest.py new file mode 100644 index 000000000..9d5513b5f --- /dev/null +++ b/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) From 2679963cb9dfec51f377267e429d13d2a01340f3 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 14 Oct 2016 04:42:13 +0200 Subject: [PATCH 2/3] add conftest.py hack needed for borg 1.0.x --- conftest.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/conftest.py b/conftest.py index 9d5513b5f..d80aed4c1 100644 --- a/conftest.py +++ b/conftest.py @@ -1,8 +1,29 @@ 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: From 2b27a06595fad7bc416c2e0e839f0713d623c3ee Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 14 Oct 2016 04:44:06 +0200 Subject: [PATCH 3/3] use monkeypatch to set env vars but only on pytest based tests. --- borg/testsuite/benchmark.py | 14 +++++++------- borg/testsuite/helpers.py | 25 ++++++++++++------------- borg/testsuite/upgrader.py | 8 ++++---- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/borg/testsuite/benchmark.py b/borg/testsuite/benchmark.py index 9751bc1a4..b3400c2f9 100644 --- a/borg/testsuite/benchmark.py +++ b/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) diff --git a/borg/testsuite/helpers.py b/borg/testsuite/helpers.py index f7b53ad7c..10574f5a1 100644 --- a/borg/testsuite/helpers.py +++ b/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): @@ -653,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 ' @@ -811,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(): diff --git a/borg/testsuite/upgrader.py b/borg/testsuite/upgrader.py index 013a8d002..fb5b5e6f4 100644 --- a/borg/testsuite/upgrader.py +++ b/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))