From fb1181c8a947e2270380930a9dcef621791d8d20 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Thu, 4 Apr 2019 18:01:27 -0500 Subject: [PATCH] Add tests for directory alternates While this feature was added back in version 1.05, tests were never added for it. These tests have identified bugs in the directory alternates. --- pylintrc | 5 ++- test/test_alt.py | 93 +++++++++++++++++++++++++++++++++++------------- test/utils.py | 47 ++++++++++++++---------- 3 files changed, 102 insertions(+), 43 deletions(-) diff --git a/pylintrc b/pylintrc index 2ae18b6..d686a0a 100644 --- a/pylintrc +++ b/pylintrc @@ -3,9 +3,12 @@ good-names=pytestmark [DESIGN] max-args=14 -max-locals=26 +max-locals=27 max-attributes=8 max-statements=65 [MESSAGES CONTROL] disable=redefined-outer-name + +[TYPECHECK] +ignored-modules=py diff --git a/test/test_alt.py b/test/test_alt.py index 60b7339..beb3b7f 100644 --- a/test/test_alt.py +++ b/test/test_alt.py @@ -3,6 +3,7 @@ import os import re import string +import py import pytest import utils @@ -33,6 +34,8 @@ WILD_TEMPLATES = [ '##$tst_class.$tst_sys.$tst_host.$tst_user', ] +TEST_PATHS = [utils.ALT_FILE1, utils.ALT_FILE2, utils.ALT_DIR] + WILD_TESTED = set() @@ -90,12 +93,18 @@ def test_alt(runner, yadm_y, paths, linked = linked_list(run.out) # assert the proper linking has occurred - for file_path in (utils.ALT_FILE1, utils.ALT_FILE2): + for file_path in TEST_PATHS: source_file = file_path + precedence[precedence_index] if tracked or (encrypt and not exclude): assert paths.work.join(file_path).islink() - assert paths.work.join(file_path).read() == source_file - assert str(paths.work.join(source_file)) in linked + target = py.path.local(paths.work.join(file_path).readlink()) + if target.isfile(): + assert paths.work.join(file_path).read() == source_file + assert str(paths.work.join(source_file)) in linked + else: + assert paths.work.join(file_path).join( + utils.CONTAINED).read() == source_file + assert str(paths.work.join(source_file)) in linked else: assert not paths.work.join(file_path).exists() assert str(paths.work.join(source_file)) not in linked @@ -169,8 +178,7 @@ def test_wild(request, runner, yadm_y, paths, test_key = f'{tracked}{encrypt}{wild_suffix}{std_suffix}' if test_key in WILD_TESTED: return - else: - WILD_TESTED.add(test_key) + WILD_TESTED.add(test_key) # set the class utils.set_local(paths, 'class', tst_class) @@ -186,11 +194,17 @@ def test_wild(request, runner, yadm_y, paths, linked = linked_list(run.out) # assert the proper linking has occurred - for file_path in (utils.ALT_FILE1, utils.ALT_FILE2): + for file_path in TEST_PATHS: source_file = file_path + wild_suffix assert paths.work.join(file_path).islink() - assert paths.work.join(file_path).read() == source_file - assert str(paths.work.join(source_file)) in linked + target = py.path.local(paths.work.join(file_path).readlink()) + if target.isfile(): + assert paths.work.join(file_path).read() == source_file + assert str(paths.work.join(source_file)) in linked + else: + assert paths.work.join(file_path).join( + utils.CONTAINED).read() == source_file + assert str(paths.work.join(source_file)) in linked # create files using the standard suffix utils.create_alt_files(paths, std_suffix, tracked=tracked, @@ -203,11 +217,17 @@ def test_wild(request, runner, yadm_y, paths, linked = linked_list(run.out) # assert the proper linking has occurred - for file_path in (utils.ALT_FILE1, utils.ALT_FILE2): + for file_path in TEST_PATHS: source_file = file_path + std_suffix assert paths.work.join(file_path).islink() - assert paths.work.join(file_path).read() == source_file - assert str(paths.work.join(source_file)) in linked + target = py.path.local(paths.work.join(file_path).readlink()) + if target.isfile(): + assert paths.work.join(file_path).read() == source_file + assert str(paths.work.join(source_file)) in linked + else: + assert paths.work.join(file_path).join( + utils.CONTAINED).read() == source_file + assert str(paths.work.join(source_file)) in linked @pytest.mark.usefixtures('ds1_copy') @@ -235,11 +255,17 @@ def test_local_override(runner, yadm_y, paths, linked = linked_list(run.out) # assert the proper linking has occurred - for file_path in (utils.ALT_FILE1, utils.ALT_FILE2): + for file_path in TEST_PATHS: source_file = file_path + '##or-class.or-os.or-hostname.or-user' assert paths.work.join(file_path).islink() - assert paths.work.join(file_path).read() == source_file - assert str(paths.work.join(source_file)) in linked + target = py.path.local(paths.work.join(file_path).readlink()) + if target.isfile(): + assert paths.work.join(file_path).read() == source_file + assert str(paths.work.join(source_file)) in linked + else: + assert paths.work.join(file_path).join( + utils.CONTAINED).read() == source_file + assert str(paths.work.join(source_file)) in linked @pytest.mark.parametrize('suffix', ['AAA', 'ZZZ', 'aaa', 'zzz']) @@ -267,11 +293,17 @@ def test_class_case(runner, yadm_y, paths, tst_sys, suffix): linked = linked_list(run.out) # assert the proper linking has occurred - for file_path in (utils.ALT_FILE1, utils.ALT_FILE2): + for file_path in TEST_PATHS: source_file = file_path + f'##{suffix}' assert paths.work.join(file_path).islink() - assert paths.work.join(file_path).read() == source_file - assert str(paths.work.join(source_file)) in linked + target = py.path.local(paths.work.join(file_path).readlink()) + if target.isfile(): + assert paths.work.join(file_path).read() == source_file + assert str(paths.work.join(source_file)) in linked + else: + assert paths.work.join(file_path).join( + utils.CONTAINED).read() == source_file + assert str(paths.work.join(source_file)) in linked @pytest.mark.parametrize('autoalt', [None, 'true', 'false']) @@ -294,15 +326,22 @@ def test_auto_alt(runner, yadm_y, paths, autoalt): linked = linked_list(run.out) # assert the proper linking has occurred - for file_path in (utils.ALT_FILE1, utils.ALT_FILE2): + for file_path in TEST_PATHS: source_file = file_path + suffix if autoalt == 'false': assert not paths.work.join(file_path).exists() else: assert paths.work.join(file_path).islink() - assert paths.work.join(file_path).read() == source_file - # no linking output when run via auto-alt - assert str(paths.work.join(source_file)) not in linked + target = py.path.local(paths.work.join(file_path).readlink()) + if target.isfile(): + assert paths.work.join(file_path).read() == source_file + # no linking output when run via auto-alt + assert str(paths.work.join(source_file)) not in linked + else: + assert paths.work.join(file_path).join( + utils.CONTAINED).read() == source_file + # no linking output when run via auto-alt + assert str(paths.work.join(source_file)) not in linked @pytest.mark.parametrize('delimiter', ['.', '_']) @@ -324,12 +363,18 @@ def test_delimiter(runner, yadm_y, paths, # assert the proper linking has occurred # only a delimiter of '.' is valid - for file_path in (utils.ALT_FILE1, utils.ALT_FILE2): + for file_path in TEST_PATHS: source_file = file_path + suffix if delimiter == '.': assert paths.work.join(file_path).islink() - assert paths.work.join(file_path).read() == source_file - assert str(paths.work.join(source_file)) in linked + target = py.path.local(paths.work.join(file_path).readlink()) + if target.isfile(): + assert paths.work.join(file_path).read() == source_file + assert str(paths.work.join(source_file)) in linked + else: + assert paths.work.join(file_path).join( + utils.CONTAINED).read() == source_file + assert str(paths.work.join(source_file)) in linked else: assert not paths.work.join(file_path).exists() assert str(paths.work.join(source_file)) not in linked diff --git a/test/utils.py b/test/utils.py index 411dde1..3303745 100644 --- a/test/utils.py +++ b/test/utils.py @@ -7,6 +7,11 @@ import os ALT_FILE1 = 'test_alt' ALT_FILE2 = 'test alt/test alt' +ALT_DIR = 'test alt/test alt dir' + +# Directory based alternates must have a tracked contained file. +# This will be the test contained file name +CONTAINED = 'contained_file' def set_local(paths, variable, value): @@ -29,31 +34,37 @@ def create_alt_files(paths, suffix, """ if not preserve: - if paths.work.join(ALT_FILE1).exists(): - paths.work.join(ALT_FILE1).remove(rec=1, ignore_errors=True) - assert not paths.work.join(ALT_FILE1).exists() - if paths.work.join(ALT_FILE2).exists(): - paths.work.join(ALT_FILE2).remove(rec=1, ignore_errors=True) - assert not paths.work.join(ALT_FILE2).exists() + for remove_path in (ALT_FILE1, ALT_FILE2, ALT_DIR): + if paths.work.join(remove_path).exists(): + paths.work.join(remove_path).remove(rec=1, ignore_errors=True) + assert not paths.work.join(remove_path).exists() new_file1 = paths.work.join(ALT_FILE1 + suffix) new_file1.write(ALT_FILE1 + suffix, ensure=True) new_file2 = paths.work.join(ALT_FILE2 + suffix) new_file2.write(ALT_FILE2 + suffix, ensure=True) - if content: - new_file1.write('\n' + content, mode='a', ensure=True) - new_file2.write('\n' + content, mode='a', ensure=True) - assert new_file1.exists() - assert new_file2.exists() + new_dir = paths.work.join(ALT_DIR + suffix).join(CONTAINED) + new_dir.write(ALT_DIR + suffix, ensure=True) + + # Do not test directory support for jinja alternates + test_paths = [new_file1, new_file2] + test_names = [ALT_FILE1, ALT_FILE2] + if suffix != '##yadm.j2': + test_paths += [new_dir] + test_names += [ALT_DIR] + + for test_path in test_paths: + if content: + test_path.write('\n' + content, mode='a', ensure=True) + assert test_path.exists() if tracked: - for path in (new_file1, new_file2): - os.system(f'GIT_DIR={str(paths.repo)} git add "{path}"') + for track_path in test_paths: + os.system(f'GIT_DIR={str(paths.repo)} git add "{track_path}"') os.system(f'GIT_DIR={str(paths.repo)} git commit -m "Add test files"') if encrypt: - paths.encrypt.write(f'{ALT_FILE1 + suffix}\n', mode='a') - paths.encrypt.write(f'{ALT_FILE2 + suffix}\n', mode='a') - if exclude: - paths.encrypt.write(f'!{ALT_FILE1 + suffix}\n', mode='a') - paths.encrypt.write(f'!{ALT_FILE2 + suffix}\n', mode='a') + for encrypt_name in test_names: + paths.encrypt.write(f'{encrypt_name + suffix}\n', mode='a') + if exclude: + paths.encrypt.write(f'!{encrypt_name + suffix}\n', mode='a')