From 8efe2c8fada843ac8a6c064aa0dd14373143a168 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Tue, 17 Nov 2020 14:57:34 -0600 Subject: [PATCH] Improve tests to use XDG* variables Instead of overriding YADM_DIR via -Y, set the XDG* environment variables. --- test/conftest.py | 46 +++++++++++++++++--------- test/test_alt.py | 26 ++++++++------- test/test_unit_configure_paths.py | 19 ++++++++--- test/test_unit_set_local_alt_values.py | 2 +- test/test_unit_set_yadm_dir.py | 29 ++++++++++++---- 5 files changed, 83 insertions(+), 39 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 5703a5d..5e18ac5 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -180,6 +180,10 @@ class Runner(): self.command = ' '.join([str(cmd) for cmd in command]) else: self.command = command + if env is None: + env = {} + merged_env = os.environ.copy() + merged_env.update(env) self.inp = inp self.wrap(expect) process = Popen( @@ -189,7 +193,7 @@ class Runner(): stderr=PIPE, shell=shell, cwd=cwd, - env=env, + env=merged_env, ) input_bytes = self.inp if self.inp: @@ -280,13 +284,17 @@ def yadm(): @pytest.fixture() def paths(tmpdir, yadm): """Function scoped test paths""" + dir_root = tmpdir.mkdir('root') - dir_work = dir_root.mkdir('work') - dir_yadm = dir_root.mkdir('yadm') - dir_repo = dir_yadm.mkdir('repo.git') - dir_hooks = dir_yadm.mkdir('hooks') dir_remote = dir_root.mkdir('remote') - file_archive = dir_yadm.join('archive') + dir_work = dir_root.mkdir('work') + dir_xdg_data = dir_root.mkdir('xdg_data') + dir_xdg_home = dir_root.mkdir('xdg_home') + dir_data = dir_xdg_data.mkdir('yadm') + dir_yadm = dir_xdg_home.mkdir('yadm') + dir_hooks = dir_yadm.mkdir('hooks') + dir_repo = dir_data.mkdir('repo.git') + file_archive = dir_data.join('archive') file_bootstrap = dir_yadm.join('bootstrap') file_config = dir_yadm.join('config') file_encrypt = dir_yadm.join('encrypt') @@ -294,24 +302,32 @@ def paths(tmpdir, yadm): 'Paths', [ 'pgm', 'root', - 'work', - 'yadm', - 'repo', - 'hooks', 'remote', + 'work', + 'xdg_data', + 'xdg_home', + 'data', + 'yadm', + 'hooks', + 'repo', 'archive', 'bootstrap', 'config', 'encrypt', ]) + os.environ['XDG_CONFIG_HOME'] = str(dir_xdg_home) + os.environ['XDG_DATA_HOME'] = str(dir_xdg_data) return paths( yadm, dir_root, - dir_work, - dir_yadm, - dir_repo, - dir_hooks, dir_remote, + dir_work, + dir_xdg_data, + dir_xdg_home, + dir_data, + dir_yadm, + dir_hooks, + dir_repo, file_archive, file_bootstrap, file_config, @@ -324,7 +340,7 @@ def yadm_y(paths): """Generate custom command_list function""" def command_list(*args): """Produce params for running yadm with -Y""" - return [paths.pgm, '-Y', str(paths.yadm)] + list(args) + return [paths.pgm] + list(args) return command_list diff --git a/test/test_alt.py b/test/test_alt.py index 96c3129..eb01997 100644 --- a/test/test_alt.py +++ b/test/test_alt.py @@ -22,12 +22,12 @@ def test_alt_source( tracked, encrypt, exclude, yadm_alt): """Test yadm alt operates on all expected sources of alternates""" - yadm_dir = setup_standard_yadm_dir(paths) + yadm_dir, yadm_data = setup_standard_yadm_dir(paths) utils.create_alt_files( paths, '##default', tracked=tracked, encrypt=encrypt, exclude=exclude, yadm_alt=yadm_alt, yadm_dir=yadm_dir) - run = runner([paths.pgm, '-Y', yadm_dir, 'alt']) + run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt']) assert run.success assert run.err == '' linked = utils.parse_alt_output(run.out) @@ -57,12 +57,12 @@ def test_alt_source( @pytest.mark.parametrize('yadm_alt', [True, False], ids=['alt', 'worktree']) def test_relative_link(runner, paths, yadm_alt): """Confirm links created are relative""" - yadm_dir = setup_standard_yadm_dir(paths) + yadm_dir, yadm_data = setup_standard_yadm_dir(paths) utils.create_alt_files( paths, '##default', tracked=True, encrypt=False, exclude=False, yadm_alt=yadm_alt, yadm_dir=yadm_dir) - run = runner([paths.pgm, '-Y', yadm_dir, 'alt']) + run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt']) assert run.success assert run.err == '' @@ -91,7 +91,7 @@ def test_alt_conditions( runner, paths, tst_sys, tst_distro, tst_host, tst_user, suffix): """Test conditions supported by yadm alt""" - yadm_dir = setup_standard_yadm_dir(paths) + yadm_dir, yadm_data = setup_standard_yadm_dir(paths) # set the class tst_class = 'testclass' @@ -106,7 +106,7 @@ def test_alt_conditions( ) utils.create_alt_files(paths, suffix) - run = runner([paths.pgm, '-Y', yadm_dir, 'alt']) + run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt']) assert run.success assert run.err == '' linked = utils.parse_alt_output(run.out) @@ -131,13 +131,13 @@ def test_alt_conditions( def test_alt_templates( runner, paths, kind, label): """Test templates supported by yadm alt""" - yadm_dir = setup_standard_yadm_dir(paths) + yadm_dir, yadm_data = setup_standard_yadm_dir(paths) suffix = f'##{label}.{kind}' if kind is None: suffix = f'##{label}' utils.create_alt_files(paths, suffix) - run = runner([paths.pgm, '-Y', yadm_dir, 'alt']) + run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt']) assert run.success assert run.err == '' created = utils.parse_alt_output(run.out, linked=False) @@ -265,12 +265,13 @@ def test_template_overwrite_symlink(runner, yadm_y, paths, tst_sys): @pytest.mark.parametrize('style', ['symlink', 'template']) def test_ensure_alt_path(runner, paths, style): """Test that directories are created before making alternates""" - yadm_dir = setup_standard_yadm_dir(paths) + yadm_dir, yadm_data = setup_standard_yadm_dir(paths) suffix = 'default' if style == 'symlink' else 'template' filename = 'a/b/c/file' source = yadm_dir.join(f'alt/{filename}##{suffix}') source.write('test-data', ensure=True) - run = runner([paths.pgm, '-Y', yadm_dir, 'add', source]) + run = runner([ + paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'add', source]) assert run.success assert run.err == '' assert run.out == '' @@ -280,6 +281,7 @@ def test_ensure_alt_path(runner, paths, style): def setup_standard_yadm_dir(paths): """Configure a yadm home within the work tree""" std_yadm_dir = paths.work.mkdir('.config').mkdir('yadm') - std_yadm_dir.join('repo.git').mksymlinkto(paths.repo, absolute=1) + std_yadm_data = paths.work.mkdir('.local').mkdir('share').mkdir('yadm') + std_yadm_data.join('repo.git').mksymlinkto(paths.repo, absolute=1) std_yadm_dir.join('encrypt').mksymlinkto(paths.encrypt, absolute=1) - return std_yadm_dir + return std_yadm_dir, std_yadm_data diff --git a/test/test_unit_configure_paths.py b/test/test_unit_configure_paths.py index d0bf9f3..fbb05eb 100644 --- a/test/test_unit_configure_paths.py +++ b/test/test_unit_configure_paths.py @@ -9,12 +9,14 @@ ENCRYPT = 'encrypt' HOME = '/testhome' REPO = 'repo.git' YDIR = '.config/yadm' +YDATA = '.local/share/yadm' @pytest.mark.parametrize( 'override, expect', [ (None, {}), - ('-Y', {}), + ('-Y', {'yadm': 'YADM_DIR'}), + ('--yadm-data', {'data': 'YADM_DATA'}), ('--yadm-repo', {'repo': 'YADM_REPO', 'git': 'GIT_DIR'}), ('--yadm-config', {'config': 'YADM_CONFIG'}), ('--yadm-encrypt', {'encrypt': 'YADM_ENCRYPT'}), @@ -23,6 +25,7 @@ YDIR = '.config/yadm' ], ids=[ 'default', 'override yadm dir', + 'override yadm data', 'override repo', 'override config', 'override encrypt', @@ -36,6 +39,8 @@ def test_config(runner, paths, override, expect): args = [] if override == '-Y': matches = match_map('/' + opath) + if override == '--yadm-data': + matches = match_map(None, '/' + opath) if override: args = [override, '/' + opath] @@ -49,18 +54,20 @@ def test_config(runner, paths, override, expect): run_test(runner, paths, args, matches.values(), 0) -def match_map(yadm_dir=None): +def match_map(yadm_dir=None, yadm_data=None): """Create a dictionary of matches, relative to yadm_dir""" if not yadm_dir: yadm_dir = '/'.join([HOME, YDIR]) + if not yadm_data: + yadm_data = '/'.join([HOME, YDATA]) return { 'yadm': f'YADM_DIR="{yadm_dir}"', - 'repo': f'YADM_REPO="{yadm_dir}/{REPO}"', + 'repo': f'YADM_REPO="{yadm_data}/{REPO}"', 'config': f'YADM_CONFIG="{yadm_dir}/{CONFIG}"', 'encrypt': f'YADM_ENCRYPT="{yadm_dir}/{ENCRYPT}"', - 'archive': f'YADM_ARCHIVE="{yadm_dir}/{ARCHIVE}"', + 'archive': f'YADM_ARCHIVE="{yadm_data}/{ARCHIVE}"', 'bootstrap': f'YADM_BOOTSTRAP="{yadm_dir}/{BOOTSTRAP}"', - 'git': f'GIT_DIR="{yadm_dir}/{REPO}"', + 'git': f'GIT_DIR="{yadm_data}/{REPO}"', } @@ -70,6 +77,8 @@ def run_test(runner, paths, args, expected_matches, expected_code=0): script = f""" YADM_TEST=1 HOME="{HOME}" source {paths.pgm} process_global_args {argstring} + XDG_CONFIG_HOME= + XDG_DATA_HOME= HOME="{HOME}" set_yadm_dirs configure_paths declare -p | grep -E '(YADM|GIT)_' diff --git a/test/test_unit_set_local_alt_values.py b/test/test_unit_set_local_alt_values.py index d3d2447..827840f 100644 --- a/test/test_unit_set_local_alt_values.py +++ b/test/test_unit_set_local_alt_values.py @@ -26,7 +26,7 @@ def test_set_local_alt_values( script = f""" YADM_TEST=1 source {yadm} && set_operating_system && - YADM_DIR={paths.yadm} configure_paths && + YADM_DIR={paths.yadm} YADM_DATA={paths.data} configure_paths && set_local_alt_values echo "class='$local_class'" echo "os='$local_system'" diff --git a/test/test_unit_set_yadm_dir.py b/test/test_unit_set_yadm_dir.py index 0133023..32af8bf 100644 --- a/test/test_unit_set_yadm_dir.py +++ b/test/test_unit_set_yadm_dir.py @@ -3,29 +3,46 @@ import pytest @pytest.mark.parametrize( - 'condition', - ['basic', 'override', 'xdg_config_home'], + 'condition', [ + 'basic', + 'override', + 'override_data', + 'xdg_config_home', + 'xdg_data_home' + ], ) def test_set_yadm_dirs(runner, yadm, condition): """Test set_yadm_dirs""" setup = '' if condition == 'override': setup = 'YADM_DIR=/override' + elif condition == 'override_data': + setup = 'YADM_DATA=/override' elif condition == 'xdg_config_home': setup = 'XDG_CONFIG_HOME=/xdg' + elif condition == 'xdg_data_home': + setup = 'XDG_DATA_HOME=/xdg' script = f""" HOME=/testhome YADM_TEST=1 source {yadm} + XDG_CONFIG_HOME= + XDG_DATA_HOME= {setup} set_yadm_dirs - echo "$YADM_DIR" + echo "YADM_DIR=$YADM_DIR" + echo "YADM_DATA=$YADM_DATA" """ run = runner(command=['bash'], inp=script) assert run.success assert run.err == '' if condition == 'basic': - assert run.out.rstrip() == '/testhome/.config/yadm' + assert 'YADM_DIR=/testhome/.config/yadm' in run.out + assert 'YADM_DATA=/testhome/.local/share/yadm' in run.out elif condition == 'override': - assert run.out.rstrip() == '/override' + assert 'YADM_DIR=/override' in run.out + elif condition == 'override_data': + assert 'YADM_DATA=/override' in run.out elif condition == 'xdg_config_home': - assert run.out.rstrip() == '/xdg/yadm' + assert 'YADM_DIR=/xdg/yadm' in run.out + elif condition == 'xdg_data_home': + assert 'YADM_DATA=/xdg/yadm' in run.out