summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Holland <dominik.holland@qt.io>2021-07-22 16:01:53 +0200
committerDominik Holland <dominik.holland@googlemail.com>2022-01-27 13:50:04 +0100
commitcc4234b07474d6b3cc933e5c23ce12caf2076eda (patch)
tree1e6aa17d7c6ffcd9d07d9492f666407dbb9738f3
parent4f9a87655bae24b5fbd555215e838f54cf0c0316 (diff)
downloadqtivi-qface-cc4234b07474d6b3cc933e5c23ce12caf2076eda.tar.gz
Add a unitest to verify that all jinja errors are handled correctly
-rw-r--r--tests/templates/syntaxError.txt2
-rw-r--r--tests/templates/undefinedVariable.txt1
-rw-r--r--tests/test_generator.py42
3 files changed, 45 insertions, 0 deletions
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"