1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2026-03-02 03:49:29 +00:00

Add support for including files using the default template processor

The syntax is '{% include "file" %}' where file is either an absolute path or a
path relative to the current template file's directory.

Variables in the included file will be replaced as for the main template. But
the included file can't include files itself.
This commit is contained in:
Erik Flodin
2020-10-09 15:36:58 +02:00
parent 48d77c9f21
commit 9bcf070dfe
3 changed files with 97 additions and 3 deletions

View File

@@ -88,6 +88,36 @@ Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
end of template
'''
INCLUDE_BASIC = 'basic\n'
INCLUDE_VARIABLES = f'''\
included <{{{{ yadm.class }}}}> file
empty line above
'''
INCLUDE_NESTED = 'no newline at the end'
TEMPLATE_INCLUDE = '''\
The first line
{% include empty %}
An empty file removes the line above
{%include basic%}
{% include "./variables.{{ yadm.os }}" %}
{% include dir/nested %}
Include basic again:
{% include basic %}
'''
EXPECTED_INCLUDE = f'''\
The first line
An empty file removes the line above
basic
included <{LOCAL_CLASS}> file
empty line above
no newline at the end
Include basic again:
basic
'''
def test_template_default(runner, yadm, tmpdir):
"""Test template_default"""
@@ -132,3 +162,37 @@ def test_source(runner, yadm, tmpdir):
assert run.err == ''
assert output_file.read().strip() == str(input_file)
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
def test_include(runner, yadm, tmpdir):
"""Test include"""
empty_file = tmpdir.join('empty')
empty_file.write('', ensure=True)
basic_file = tmpdir.join('basic')
basic_file.write(INCLUDE_BASIC)
variables_file = tmpdir.join(f'variables.{LOCAL_SYSTEM}')
variables_file.write(INCLUDE_VARIABLES)
nested_file = tmpdir.join('dir').join('nested')
nested_file.write(INCLUDE_NESTED, ensure=True)
input_file = tmpdir.join('input')
input_file.write(TEMPLATE_INCLUDE)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
script = f"""
YADM_TEST=1 source {yadm}
set_awk
local_class="{LOCAL_CLASS}"
local_system="{LOCAL_SYSTEM}"
template_default "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
assert output_file.read() == EXPECTED_INCLUDE
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode