Skip to content

Commit

Permalink
add unit test for checking updates to stencils
Browse files Browse the repository at this point in the history
  • Loading branch information
mzuther committed Mar 7, 2023
1 parent d5b6601 commit ad0235f
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{%- import 'stencils/common.jinja' as common -%}

{# -------------------------------------------------------------------------- #}

{%- set all_variables = {
'A': {
'repeats': 5,
},
'b': {
'repeats': 3,
},
} -%}

{# -------------------------------------------------------------------------- #}

{{- common.start_new_file('ab.txt') -}}

{{- common.render(all_variables) -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{%- import 'stencils/common.jinja' as common -%}

{# -------------------------------------------------------------------------- #}

{%- set all_variables = {
'c': {
'repeats': 2,
},
'D': {
'repeats': 10,
},
} -%}

{# -------------------------------------------------------------------------- #}

{{- common.start_new_file('cd.txt') -}}

{{- common.render(all_variables) -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{# Create a new file by inserting a special string into the output. #}
{# This allows you to create multiple files from a single template. #}

{%- macro start_new_file(filename) %}
### New file: {{ filename }}
### Content:
{% endmacro -%}

{# -------------------------------------------------------------------------- #}

{% macro render(variables) %}
{% for variable_key in variables -%}
{%- for n in range(variables[variable_key].repeats) -%}
{{ variable_key }}
{{- '.' if not loop.last }}
{%- endfor %}

{% endfor %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AAAAA
bbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cc
DDDDDDDDDD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A.A.A.A.A
b.b.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
c.c
D.D.D.D.D.D.D.D.D.D
57 changes: 56 additions & 1 deletion src/unittest/test_mascara.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def modify_file(partial_file_path):
assert saved_files == 2



# She updates a template and checks whether a partial run updates the
# respective output file.
def test_process_only_modified_2(self, tmp_path):
Expand Down Expand Up @@ -173,6 +172,62 @@ def update_file(partial_file_path):
assert saved_files == 1


# Mascara also checks whether updating a stencil changes any output files in
# a partial run. It does not.
def test_process_only_modified_3(self, tmp_path):

def convenience_run(partial_run, matching):
_, saved_files = self.run(
config_path,
process_only_modified=partial_run)

if matching:
self.compare_directories(config)
else:
with pytest.raises(AssertionError):
self.compare_directories(config)

return saved_files


def update_file(partial_file_path):
input_path = os.path.join(tmp_path, partial_file_path)
output_path = input_path.replace('_updated', '')

shutil.copyfile(input_path, output_path)

# ---------------------------------------------------------------------

config = {
'stencil_dir_name': 'stencils',
}

config_path = self.create_config(
config, tmp_path, 'settings.json')

unit_test_directory = '1_process_only_modified_3'

with open(config_path, mode='r') as f:
config = json.load(f)

# set up StempelWerk and execute full run
_, saved_files = self.run_and_compare(config_path, unit_test_directory)
assert saved_files == 2

update_file('10-templates_updated/stencils/common.jinja')

# partial run does not check for changed stencils
saved_files = convenience_run(partial_run=True, matching=True)
assert saved_files == 0

update_file('30-expected_updated/ab.txt')
update_file('30-expected_updated/cd.txt')

# full run applies changed stencils
saved_files = convenience_run(partial_run=False, matching=True)
assert saved_files == 2


# Mascara wants to get become more proficient in Python [ahem] and checks
# whether StempelWerk is really as lean as its developer promises.
def test_lean_template_removal(self, tmp_path):
Expand Down

0 comments on commit ad0235f

Please sign in to comment.