1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
# Copyright 2016-2018 Dirk Thomas
# Licensed under the Apache License, Version 2.0
from pathlib import Path
import sys
from tempfile import TemporaryDirectory
from unittest.mock import patch
from colcon_core.shell.template import expand_template
from em import TransientParseError
import pytest
def test_expand_template():
with TemporaryDirectory(prefix='test_colcon_') as base_path:
template_path = Path(base_path) / 'template.em'
destination_path = Path(base_path) / 'expanded_template'
# invalid template, missing @[end if]
template_path.write_text(
'@[if True]')
with pytest.raises(TransientParseError):
with patch('colcon_core.shell.template.logger.error') as error:
expand_template(template_path, destination_path, {})
# the raised exception is catched and results in an error message
assert error.call_count == 1
assert len(error.call_args[0]) == 1
assert error.call_args[0][0].endswith(
f" processing template '{template_path}'")
assert not destination_path.exists()
# missing variable
template_path.write_text(
'@(var)')
with pytest.raises(NameError):
with patch('colcon_core.shell.template.logger.error') as error:
expand_template(template_path, destination_path, {})
# the raised exception is catched and results in an error message
assert error.call_count == 1
assert len(error.call_args[0]) == 1
assert error.call_args[0][0].endswith(
f" processing template '{template_path}'")
assert not destination_path.exists()
# proper use
expand_template(template_path, destination_path, {'var': 'value1'})
assert destination_path.exists()
assert destination_path.read_text() == 'value1'
# overwrite with a different value
expand_template(template_path, destination_path, {'var': 'value2'})
assert destination_path.exists()
assert destination_path.read_text() == 'value2'
destination_path.unlink()
# skip all symlink tests on Windows for now
if sys.platform == 'win32': # pragma: no cover
return
# remove destination if it is a symlink
destination_path.symlink_to(template_path)
assert destination_path.is_symlink()
expand_template(template_path, destination_path, {'var': 'value'})
assert not destination_path.is_symlink()
assert destination_path.exists()
|