diff options
Diffstat (limited to 'tests/test_generator.py')
-rw-r--r-- | tests/test_generator.py | 42 |
1 files changed, 42 insertions, 0 deletions
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" |