Add tests for jinja includes

This commit is contained in:
Tim Byrne 2019-04-12 07:43:18 -05:00
parent d245f633bf
commit 7bc8f02d68
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
2 changed files with 36 additions and 5 deletions

View File

@ -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'))

View File

@ -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]