From cc4234b07474d6b3cc933e5c23ce12caf2076eda Mon Sep 17 00:00:00 2001 From: Dominik Holland Date: Thu, 22 Jul 2021 16:01:53 +0200 Subject: Add a unitest to verify that all jinja errors are handled correctly --- tests/templates/syntaxError.txt | 2 ++ tests/templates/undefinedVariable.txt | 1 + tests/test_generator.py | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/templates/syntaxError.txt create mode 100644 tests/templates/undefinedVariable.txt diff --git a/tests/templates/syntaxError.txt b/tests/templates/syntaxError.txt new file mode 100644 index 0000000..6ecde06 --- /dev/null +++ b/tests/templates/syntaxError.txt @@ -0,0 +1,2 @@ +{% fooo %} +{{this_is_not_defined}} diff --git a/tests/templates/undefinedVariable.txt b/tests/templates/undefinedVariable.txt new file mode 100644 index 0000000..b696491 --- /dev/null +++ b/tests/templates/undefinedVariable.txt @@ -0,0 +1 @@ +{{this_is_not_defined + 42}} diff --git a/tests/test_generator.py b/tests/test_generator.py index 9aa6e71..f83fb1b 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -1,6 +1,9 @@ from qface.generator import FileSystem, Generator +from unittest.mock import patch +from io import StringIO import logging import logging.config +import tempfile from path import Path # logging.config.fileConfig('logging.ini') @@ -69,3 +72,42 @@ def test_destination_prefix(): path = generator.apply(dst_template, ctx) assert Path(path).exists() == True out.rmtree_p() + +@patch('sys.stderr', new_callable=StringIO) +def test_error_template_syntax_error(mock_stderr): + tmpDir = tempfile.TemporaryDirectory() + src = [inputPath, inputPath / 'com.pelagicore.ivi.climate.qface'] + system = FileSystem.parse(src) + dst_template = '{{out}}/out.txt' + ctx = {'out': tmpDir.name} + generator = Generator(search_path='tests/templates') + generator.write(dst_template, 'syntaxError.txt', ctx) + path = generator.apply(dst_template, ctx) + assert Path(path).exists() == False + assert mock_stderr.getvalue() == "tests/templates/syntaxError.txt:1: error: Encountered unknown tag 'fooo'.\n" + +@patch('sys.stderr', new_callable=StringIO) +def test_error_template_undefined_variable(mock_stderr): + tmpDir = tempfile.TemporaryDirectory() + src = [inputPath, inputPath / 'com.pelagicore.ivi.climate.qface'] + system = FileSystem.parse(src) + dst_template = '{{out}}/out.txt' + ctx = {'out': tmpDir.name} + generator = Generator(search_path='tests/templates') + generator.write(dst_template, 'undefinedVariable.txt', ctx) + path = generator.apply(dst_template, ctx) + assert Path(path).exists() == False + assert mock_stderr.getvalue() == "tests/templates/undefinedVariable.txt:1: error: 'this_is_not_defined' is undefined\n" + +@patch('sys.stderr', new_callable=StringIO) +def test_error_template_doesnt_exist(mock_stderr): + tmpDir = tempfile.TemporaryDirectory() + src = [inputPath, inputPath / 'com.pelagicore.ivi.climate.qface'] + system = FileSystem.parse(src) + dst_template = '{{out}}/out.txt' + ctx = {'out': tmpDir.name} + generator = Generator(search_path='tests/templates') + generator.write(dst_template, 'doesnt_exist.txt', ctx) + path = generator.apply(dst_template, ctx) + assert Path(path).exists() == False + assert mock_stderr.getvalue() == "/doesnt_exist.txt: error: Template not found\n" -- cgit v1.2.1