From 7bc8f02d68f04d6ed7e84dad66216591eb8700f7 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Fri, 12 Apr 2019 07:43:18 -0500 Subject: [PATCH] Add tests for jinja includes --- test/test_jinja.py | 22 ++++++++++++++++++---- test/utils.py | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/test/test_jinja.py b/test/test_jinja.py index 6e43f35..54d248a 100644 --- a/test/test_jinja.py +++ b/test/test_jinja.py @@ -13,7 +13,7 @@ def envtpl_present(runner): run = runner(command=['envtpl', '-h']) if run.success: return True - except BaseException: + except OSError: pass return False @@ -35,11 +35,20 @@ def test_local_override(runner, yadm_y, paths, 'j2-{{ YADM_CLASS }}-' '{{ YADM_OS }}-{{ YADM_HOSTNAME }}-' '{{ YADM_USER }}-{{ YADM_DISTRO }}' + '-{%- ' + f"include '{utils.INCLUDE_FILE}'" + ' -%}' + ) + expected = ( + f'j2-or-class-or-os-or-hostname-or-user-{tst_distro}' + f'-{utils.INCLUDE_CONTENT}' ) - expected = f'j2-or-class-or-os-or-hostname-or-user-{tst_distro}' - utils.create_alt_files(paths, '##yadm.j2', content=template) + utils.create_alt_files(paths, '##yadm.j2', content=template, + includefile=True) + # os.system(f'find {paths.work}' + ' -name *j2 -ls -exec cat \'{}\' ";"') + # os.system(f'find {paths.work}') # run alt to trigger linking run = runner(yadm_y('alt')) assert run.success @@ -145,15 +154,20 @@ def test_jinja(runner, yadm_y, paths, 'j2-{{ YADM_CLASS }}-' '{{ YADM_OS }}-{{ YADM_HOSTNAME }}-' '{{ YADM_USER }}-{{ YADM_DISTRO }}' + '-{%- ' + f"include '{utils.INCLUDE_FILE}'" + ' -%}' ) expected = ( f'j2-{tst_class}-' f'{tst_sys}-{tst_host}-' f'{tst_user}-{tst_distro}' + f'-{utils.INCLUDE_CONTENT}' ) utils.create_alt_files(paths, jinja_suffix, content=template, - tracked=tracked, encrypt=encrypt, exclude=exclude) + tracked=tracked, encrypt=encrypt, exclude=exclude, + includefile=True) # run alt to trigger linking run = runner(yadm_y('alt')) diff --git a/test/utils.py b/test/utils.py index 3303745..577281c 100644 --- a/test/utils.py +++ b/test/utils.py @@ -13,6 +13,12 @@ ALT_DIR = 'test alt/test alt dir' # This will be the test contained file name CONTAINED = 'contained_file' +# These variables are used for making include files which will be processed +# within jinja templates +INCLUDE_FILE = 'inc_file' +INCLUDE_DIRS = ['', 'test alt'] +INCLUDE_CONTENT = '8780846c02e34c930d0afd127906668f' + def set_local(paths, variable, value): """Set local override""" @@ -25,7 +31,7 @@ def set_local(paths, variable, value): def create_alt_files(paths, suffix, preserve=False, tracked=True, encrypt=False, exclude=False, - content=None): + content=None, includefile=False): """Create new files, and add to the repo This is used for testing alternate files. In each case, a suffix is @@ -58,6 +64,8 @@ def create_alt_files(paths, suffix, test_path.write('\n' + content, mode='a', ensure=True) assert test_path.exists() + create_includefiles(includefile, paths, test_paths) + if tracked: for track_path in test_paths: os.system(f'GIT_DIR={str(paths.repo)} git add "{track_path}"') @@ -68,3 +76,12 @@ def create_alt_files(paths, suffix, paths.encrypt.write(f'{encrypt_name + suffix}\n', mode='a') if exclude: paths.encrypt.write(f'!{encrypt_name + suffix}\n', mode='a') + + +def create_includefiles(includefile, paths, test_paths): + """Generate files for testing jinja includes""" + if includefile: + for dpath in INCLUDE_DIRS: + incfile = paths.work.join(dpath + '/' + INCLUDE_FILE) + incfile.write(INCLUDE_CONTENT, ensure=True) + test_paths += [incfile]