Add tests for jinja includes
This commit is contained in:
parent
d245f633bf
commit
7bc8f02d68
2 changed files with 36 additions and 5 deletions
|
@ -13,7 +13,7 @@ def envtpl_present(runner):
|
||||||
run = runner(command=['envtpl', '-h'])
|
run = runner(command=['envtpl', '-h'])
|
||||||
if run.success:
|
if run.success:
|
||||||
return True
|
return True
|
||||||
except BaseException:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -35,11 +35,20 @@ def test_local_override(runner, yadm_y, paths,
|
||||||
'j2-{{ YADM_CLASS }}-'
|
'j2-{{ YADM_CLASS }}-'
|
||||||
'{{ YADM_OS }}-{{ YADM_HOSTNAME }}-'
|
'{{ YADM_OS }}-{{ YADM_HOSTNAME }}-'
|
||||||
'{{ YADM_USER }}-{{ YADM_DISTRO }}'
|
'{{ 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 alt to trigger linking
|
||||||
run = runner(yadm_y('alt'))
|
run = runner(yadm_y('alt'))
|
||||||
assert run.success
|
assert run.success
|
||||||
|
@ -145,15 +154,20 @@ def test_jinja(runner, yadm_y, paths,
|
||||||
'j2-{{ YADM_CLASS }}-'
|
'j2-{{ YADM_CLASS }}-'
|
||||||
'{{ YADM_OS }}-{{ YADM_HOSTNAME }}-'
|
'{{ YADM_OS }}-{{ YADM_HOSTNAME }}-'
|
||||||
'{{ YADM_USER }}-{{ YADM_DISTRO }}'
|
'{{ YADM_USER }}-{{ YADM_DISTRO }}'
|
||||||
|
'-{%- '
|
||||||
|
f"include '{utils.INCLUDE_FILE}'"
|
||||||
|
' -%}'
|
||||||
)
|
)
|
||||||
expected = (
|
expected = (
|
||||||
f'j2-{tst_class}-'
|
f'j2-{tst_class}-'
|
||||||
f'{tst_sys}-{tst_host}-'
|
f'{tst_sys}-{tst_host}-'
|
||||||
f'{tst_user}-{tst_distro}'
|
f'{tst_user}-{tst_distro}'
|
||||||
|
f'-{utils.INCLUDE_CONTENT}'
|
||||||
)
|
)
|
||||||
|
|
||||||
utils.create_alt_files(paths, jinja_suffix, content=template,
|
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 alt to trigger linking
|
||||||
run = runner(yadm_y('alt'))
|
run = runner(yadm_y('alt'))
|
||||||
|
|
|
@ -13,6 +13,12 @@ ALT_DIR = 'test alt/test alt dir'
|
||||||
# This will be the test contained file name
|
# This will be the test contained file name
|
||||||
CONTAINED = 'contained_file'
|
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):
|
def set_local(paths, variable, value):
|
||||||
"""Set local override"""
|
"""Set local override"""
|
||||||
|
@ -25,7 +31,7 @@ def set_local(paths, variable, value):
|
||||||
def create_alt_files(paths, suffix,
|
def create_alt_files(paths, suffix,
|
||||||
preserve=False, tracked=True,
|
preserve=False, tracked=True,
|
||||||
encrypt=False, exclude=False,
|
encrypt=False, exclude=False,
|
||||||
content=None):
|
content=None, includefile=False):
|
||||||
"""Create new files, and add to the repo
|
"""Create new files, and add to the repo
|
||||||
|
|
||||||
This is used for testing alternate files. In each case, a suffix is
|
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)
|
test_path.write('\n' + content, mode='a', ensure=True)
|
||||||
assert test_path.exists()
|
assert test_path.exists()
|
||||||
|
|
||||||
|
create_includefiles(includefile, paths, test_paths)
|
||||||
|
|
||||||
if tracked:
|
if tracked:
|
||||||
for track_path in test_paths:
|
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 add "{track_path}"')
|
||||||
|
@ -68,3 +76,12 @@ def create_alt_files(paths, suffix,
|
||||||
paths.encrypt.write(f'{encrypt_name + suffix}\n', mode='a')
|
paths.encrypt.write(f'{encrypt_name + suffix}\n', mode='a')
|
||||||
if exclude:
|
if exclude:
|
||||||
paths.encrypt.write(f'!{encrypt_name + suffix}\n', mode='a')
|
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]
|
||||||
|
|
Loading…
Reference in a new issue