diff options
Diffstat (limited to 'test/test_unit_template_default.py')
-rw-r--r-- | test/test_unit_template_default.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/test/test_unit_template_default.py b/test/test_unit_template_default.py index 42464b8..639cb29 100644 --- a/test/test_unit_template_default.py +++ b/test/test_unit_template_default.py @@ -1,4 +1,7 @@ """Unit tests: template_default""" +import os + +FILE_MODE = 0o754 # these values are also testing the handling of bizarre characters LOCAL_CLASS = "default_Test+@-!^Class" @@ -85,12 +88,43 @@ Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again) end of template ''' +INCLUDE_BASIC = 'basic\n' +INCLUDE_VARIABLES = '''\ +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""" input_file = tmpdir.join('input') input_file.write(TEMPLATE, ensure=True) + input_file.chmod(FILE_MODE) output_file = tmpdir.join('output') script = f""" @@ -107,6 +141,7 @@ def test_template_default(runner, yadm, tmpdir): assert run.success assert run.err == '' assert output_file.read() == EXPECTED + assert os.stat(output_file).st_mode == os.stat(input_file).st_mode def test_source(runner, yadm, tmpdir): @@ -114,6 +149,7 @@ def test_source(runner, yadm, tmpdir): input_file = tmpdir.join('input') input_file.write('{{yadm.source}}', ensure=True) + input_file.chmod(FILE_MODE) output_file = tmpdir.join('output') script = f""" @@ -125,3 +161,38 @@ def test_source(runner, yadm, tmpdir): assert run.success 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 |