From 7db61908aeef4307bf6ca56d0f7d6b0583ca0b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=81?= Date: Wed, 26 Feb 2020 14:01:13 +0300 Subject: [PATCH] The structure of test directories is changed, template_engine module is added. --- calculate/templates/template_engine.py | 298 ++++++++++++++++++ pytest.ini | 1 + tests/{format => templates}/__init__.py | 0 tests/templates/format/__init__.py | 0 tests/{ => templates}/format/test_base.py | 6 +- tests/{ => templates}/format/test_bind.py | 6 +- tests/{ => templates}/format/test_compiz.py | 6 +- tests/{ => templates}/format/test_diff.py | 6 +- tests/{ => templates}/format/test_dovecot.py | 6 +- tests/{ => templates}/format/test_json.py | 6 +- tests/{ => templates}/format/test_kde.py | 6 +- tests/{ => templates}/format/test_kernel.py | 6 +- tests/{ => templates}/format/test_ldap.py | 8 +- tests/{ => templates}/format/test_openrc.py | 6 +- tests/{ => templates}/format/test_patch.py | 0 tests/{ => templates}/format/test_postfix.py | 6 +- tests/{ => templates}/format/test_procmail.py | 6 +- tests/{ => templates}/format/test_proftpd.py | 6 +- tests/{ => templates}/format/test_samba.py | 6 +- .../{ => templates}/format/test_xml_gconf.py | 6 +- tests/{ => templates}/format/test_xml_xfce.py | 6 +- .../format/testfiles/a/dir/file1.txt | 0 .../format/testfiles/a/dir/file2.txt | 0 .../format/testfiles/a1/dir/file1.txt | 0 .../format/testfiles/b/dir/file1.txt | 0 .../format/testfiles/b/dir/file2.txt | 0 .../format/testfiles/b1/dir/file1.txt | 0 .../format/testfiles/b1/dir/file2.txt | 0 .../format/testfiles/bind_original.conf | 0 .../format/testfiles/bind_result.conf | 0 .../format/testfiles/bind_template.conf | 0 .../format/testfiles/compiz_original | 0 .../format/testfiles/compiz_result | 0 .../format/testfiles/compiz_template | 0 .../format/testfiles/diff_1.patch | 0 .../format/testfiles/diff_2.patch | 0 .../format/testfiles/dovecot_original.conf | 0 .../format/testfiles/dovecot_result.conf | 0 .../format/testfiles/dovecot_template.conf | 0 .../format/testfiles/json_original.json | 0 .../format/testfiles/json_result.json | 0 .../format/testfiles/json_template.json | 0 .../format/testfiles/kde_original | 0 .../format/testfiles/kde_result | 0 .../format/testfiles/kde_template | 0 .../format/testfiles/kernel_original | 0 .../format/testfiles/kernel_result | 0 .../format/testfiles/kernel_template | 0 .../testfiles/ldap_logic_lines_test.txt | 0 .../format/testfiles/ldap_original.conf | 0 .../format/testfiles/ldap_result.conf | 0 .../format/testfiles/ldap_template.conf | 0 .../format/testfiles/logic_lines_test.txt | 0 .../testfiles/logic_lines_test_input.txt | 0 .../testfiles/logic_lines_test_output.txt | 0 .../format/testfiles/openrc_original | 0 .../format/testfiles/openrc_result | 0 .../format/testfiles/openrc_template | 0 .../format/testfiles/postfix_original | 0 .../format/testfiles/postfix_result | 0 .../format/testfiles/postfix_template | 0 .../format/testfiles/procmail_original | 0 .../format/testfiles/procmail_result | 0 .../format/testfiles/procmail_template | 0 .../format/testfiles/proftpd_original.conf | 0 .../format/testfiles/proftpd_result.conf | 0 .../format/testfiles/proftpd_template.conf | 0 .../format/testfiles/samba_original | 0 .../format/testfiles/samba_result | 0 .../format/testfiles/samba_template | 0 .../format/testfiles/xml_gconf_original.xml | 0 .../format/testfiles/xml_gconf_result.xml | 0 .../format/testfiles/xml_gconf_template.xml | 0 .../format/testfiles/xml_xfce_original.xml | 0 .../format/testfiles/xml_xfce_result.xml | 0 .../format/testfiles/xml_xfce_template.xml | 0 tests/templates/test_template_engine.py | 120 +++++++ 77 files changed, 468 insertions(+), 49 deletions(-) create mode 100644 calculate/templates/template_engine.py rename tests/{format => templates}/__init__.py (100%) create mode 100644 tests/templates/format/__init__.py rename tests/{ => templates}/format/test_base.py (92%) rename tests/{ => templates}/format/test_bind.py (97%) rename tests/{ => templates}/format/test_compiz.py (96%) rename tests/{ => templates}/format/test_diff.py (94%) rename tests/{ => templates}/format/test_dovecot.py (96%) rename tests/{ => templates}/format/test_json.py (82%) rename tests/{ => templates}/format/test_kde.py (97%) rename tests/{ => templates}/format/test_kernel.py (93%) rename tests/{ => templates}/format/test_ldap.py (97%) rename tests/{ => templates}/format/test_openrc.py (95%) rename tests/{ => templates}/format/test_patch.py (100%) rename tests/{ => templates}/format/test_postfix.py (95%) rename tests/{ => templates}/format/test_procmail.py (94%) rename tests/{ => templates}/format/test_proftpd.py (96%) rename tests/{ => templates}/format/test_samba.py (97%) rename tests/{ => templates}/format/test_xml_gconf.py (93%) rename tests/{ => templates}/format/test_xml_xfce.py (88%) rename tests/{ => templates}/format/testfiles/a/dir/file1.txt (100%) rename tests/{ => templates}/format/testfiles/a/dir/file2.txt (100%) rename tests/{ => templates}/format/testfiles/a1/dir/file1.txt (100%) rename tests/{ => templates}/format/testfiles/b/dir/file1.txt (100%) rename tests/{ => templates}/format/testfiles/b/dir/file2.txt (100%) rename tests/{ => templates}/format/testfiles/b1/dir/file1.txt (100%) rename tests/{ => templates}/format/testfiles/b1/dir/file2.txt (100%) rename tests/{ => templates}/format/testfiles/bind_original.conf (100%) rename tests/{ => templates}/format/testfiles/bind_result.conf (100%) rename tests/{ => templates}/format/testfiles/bind_template.conf (100%) rename tests/{ => templates}/format/testfiles/compiz_original (100%) rename tests/{ => templates}/format/testfiles/compiz_result (100%) rename tests/{ => templates}/format/testfiles/compiz_template (100%) rename tests/{ => templates}/format/testfiles/diff_1.patch (100%) rename tests/{ => templates}/format/testfiles/diff_2.patch (100%) rename tests/{ => templates}/format/testfiles/dovecot_original.conf (100%) rename tests/{ => templates}/format/testfiles/dovecot_result.conf (100%) rename tests/{ => templates}/format/testfiles/dovecot_template.conf (100%) rename tests/{ => templates}/format/testfiles/json_original.json (100%) rename tests/{ => templates}/format/testfiles/json_result.json (100%) rename tests/{ => templates}/format/testfiles/json_template.json (100%) rename tests/{ => templates}/format/testfiles/kde_original (100%) rename tests/{ => templates}/format/testfiles/kde_result (100%) rename tests/{ => templates}/format/testfiles/kde_template (100%) rename tests/{ => templates}/format/testfiles/kernel_original (100%) rename tests/{ => templates}/format/testfiles/kernel_result (100%) rename tests/{ => templates}/format/testfiles/kernel_template (100%) rename tests/{ => templates}/format/testfiles/ldap_logic_lines_test.txt (100%) rename tests/{ => templates}/format/testfiles/ldap_original.conf (100%) rename tests/{ => templates}/format/testfiles/ldap_result.conf (100%) rename tests/{ => templates}/format/testfiles/ldap_template.conf (100%) rename tests/{ => templates}/format/testfiles/logic_lines_test.txt (100%) rename tests/{ => templates}/format/testfiles/logic_lines_test_input.txt (100%) rename tests/{ => templates}/format/testfiles/logic_lines_test_output.txt (100%) rename tests/{ => templates}/format/testfiles/openrc_original (100%) rename tests/{ => templates}/format/testfiles/openrc_result (100%) rename tests/{ => templates}/format/testfiles/openrc_template (100%) rename tests/{ => templates}/format/testfiles/postfix_original (100%) rename tests/{ => templates}/format/testfiles/postfix_result (100%) rename tests/{ => templates}/format/testfiles/postfix_template (100%) rename tests/{ => templates}/format/testfiles/procmail_original (100%) rename tests/{ => templates}/format/testfiles/procmail_result (100%) rename tests/{ => templates}/format/testfiles/procmail_template (100%) rename tests/{ => templates}/format/testfiles/proftpd_original.conf (100%) rename tests/{ => templates}/format/testfiles/proftpd_result.conf (100%) rename tests/{ => templates}/format/testfiles/proftpd_template.conf (100%) rename tests/{ => templates}/format/testfiles/samba_original (100%) rename tests/{ => templates}/format/testfiles/samba_result (100%) rename tests/{ => templates}/format/testfiles/samba_template (100%) rename tests/{ => templates}/format/testfiles/xml_gconf_original.xml (100%) rename tests/{ => templates}/format/testfiles/xml_gconf_result.xml (100%) rename tests/{ => templates}/format/testfiles/xml_gconf_template.xml (100%) rename tests/{ => templates}/format/testfiles/xml_xfce_original.xml (100%) rename tests/{ => templates}/format/testfiles/xml_xfce_result.xml (100%) rename tests/{ => templates}/format/testfiles/xml_xfce_template.xml (100%) create mode 100644 tests/templates/test_template_engine.py diff --git a/calculate/templates/template_engine.py b/calculate/templates/template_engine.py new file mode 100644 index 0000000..2aae1eb --- /dev/null +++ b/calculate/templates/template_engine.py @@ -0,0 +1,298 @@ +from jinja2.ext import Extension +from jinja2 import Environment, FileSystemLoader, TemplateSyntaxError, nodes +from jinja2.utils import missing +from jinja2.runtime import Context, Undefined +from collections.abc import MutableMapping + + +class Variables(MutableMapping): + '''Класс заглушка вместо модуля переменных.''' + def __init__(self, *args, **kwargs): + self.__attrs = dict(*args, **kwargs) + + def __next__(self): + iterator = iter(self.__attrs) + return next(iterator) + + def __getattribute__(self, name): + if name == '_Variables__attrs': + return super().__getattribute__(name) + try: + return self.__attrs[name] + except KeyError: + raise AttributeError(name) + + def __getitem__(self, name): + return self.__attrs[name] + + def __setitem__(self, name, value): + self.__attrs[name] = value + + def __delitem__(self, name): + del self.__attrs[name] + + def __iter__(self): + return iter(self.__attrs) + + def __len__(self): + return len(self.__attrs) + + def __repr__(self): + return ''.format(self.__attrs) + + +def resolve_or_missing(context, key, missing=missing, env={}): + datavars = context.parent['__datavars__'] + if key in context.vars: + return context.vars[key] + if key in context.parent: + return context.parent[key] + if key in datavars: + return datavars[key] + for name in env: + if name in datavars and key in datavars[name]: + return datavars[name][key] + return missing + + +class CalculateContext(Context): + _env_set = set() + + def resolve(self, key): + if self._legacy_resolve_mode: + rv = resolve_or_missing(self, key, + env=self._env_set) + else: + rv = self.resolve_or_missing(key) + if rv is missing: + return self.environment.undefined(name=key) + return rv + + def resolve_or_missing(self, key): + if self._legacy_resolve_mode: + rv = self.resolve(key) + if isinstance(rv, Undefined): + rv = missing + return rv + return resolve_or_missing(self, key, + env=self._env_set) + + +class Parameters(MutableMapping): + '''Класс для хранения параметров и условий, взятых из шаблона, и передачи + их шаблонизатору.''' + def __init__(self, parameters_dictionary={}, condition=True): + self.__parameters = parameters_dictionary + self.__condition = condition + + def set_parameters(self, *args, **kwargs): + parameters = dict(*args, **kwargs) + self.__parameters.update(parameters) + + def set_condition(self, condition): + self.__condition = self.__condition and condition + + def __getitem__(self, name): + return self.__parameters[name] + + def __setitem__(self, name, value): + self.__parameters[name] = value + + def __delitem__(self, name): + del self.__parameters[name] + + def __iter__(self): + return iter(self.__parameters) + + def __len__(self): + return len(self.__parameters) + + def __repr__(self): + return ''.format(self.__parameters, + self.__condition) + + @property + def condition(self): + return self.__condition + + @property + def parameters(self): + return self.__parameters + + +class CalculateExtension(Extension): + _parameters_set = set() + + def __init__(self, environment): + self.tags = {'calculate', 'save', 'set_var'} + self.CONDITION_TOKENS_TYPES = {'eq', 'ne', 'lt', 'gt', 'lteq', 'gteq'} + self.LITERAL_TOKENS_TYPES = {'string', 'integer', 'float'} + self.TARGET_FILES_SET = {'grp', 'system', 'etc', 'local', 'remote'} + + self.parse_methods = {'calculate': self.parse_calculate, + 'save': self.parse_save} + + def parse(self, parser): + self.parser = parser + self.stream = parser.stream + tag_token = self.stream.current.value + return [self.parse_methods[tag_token]()] + + def parse_save(self): + lineno = next(self.stream).lineno + target_file = nodes.Const('', lineno=lineno) + + if self.stream.skip_if('dot'): + target_file_name = self.stream.expect('name').value + if target_file_name in self.TARGET_FILES_SET: + target_file = nodes.Const(target_file_name) + else: + TemplateSyntaxError("Unknown target file '{}'". + format(target_file_name), + lineno=lineno) + + # получаем список из имени переменной. + module_name = self.stream.expect('name').value + variable_name = [nodes.Const(module_name, lineno=lineno)] + while self.stream.skip_if('dot'): + name = self.stream.expect('name').value + variable_name.append(nodes.Const(name, lineno=lineno)) + variable_name = nodes.List(variable_name, lineno=lineno) + + if self.stream.skip_if('assign'): + right_value = self.parser.parse_expression(with_condexpr=True) + save_variable_node = self.call_method('save_variable', + [variable_name, + right_value, + target_file, + nodes.ContextReference()], + lineno=lineno) + return nodes.Output([save_variable_node], lineno=lineno) + else: + TemplateSyntaxError("'=' is expected in 'save' tag.", + lineno=lineno) + + def parse_calculate(self): + pairs_list = [] + expect_comma_flag = False + conditions = nodes.Const(True) + + lineno = next(self.stream).lineno + + while self.stream.current.type != 'block_end': + if expect_comma_flag: + self.stream.expect('comma') + + if self.stream.current.type == 'name': + if (self.stream.current.value in self._parameters_set and + self.stream.look().type != 'dot'): + pairs_list.append(self.get_parameter_node()) + else: + conditions = nodes.And( + self.parser.parse_expression( + with_condexpr=True + ), + conditions + ) + else: + raise TemplateSyntaxError('Name is expected in calculate tag.', + lineno=self.stream.current.lineno) + expect_comma_flag = True + dictionary_node = nodes.Dict(pairs_list) + save_node = self.call_method('save_parameters', + [dictionary_node, + conditions, + nodes.ContextReference()], + lineno=lineno) + return nodes.Output([save_node], lineno=lineno) + + def save_variable(self, variable_name, right_value, target_file, context): + '''Временный метод для сохранения значений переменных.''' + datavars = context.parent['__datavars__'] + module_name = variable_name[0] + namespaces = variable_name[1:-1] + variable_name = variable_name[-1] + + if module_name in datavars: + variables_module = datavars[module_name] + for namespace in namespaces: + if namespace not in variables_module: + variables_module[namespace] = Variables() + variables_module = variables_module[namespace] + variables_module[variable_name] = right_value + else: + AttributeError("Unknown variables module '{}'". + format(module_name)) + return '' + + def get_parameter_node(self): + lineno = self.stream.current.lineno + parameter_name = self.stream.expect('name').value + parameter_name_node = nodes.Const(parameter_name, lineno=lineno) + if self.stream.skip_if('assign'): + parameter_value = self.stream.current.value + parameter_rvalue = self.parser.parse_expression(with_condexpr=True) + if parameter_name == 'env': + env_names = parameter_value.split(',') + for name in env_names: + CalculateContext._env_set.add(name.strip()) + else: + parameter_rvalue = nodes.Const(True, lineno=lineno) + return nodes.Pair(parameter_name_node, parameter_rvalue) + + def save_parameters(cls, parameters_dictionary, compare_result, context): + context.parent['__parameters__'].set_parameters(parameters_dictionary) + context.parent['__parameters__'].set_condition(compare_result) + return '' + + +class TemplateEngine(): + def __init__(self, directory_path='/', + parameters_set=set(), + env_set=set(), + datavars_module=Variables()): + CalculateExtension._parameters_set = parameters_set + + self._datavars_module = datavars_module + self._parameters_object = Parameters() + self._template_text = '' + + self.environment = Environment(loader=FileSystemLoader(directory_path), + extensions=[CalculateExtension]) + self.environment.context_class = CalculateContext + + def change_directory(self, directory_path): + self.environment.loader = FileSystemLoader(directory_path) + + def process_template(self, template_path, env=set()): + CalculateContext._env_set = env + template = self.environment.get_template(template_path) + self._parameters_object = Parameters(parameters_dictionary={}, + condition=True) + self._template_text = template.render( + __datavars__=self._datavars_module, + __parameters__=self._parameters_object + ) + + def process_template_from_string(self, string, env=set()): + CalculateContext._env_set = env + template = self.environment.from_string(string) + self._parameters_object = Parameters(parameters_dictionary={}, + condition=True) + self._template_text = template.render( + __datavars__=self._datavars_module, + __parameters__=self._parameters_object + ) + + @property + def condition(self): + return self._parameters_object.condition + + @property + def parameters(self): + return self._parameters_object.parameters + + @property + def template_text(self): + text, self._template_text = self._template_text, '' + return text diff --git a/pytest.ini b/pytest.ini index 7744e26..8dd47a3 100644 --- a/pytest.ini +++ b/pytest.ini @@ -21,3 +21,4 @@ markers = xml_xfce: marker for running tests for xml xfce format. xml_gconf: marker for running tests for xml gconf format. files: marker for running tests for calculate.utils.files module. + template_engine: marker for running tests for template_engine. diff --git a/tests/format/__init__.py b/tests/templates/__init__.py similarity index 100% rename from tests/format/__init__.py rename to tests/templates/__init__.py diff --git a/tests/templates/format/__init__.py b/tests/templates/format/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/format/test_base.py b/tests/templates/format/test_base.py similarity index 92% rename from tests/format/test_base.py rename to tests/templates/format/test_base.py index da1e6db..9555532 100644 --- a/tests/format/test_base.py +++ b/tests/templates/format/test_base.py @@ -56,7 +56,7 @@ class TestJoinMethod: @pytest.mark.base class TestLogicLinesMethod: def test_if_input_is_text_document_the_method_returns_list_of_its_lines(self): - with open('./tests/format/testfiles/logic_lines_test.txt', 'r') as InputFile: + with open('./tests/templates/format/testfiles/logic_lines_test.txt', 'r') as InputFile: InputText = InputFile.read() processingMethods = [] @@ -71,10 +71,10 @@ class TestLogicLinesMethod: def test_if_lines_in_document_divided_using_backslash_as_continuation_symbol__method_returns_list_of_full_lines(self): - with open('./tests/format/testfiles/logic_lines_test_input.txt', 'r') as InputFile: + with open('./tests/templates/format/testfiles/logic_lines_test_input.txt', 'r') as InputFile: InputText = InputFile.read() - with open('./tests/format/testfiles/logic_lines_test_output.txt', 'r') as OutputFile: + with open('./tests/templates/format/testfiles/logic_lines_test_output.txt', 'r') as OutputFile: OutputText = OutputFile.read() BaseObject = BaseFormat([]) diff --git a/tests/format/test_bind.py b/tests/templates/format/test_bind.py similarity index 97% rename from tests/format/test_bind.py rename to tests/templates/format/test_bind.py index 6779521..2bc7541 100644 --- a/tests/format/test_bind.py +++ b/tests/templates/format/test_bind.py @@ -280,19 +280,19 @@ options { assert bind_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/bind_original.conf', 'r') as original_file: + with open('./tests/templates/format/testfiles/bind_original.conf', 'r') as original_file: original_text = original_file.read() print(original_text) bind_original_object = BINDFormat(original_text) - with open('./tests/format/testfiles/bind_template.conf', 'r') as template_file: + with open('./tests/templates/format/testfiles/bind_template.conf', 'r') as template_file: template_text = template_file.read() bind_template_object = BINDFormat(template_text, ignore_comments=True) bind_original_object.join_template(bind_template_object) - with open('./tests/format/testfiles/bind_result.conf', 'r') as result_file: + with open('./tests/templates/format/testfiles/bind_result.conf', 'r') as result_file: result_text = result_file.read() assert bind_original_object.get_document_text() == result_text diff --git a/tests/format/test_compiz.py b/tests/templates/format/test_compiz.py similarity index 96% rename from tests/format/test_compiz.py rename to tests/templates/format/test_compiz.py index 7894d30..61ae855 100644 --- a/tests/format/test_compiz.py +++ b/tests/templates/format/test_compiz.py @@ -201,18 +201,18 @@ class TestParsingMethods: assert compiz_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/compiz_original', 'r') as original_file: + with open('./tests/templates/format/testfiles/compiz_original', 'r') as original_file: original_text = original_file.read() compiz_original_object = CompizFormat(original_text) - with open('./tests/format/testfiles/compiz_template', 'r') as template_file: + with open('./tests/templates/format/testfiles/compiz_template', 'r') as template_file: template_text = template_file.read() compiz_template_object = CompizFormat(template_text, ignore_comments=True) compiz_original_object.join_template(compiz_template_object) - with open('./tests/format/testfiles/compiz_result', 'r') as result_file: + with open('./tests/templates/format/testfiles/compiz_result', 'r') as result_file: result_text = result_file.read() assert compiz_original_object.get_document_text() == result_text diff --git a/tests/format/test_diff.py b/tests/templates/format/test_diff.py similarity index 94% rename from tests/format/test_diff.py rename to tests/templates/format/test_diff.py index cc3575b..87bc4f9 100644 --- a/tests/format/test_diff.py +++ b/tests/templates/format/test_diff.py @@ -9,7 +9,7 @@ import os class TestExecuteMethods: def test_if_diff_patch_used_for_patching_of_several_files__it_changes_patched_file_correctly(self): test_result = True - root_path = path.join(os.getcwd(), 'tests/format/testfiles/') + root_path = path.join(os.getcwd(), 'tests/templates/format/testfiles/') with open(path.join(root_path, 'diff_1.patch')) as patch_file: patch_text = patch_file.read() @@ -46,9 +46,9 @@ class TestExecuteMethods: def test_if_diff_patch_used_for_patching_of_directories__it_changes_files_in_directories_and_adds_ones(self): test_result = True root_path = path.join(os.getcwd(), - 'tests/format/testfiles/a1') + 'tests/templates/format/testfiles/a1') patch_path = path.join(os.getcwd(), - 'tests/format/testfiles/diff_2.patch') + 'tests/templates/format/testfiles/diff_2.patch') with open(path.join(patch_path)) as patch_file: patch_text = patch_file.read() diff --git a/tests/format/test_dovecot.py b/tests/templates/format/test_dovecot.py similarity index 96% rename from tests/format/test_dovecot.py rename to tests/templates/format/test_dovecot.py index cb709dd..2dddc65 100644 --- a/tests/format/test_dovecot.py +++ b/tests/templates/format/test_dovecot.py @@ -238,18 +238,18 @@ class TestParsingMethods: assert dovecot_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/dovecot_original.conf', 'r') as original_file: + with open('./tests/templates/format/testfiles/dovecot_original.conf', 'r') as original_file: original_text = original_file.read() dovecot_original_object = DovecotFormat(original_text) - with open('./tests/format/testfiles/dovecot_template.conf', 'r') as template_file: + with open('./tests/templates/format/testfiles/dovecot_template.conf', 'r') as template_file: template_text = template_file.read() dovecot_template_object = DovecotFormat(template_text, ignore_comments=True) dovecot_original_object.join_template(dovecot_template_object) - with open('./tests/format/testfiles/dovecot_result.conf', 'r') as result_file: + with open('./tests/templates/format/testfiles/dovecot_result.conf', 'r') as result_file: result_text = result_file.read() assert dovecot_original_object.get_document_text() == result_text diff --git a/tests/format/test_json.py b/tests/templates/format/test_json.py similarity index 82% rename from tests/format/test_json.py rename to tests/templates/format/test_json.py index ae23989..f44cf62 100644 --- a/tests/format/test_json.py +++ b/tests/templates/format/test_json.py @@ -28,17 +28,17 @@ class TestParsingMethods: assert jsonObject._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/json_original.json', 'r') as originalFile: + with open('./tests/templates/format/testfiles/json_original.json', 'r') as originalFile: originalText = originalFile.read() jsonOriginalObject = JSONFormat(originalText) - with open('./tests/format/testfiles/json_template.json', 'r') as templateFile: + with open('./tests/templates/format/testfiles/json_template.json', 'r') as templateFile: templateText = templateFile.read() jsonTemplateObject = JSONFormat(templateText) jsonOriginalObject.join_template(jsonTemplateObject) - with open('./tests/format/testfiles/json_result.json', 'r') as resultFile: + with open('./tests/templates/format/testfiles/json_result.json', 'r') as resultFile: resultText = resultFile.read() assert jsonOriginalObject.get_document_text() == resultText diff --git a/tests/format/test_kde.py b/tests/templates/format/test_kde.py similarity index 97% rename from tests/format/test_kde.py rename to tests/templates/format/test_kde.py index 7ad1eef..6de71e1 100644 --- a/tests/format/test_kde.py +++ b/tests/templates/format/test_kde.py @@ -205,18 +205,18 @@ class TestParsingMethods: assert kde_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/kde_original', 'r') as original_file: + with open('./tests/templates/format/testfiles/kde_original', 'r') as original_file: original_text = original_file.read() kde_original_object = KDEFormat(original_text) - with open('./tests/format/testfiles/kde_template', 'r') as template_file: + with open('./tests/templates/format/testfiles/kde_template', 'r') as template_file: template_text = template_file.read() kde_template_object = KDEFormat(template_text, ignore_comments=True) kde_original_object.join_template(kde_template_object) - with open('./tests/format/testfiles/kde_result', 'r') as result_file: + with open('./tests/templates/format/testfiles/kde_result', 'r') as result_file: result_text = result_file.read() assert kde_original_object.get_document_text() == result_text diff --git a/tests/format/test_kernel.py b/tests/templates/format/test_kernel.py similarity index 93% rename from tests/format/test_kernel.py rename to tests/templates/format/test_kernel.py index bab9211..e535829 100644 --- a/tests/format/test_kernel.py +++ b/tests/templates/format/test_kernel.py @@ -95,18 +95,18 @@ class TestParsingMethods: assert kernel_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/kernel_original', 'r') as original_file: + with open('./tests/templates/format/testfiles/kernel_original', 'r') as original_file: original_text = original_file.read() kernel_original_object = KernelFormat(original_text) - with open('./tests/format/testfiles/kernel_template', 'r') as template_file: + with open('./tests/templates/format/testfiles/kernel_template', 'r') as template_file: template_text = template_file.read() kernel_template_object = KernelFormat(template_text, ignore_comments=True) kernel_original_object.join_template(kernel_template_object) - with open('./tests/format/testfiles/kernel_result', 'r') as result_file: + with open('./tests/templates/format/testfiles/kernel_result', 'r') as result_file: result_text = result_file.read() assert kernel_original_object.get_document_text() == result_text diff --git a/tests/format/test_ldap.py b/tests/templates/format/test_ldap.py similarity index 97% rename from tests/format/test_ldap.py rename to tests/templates/format/test_ldap.py index 93e68d7..d33e279 100644 --- a/tests/format/test_ldap.py +++ b/tests/templates/format/test_ldap.py @@ -6,7 +6,7 @@ from calculate.templates.format.ldap_format import LDAPFormat @pytest.mark.ldap class TestParsingMethods: def test_if_logiclines_method_takes_text_with_lines_that_starts_whit_space_symbols__it_returns_joined_lines(self): - with open('./tests/format/testfiles/ldap_logic_lines_test.txt', 'r') as input_file: + with open('./tests/templates/format/testfiles/ldap_logic_lines_test.txt', 'r') as input_file: input_text = input_file.read() output_lines = ['First string of test file.', @@ -347,18 +347,18 @@ database bdb assert ldap_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/ldap_original.conf', 'r') as original_file: + with open('./tests/templates/format/testfiles/ldap_original.conf', 'r') as original_file: original_text = original_file.read() ldap_original_object = LDAPFormat(original_text) - with open('./tests/format/testfiles/ldap_template.conf', 'r') as template_file: + with open('./tests/templates/format/testfiles/ldap_template.conf', 'r') as template_file: template_text = template_file.read() ldap_template_object = LDAPFormat(template_text, ignore_comments=True) ldap_original_object.join_template(ldap_template_object) - with open('./tests/format/testfiles/ldap_result.conf', 'r') as result_file: + with open('./tests/templates/format/testfiles/ldap_result.conf', 'r') as result_file: result_text = result_file.read() assert ldap_original_object.get_document_text() == result_text diff --git a/tests/format/test_openrc.py b/tests/templates/format/test_openrc.py similarity index 95% rename from tests/format/test_openrc.py rename to tests/templates/format/test_openrc.py index 87127c1..323cbd8 100644 --- a/tests/format/test_openrc.py +++ b/tests/templates/format/test_openrc.py @@ -129,18 +129,18 @@ class TestParsingMethods: assert openrc_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/openrc_original', 'r') as original_file: + with open('./tests/templates/format/testfiles/openrc_original', 'r') as original_file: original_text = original_file.read() openrc_original_object = OpenRCFormat(original_text) - with open('./tests/format/testfiles/openrc_template', 'r') as template_file: + with open('./tests/templates/format/testfiles/openrc_template', 'r') as template_file: template_text = template_file.read() openrc_template_object = OpenRCFormat(template_text, ignore_comments=True) openrc_original_object.join_template(openrc_template_object) - with open('./tests/format/testfiles/openrc_result', 'r') as result_file: + with open('./tests/templates/format/testfiles/openrc_result', 'r') as result_file: result_text = result_file.read() assert openrc_original_object.get_document_text() == result_text diff --git a/tests/format/test_patch.py b/tests/templates/format/test_patch.py similarity index 100% rename from tests/format/test_patch.py rename to tests/templates/format/test_patch.py diff --git a/tests/format/test_postfix.py b/tests/templates/format/test_postfix.py similarity index 95% rename from tests/format/test_postfix.py rename to tests/templates/format/test_postfix.py index 4e67d51..ce106f9 100644 --- a/tests/format/test_postfix.py +++ b/tests/templates/format/test_postfix.py @@ -107,18 +107,18 @@ class TestParsingMethods: assert postfix_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/postfix_original', 'r') as original_file: + with open('./tests/templates/format/testfiles/postfix_original', 'r') as original_file: original_text = original_file.read() postfix_original_object = PostfixFormat(original_text) - with open('./tests/format/testfiles/postfix_template', 'r') as template_file: + with open('./tests/templates/format/testfiles/postfix_template', 'r') as template_file: template_text = template_file.read() postfix_template_object = PostfixFormat(template_text, ignore_comments=True) postfix_original_object.join_template(postfix_template_object) - with open('./tests/format/testfiles/postfix_result', 'r') as result_file: + with open('./tests/templates/format/testfiles/postfix_result', 'r') as result_file: result_text = result_file.read() assert postfix_original_object.get_document_text() == result_text diff --git a/tests/format/test_procmail.py b/tests/templates/format/test_procmail.py similarity index 94% rename from tests/format/test_procmail.py rename to tests/templates/format/test_procmail.py index 576d8d4..852f816 100644 --- a/tests/format/test_procmail.py +++ b/tests/templates/format/test_procmail.py @@ -113,18 +113,18 @@ class TestParsingMethods: assert procmail_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/procmail_original', 'r') as original_file: + with open('./tests/templates/format/testfiles/procmail_original', 'r') as original_file: original_text = original_file.read() procmail_original_object = ProcmailFormat(original_text) - with open('./tests/format/testfiles/procmail_template', 'r') as template_file: + with open('./tests/templates/format/testfiles/procmail_template', 'r') as template_file: template_text = template_file.read() procmail_template_object = ProcmailFormat(template_text, ignore_comments=True) procmail_original_object.join_template(procmail_template_object) - with open('./tests/format/testfiles/procmail_result', 'r') as result_file: + with open('./tests/templates/format/testfiles/procmail_result', 'r') as result_file: result_text = result_file.read() assert procmail_original_object.get_document_text() == result_text diff --git a/tests/format/test_proftpd.py b/tests/templates/format/test_proftpd.py similarity index 96% rename from tests/format/test_proftpd.py rename to tests/templates/format/test_proftpd.py index 1736492..3aaf547 100644 --- a/tests/format/test_proftpd.py +++ b/tests/templates/format/test_proftpd.py @@ -220,18 +220,18 @@ class TestParsingMethods: assert proftpd_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/proftpd_original.conf', 'r') as original_file: + with open('./tests/templates/format/testfiles/proftpd_original.conf', 'r') as original_file: original_text = original_file.read() proftpd_original_object = ProFTPDFormat(original_text) - with open('./tests/format/testfiles/proftpd_template.conf', 'r') as template_file: + with open('./tests/templates/format/testfiles/proftpd_template.conf', 'r') as template_file: template_text = template_file.read() proftpd_template_object = ProFTPDFormat(template_text, ignore_comments=True) proftpd_original_object.join_template(proftpd_template_object) - with open('./tests/format/testfiles/proftpd_result.conf', 'r') as result_file: + with open('./tests/templates/format/testfiles/proftpd_result.conf', 'r') as result_file: result_text = result_file.read() assert proftpd_original_object.get_document_text() == result_text diff --git a/tests/format/test_samba.py b/tests/templates/format/test_samba.py similarity index 97% rename from tests/format/test_samba.py rename to tests/templates/format/test_samba.py index 8e02b28..ff3990f 100644 --- a/tests/format/test_samba.py +++ b/tests/templates/format/test_samba.py @@ -212,18 +212,18 @@ class TestParsingMethods: assert samba_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/samba_original', 'r') as original_file: + with open('./tests/templates/format/testfiles/samba_original', 'r') as original_file: original_text = original_file.read() samba_original_object = SambaFormat(original_text) - with open('./tests/format/testfiles/samba_template', 'r') as template_file: + with open('./tests/templates/format/testfiles/samba_template', 'r') as template_file: template_text = template_file.read() samba_template_object = SambaFormat(template_text, ignore_comments=True) samba_original_object.join_template(samba_template_object) - with open('./tests/format/testfiles/samba_result', 'r') as result_file: + with open('./tests/templates/format/testfiles/samba_result', 'r') as result_file: result_text = result_file.read() assert samba_original_object.get_document_text() == result_text diff --git a/tests/format/test_xml_gconf.py b/tests/templates/format/test_xml_gconf.py similarity index 93% rename from tests/format/test_xml_gconf.py rename to tests/templates/format/test_xml_gconf.py index 402f9f7..04b6d29 100644 --- a/tests/format/test_xml_gconf.py +++ b/tests/templates/format/test_xml_gconf.py @@ -73,17 +73,17 @@ class TestParsingMethods: assert xml_gconf_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/xml_gconf_original.xml', 'r') as original_file: + with open('./tests/templates/format/testfiles/xml_gconf_original.xml', 'r') as original_file: original_text = original_file.read() xml_gconf_original_object = XMLGConfFormat(original_text) - with open('./tests/format/testfiles/xml_gconf_template.xml', 'r') as template_file: + with open('./tests/templates/format/testfiles/xml_gconf_template.xml', 'r') as template_file: template_text = template_file.read() xml_gconf_template_object = XMLGConfFormat(template_text) xml_gconf_original_object.join_template(xml_gconf_template_object) - with open('./tests/format/testfiles/xml_gconf_result.xml', 'r') as result_file: + with open('./tests/templates/format/testfiles/xml_gconf_result.xml', 'r') as result_file: result_text = result_file.read() assert xml_gconf_original_object.get_document_text() == result_text diff --git a/tests/format/test_xml_xfce.py b/tests/templates/format/test_xml_xfce.py similarity index 88% rename from tests/format/test_xml_xfce.py rename to tests/templates/format/test_xml_xfce.py index 49214da..ce472fe 100644 --- a/tests/format/test_xml_xfce.py +++ b/tests/templates/format/test_xml_xfce.py @@ -36,17 +36,17 @@ class TestParsingMethods: assert xml_xfce_object._document_dictionary == result def test_joining_documents_1(self): - with open('./tests/format/testfiles/xml_xfce_original.xml', 'r') as original_file: + with open('./tests/templates/format/testfiles/xml_xfce_original.xml', 'r') as original_file: original_text = original_file.read() xml_xfce_original_object = XMLXfceFormat(original_text) - with open('./tests/format/testfiles/xml_xfce_template.xml', 'r') as template_file: + with open('./tests/templates/format/testfiles/xml_xfce_template.xml', 'r') as template_file: template_text = template_file.read() xml_xfce_template_object = XMLXfceFormat(template_text) xml_xfce_original_object.join_template(xml_xfce_template_object) - with open('./tests/format/testfiles/xml_xfce_result.xml', 'r') as result_file: + with open('./tests/templates/format/testfiles/xml_xfce_result.xml', 'r') as result_file: result_text = result_file.read() assert xml_xfce_original_object.get_document_text() == result_text diff --git a/tests/format/testfiles/a/dir/file1.txt b/tests/templates/format/testfiles/a/dir/file1.txt similarity index 100% rename from tests/format/testfiles/a/dir/file1.txt rename to tests/templates/format/testfiles/a/dir/file1.txt diff --git a/tests/format/testfiles/a/dir/file2.txt b/tests/templates/format/testfiles/a/dir/file2.txt similarity index 100% rename from tests/format/testfiles/a/dir/file2.txt rename to tests/templates/format/testfiles/a/dir/file2.txt diff --git a/tests/format/testfiles/a1/dir/file1.txt b/tests/templates/format/testfiles/a1/dir/file1.txt similarity index 100% rename from tests/format/testfiles/a1/dir/file1.txt rename to tests/templates/format/testfiles/a1/dir/file1.txt diff --git a/tests/format/testfiles/b/dir/file1.txt b/tests/templates/format/testfiles/b/dir/file1.txt similarity index 100% rename from tests/format/testfiles/b/dir/file1.txt rename to tests/templates/format/testfiles/b/dir/file1.txt diff --git a/tests/format/testfiles/b/dir/file2.txt b/tests/templates/format/testfiles/b/dir/file2.txt similarity index 100% rename from tests/format/testfiles/b/dir/file2.txt rename to tests/templates/format/testfiles/b/dir/file2.txt diff --git a/tests/format/testfiles/b1/dir/file1.txt b/tests/templates/format/testfiles/b1/dir/file1.txt similarity index 100% rename from tests/format/testfiles/b1/dir/file1.txt rename to tests/templates/format/testfiles/b1/dir/file1.txt diff --git a/tests/format/testfiles/b1/dir/file2.txt b/tests/templates/format/testfiles/b1/dir/file2.txt similarity index 100% rename from tests/format/testfiles/b1/dir/file2.txt rename to tests/templates/format/testfiles/b1/dir/file2.txt diff --git a/tests/format/testfiles/bind_original.conf b/tests/templates/format/testfiles/bind_original.conf similarity index 100% rename from tests/format/testfiles/bind_original.conf rename to tests/templates/format/testfiles/bind_original.conf diff --git a/tests/format/testfiles/bind_result.conf b/tests/templates/format/testfiles/bind_result.conf similarity index 100% rename from tests/format/testfiles/bind_result.conf rename to tests/templates/format/testfiles/bind_result.conf diff --git a/tests/format/testfiles/bind_template.conf b/tests/templates/format/testfiles/bind_template.conf similarity index 100% rename from tests/format/testfiles/bind_template.conf rename to tests/templates/format/testfiles/bind_template.conf diff --git a/tests/format/testfiles/compiz_original b/tests/templates/format/testfiles/compiz_original similarity index 100% rename from tests/format/testfiles/compiz_original rename to tests/templates/format/testfiles/compiz_original diff --git a/tests/format/testfiles/compiz_result b/tests/templates/format/testfiles/compiz_result similarity index 100% rename from tests/format/testfiles/compiz_result rename to tests/templates/format/testfiles/compiz_result diff --git a/tests/format/testfiles/compiz_template b/tests/templates/format/testfiles/compiz_template similarity index 100% rename from tests/format/testfiles/compiz_template rename to tests/templates/format/testfiles/compiz_template diff --git a/tests/format/testfiles/diff_1.patch b/tests/templates/format/testfiles/diff_1.patch similarity index 100% rename from tests/format/testfiles/diff_1.patch rename to tests/templates/format/testfiles/diff_1.patch diff --git a/tests/format/testfiles/diff_2.patch b/tests/templates/format/testfiles/diff_2.patch similarity index 100% rename from tests/format/testfiles/diff_2.patch rename to tests/templates/format/testfiles/diff_2.patch diff --git a/tests/format/testfiles/dovecot_original.conf b/tests/templates/format/testfiles/dovecot_original.conf similarity index 100% rename from tests/format/testfiles/dovecot_original.conf rename to tests/templates/format/testfiles/dovecot_original.conf diff --git a/tests/format/testfiles/dovecot_result.conf b/tests/templates/format/testfiles/dovecot_result.conf similarity index 100% rename from tests/format/testfiles/dovecot_result.conf rename to tests/templates/format/testfiles/dovecot_result.conf diff --git a/tests/format/testfiles/dovecot_template.conf b/tests/templates/format/testfiles/dovecot_template.conf similarity index 100% rename from tests/format/testfiles/dovecot_template.conf rename to tests/templates/format/testfiles/dovecot_template.conf diff --git a/tests/format/testfiles/json_original.json b/tests/templates/format/testfiles/json_original.json similarity index 100% rename from tests/format/testfiles/json_original.json rename to tests/templates/format/testfiles/json_original.json diff --git a/tests/format/testfiles/json_result.json b/tests/templates/format/testfiles/json_result.json similarity index 100% rename from tests/format/testfiles/json_result.json rename to tests/templates/format/testfiles/json_result.json diff --git a/tests/format/testfiles/json_template.json b/tests/templates/format/testfiles/json_template.json similarity index 100% rename from tests/format/testfiles/json_template.json rename to tests/templates/format/testfiles/json_template.json diff --git a/tests/format/testfiles/kde_original b/tests/templates/format/testfiles/kde_original similarity index 100% rename from tests/format/testfiles/kde_original rename to tests/templates/format/testfiles/kde_original diff --git a/tests/format/testfiles/kde_result b/tests/templates/format/testfiles/kde_result similarity index 100% rename from tests/format/testfiles/kde_result rename to tests/templates/format/testfiles/kde_result diff --git a/tests/format/testfiles/kde_template b/tests/templates/format/testfiles/kde_template similarity index 100% rename from tests/format/testfiles/kde_template rename to tests/templates/format/testfiles/kde_template diff --git a/tests/format/testfiles/kernel_original b/tests/templates/format/testfiles/kernel_original similarity index 100% rename from tests/format/testfiles/kernel_original rename to tests/templates/format/testfiles/kernel_original diff --git a/tests/format/testfiles/kernel_result b/tests/templates/format/testfiles/kernel_result similarity index 100% rename from tests/format/testfiles/kernel_result rename to tests/templates/format/testfiles/kernel_result diff --git a/tests/format/testfiles/kernel_template b/tests/templates/format/testfiles/kernel_template similarity index 100% rename from tests/format/testfiles/kernel_template rename to tests/templates/format/testfiles/kernel_template diff --git a/tests/format/testfiles/ldap_logic_lines_test.txt b/tests/templates/format/testfiles/ldap_logic_lines_test.txt similarity index 100% rename from tests/format/testfiles/ldap_logic_lines_test.txt rename to tests/templates/format/testfiles/ldap_logic_lines_test.txt diff --git a/tests/format/testfiles/ldap_original.conf b/tests/templates/format/testfiles/ldap_original.conf similarity index 100% rename from tests/format/testfiles/ldap_original.conf rename to tests/templates/format/testfiles/ldap_original.conf diff --git a/tests/format/testfiles/ldap_result.conf b/tests/templates/format/testfiles/ldap_result.conf similarity index 100% rename from tests/format/testfiles/ldap_result.conf rename to tests/templates/format/testfiles/ldap_result.conf diff --git a/tests/format/testfiles/ldap_template.conf b/tests/templates/format/testfiles/ldap_template.conf similarity index 100% rename from tests/format/testfiles/ldap_template.conf rename to tests/templates/format/testfiles/ldap_template.conf diff --git a/tests/format/testfiles/logic_lines_test.txt b/tests/templates/format/testfiles/logic_lines_test.txt similarity index 100% rename from tests/format/testfiles/logic_lines_test.txt rename to tests/templates/format/testfiles/logic_lines_test.txt diff --git a/tests/format/testfiles/logic_lines_test_input.txt b/tests/templates/format/testfiles/logic_lines_test_input.txt similarity index 100% rename from tests/format/testfiles/logic_lines_test_input.txt rename to tests/templates/format/testfiles/logic_lines_test_input.txt diff --git a/tests/format/testfiles/logic_lines_test_output.txt b/tests/templates/format/testfiles/logic_lines_test_output.txt similarity index 100% rename from tests/format/testfiles/logic_lines_test_output.txt rename to tests/templates/format/testfiles/logic_lines_test_output.txt diff --git a/tests/format/testfiles/openrc_original b/tests/templates/format/testfiles/openrc_original similarity index 100% rename from tests/format/testfiles/openrc_original rename to tests/templates/format/testfiles/openrc_original diff --git a/tests/format/testfiles/openrc_result b/tests/templates/format/testfiles/openrc_result similarity index 100% rename from tests/format/testfiles/openrc_result rename to tests/templates/format/testfiles/openrc_result diff --git a/tests/format/testfiles/openrc_template b/tests/templates/format/testfiles/openrc_template similarity index 100% rename from tests/format/testfiles/openrc_template rename to tests/templates/format/testfiles/openrc_template diff --git a/tests/format/testfiles/postfix_original b/tests/templates/format/testfiles/postfix_original similarity index 100% rename from tests/format/testfiles/postfix_original rename to tests/templates/format/testfiles/postfix_original diff --git a/tests/format/testfiles/postfix_result b/tests/templates/format/testfiles/postfix_result similarity index 100% rename from tests/format/testfiles/postfix_result rename to tests/templates/format/testfiles/postfix_result diff --git a/tests/format/testfiles/postfix_template b/tests/templates/format/testfiles/postfix_template similarity index 100% rename from tests/format/testfiles/postfix_template rename to tests/templates/format/testfiles/postfix_template diff --git a/tests/format/testfiles/procmail_original b/tests/templates/format/testfiles/procmail_original similarity index 100% rename from tests/format/testfiles/procmail_original rename to tests/templates/format/testfiles/procmail_original diff --git a/tests/format/testfiles/procmail_result b/tests/templates/format/testfiles/procmail_result similarity index 100% rename from tests/format/testfiles/procmail_result rename to tests/templates/format/testfiles/procmail_result diff --git a/tests/format/testfiles/procmail_template b/tests/templates/format/testfiles/procmail_template similarity index 100% rename from tests/format/testfiles/procmail_template rename to tests/templates/format/testfiles/procmail_template diff --git a/tests/format/testfiles/proftpd_original.conf b/tests/templates/format/testfiles/proftpd_original.conf similarity index 100% rename from tests/format/testfiles/proftpd_original.conf rename to tests/templates/format/testfiles/proftpd_original.conf diff --git a/tests/format/testfiles/proftpd_result.conf b/tests/templates/format/testfiles/proftpd_result.conf similarity index 100% rename from tests/format/testfiles/proftpd_result.conf rename to tests/templates/format/testfiles/proftpd_result.conf diff --git a/tests/format/testfiles/proftpd_template.conf b/tests/templates/format/testfiles/proftpd_template.conf similarity index 100% rename from tests/format/testfiles/proftpd_template.conf rename to tests/templates/format/testfiles/proftpd_template.conf diff --git a/tests/format/testfiles/samba_original b/tests/templates/format/testfiles/samba_original similarity index 100% rename from tests/format/testfiles/samba_original rename to tests/templates/format/testfiles/samba_original diff --git a/tests/format/testfiles/samba_result b/tests/templates/format/testfiles/samba_result similarity index 100% rename from tests/format/testfiles/samba_result rename to tests/templates/format/testfiles/samba_result diff --git a/tests/format/testfiles/samba_template b/tests/templates/format/testfiles/samba_template similarity index 100% rename from tests/format/testfiles/samba_template rename to tests/templates/format/testfiles/samba_template diff --git a/tests/format/testfiles/xml_gconf_original.xml b/tests/templates/format/testfiles/xml_gconf_original.xml similarity index 100% rename from tests/format/testfiles/xml_gconf_original.xml rename to tests/templates/format/testfiles/xml_gconf_original.xml diff --git a/tests/format/testfiles/xml_gconf_result.xml b/tests/templates/format/testfiles/xml_gconf_result.xml similarity index 100% rename from tests/format/testfiles/xml_gconf_result.xml rename to tests/templates/format/testfiles/xml_gconf_result.xml diff --git a/tests/format/testfiles/xml_gconf_template.xml b/tests/templates/format/testfiles/xml_gconf_template.xml similarity index 100% rename from tests/format/testfiles/xml_gconf_template.xml rename to tests/templates/format/testfiles/xml_gconf_template.xml diff --git a/tests/format/testfiles/xml_xfce_original.xml b/tests/templates/format/testfiles/xml_xfce_original.xml similarity index 100% rename from tests/format/testfiles/xml_xfce_original.xml rename to tests/templates/format/testfiles/xml_xfce_original.xml diff --git a/tests/format/testfiles/xml_xfce_result.xml b/tests/templates/format/testfiles/xml_xfce_result.xml similarity index 100% rename from tests/format/testfiles/xml_xfce_result.xml rename to tests/templates/format/testfiles/xml_xfce_result.xml diff --git a/tests/format/testfiles/xml_xfce_template.xml b/tests/templates/format/testfiles/xml_xfce_template.xml similarity index 100% rename from tests/format/testfiles/xml_xfce_template.xml rename to tests/templates/format/testfiles/xml_xfce_template.xml diff --git a/tests/templates/test_template_engine.py b/tests/templates/test_template_engine.py new file mode 100644 index 0000000..945e4a2 --- /dev/null +++ b/tests/templates/test_template_engine.py @@ -0,0 +1,120 @@ +import pytest +from calculate.templates.template_engine import TemplateEngine, Variables + + +PARAMETERS_SET = {'name', 'path', 'append', 'chmod', 'chown', + 'autoupdate', 'env', 'link', 'force', 'symbolic', + 'format', 'comment', 'protected', 'mirror', 'run', + 'exec', 'env', 'stretch', 'convert', 'dotall', + 'multiline', 'dconf', 'package', 'merge', + 'postmerge', 'action'} + + +@pytest.mark.template_engine +class TestUtils(): + def test_if_an_input_template_contains_calculate_tag_with_some_parameters__the_template_engine_object_will_collect_its_parameters(self): + input_template = '''{% calculate name = 'filename', path = '/etc/path', force %}''' + parameters = {'name': 'filename', 'path': '/etc/path', 'force': True} + template_engine = TemplateEngine(parameters_set=PARAMETERS_SET) + template_engine.process_template_from_string(input_template) + output_parameters = template_engine.parameters + assert output_parameters == parameters + + def test_if_an_input_template_binded_with_datavars_module__variables_available_in_a_template(self): + input_template = '''{% calculate name = vars.var_1, path = vars.var_2, autoupdate %}''' + parameters = {'name': 'filename', 'path': '/etc/path', + 'autoupdate': True} + datavars_module = Variables({'vars': + Variables({'var_1': 'filename', + 'var_2': '/etc/path'})}) + template_engine = TemplateEngine(parameters_set=PARAMETERS_SET, + datavars_module=datavars_module) + template_engine.process_template_from_string(input_template) + output_parameters = template_engine.parameters + assert output_parameters == parameters + + def test_if_an_input_template_contains_env_parameter_in_which_module_name_is_assigned__the_variables_from_this_module_can_be_used_in_template_without_determining_of_their_module(self): + input_template = '''{% calculate name = vars.var_1, path = var_3, env = 'other_vars' %}''' + parameters = {'name': 'filename', 'path': '/etc/other_path', + 'env': 'other_vars'} + datavars_module = Variables({'vars': + Variables({'var_1': 'filename', + 'var_2': '/etc/path'}), + 'other_vars': + Variables({'var_3': '/etc/other_path'})}) + template_engine = TemplateEngine(parameters_set=PARAMETERS_SET, + datavars_module=datavars_module) + template_engine.process_template_from_string(input_template) + output_parameters = template_engine.parameters + assert output_parameters == parameters + + def test_if_an_input_template_contains_condition__the_template_engine_object_will_contain_the_value_of_its_condition(self): + input_template = '''{% calculate vars.var_1 < vars.var_2 or (not (var_3 == 'required status') and vars.var_4), env = 'vars' %}''' + datavars_module = Variables({'vars': + Variables({'var_1': 12, + 'var_2': 1.2, + 'var_3': 'unrequired status', + 'var_4': True})}) + template_engine = TemplateEngine(parameters_set=PARAMETERS_SET, + datavars_module=datavars_module) + template_engine.process_template_from_string(input_template) + condition = template_engine.condition + assert condition is True + + def test_if_an_input_template_contains_several_calculate_tags__the_template_engine_will_parse_them_all_and_will_contain_all_parameters_and_result_of_all_conditions(self): + input_template = '''{% calculate name = vars.var_1, var_4 < var_5 %} + {% calculate path = var_3, var_6 == 'value' %} + {% calculate env = 'other_vars'%}''' + parameters = {'name': 'filename', 'path': '/etc/other_path', + 'env': 'other_vars'} + datavars_module = Variables({'vars': + Variables({'var_1': 'filename', + 'var_2': '/etc/path'}), + 'other_vars': + Variables({'var_3': '/etc/other_path', + 'var_4': 12, 'var_5': 1.2, + 'var_6': 'value'})}) + template_engine = TemplateEngine(parameters_set=PARAMETERS_SET, + datavars_module=datavars_module) + template_engine.process_template_from_string(input_template) + condition = template_engine.condition + output_parameters = template_engine.parameters + assert condition is False and output_parameters == parameters + + def test_if_an_input_template_contains_variables_in_its_text__the_rendered_text_will_contain_values_of_this_variables(self): + input_template = '''{% calculate name = 'filename', force -%} + parameter_1 = {{ vars_2.var_3 }} + {% calculate env = 'vars_1' -%} + parameter_2 = {{ vars_2.var_4 }} + parameter_3 = {{ var_1 }}''' + output_text = '''parameter_1 = value_1 + parameter_2 = value_2 + parameter_3 = 12''' + datavars_module = Variables({'vars_1': + Variables({'var_1': 12, + 'var_2': 1.2}), + 'vars_2': + Variables({'var_3': 'value_1', + 'var_4': 'value_2'})}) + template_engine = TemplateEngine(parameters_set=PARAMETERS_SET, + datavars_module=datavars_module) + template_engine.process_template_from_string(input_template) + text = template_engine.template_text + assert text == output_text + + def test_if_value_of_variable_is_set_using_save_tag__the_new_value_of_variable_can_be_used_in_template(self): + input_template = '''{% calculate name = 'filename', force -%} + {% save vars_1.var_3 = 13 -%} + parameter = {{ vars_1.var_3 - 1 + vars_1.var_2 * 2 }} + {% save.session vars_1.var_4 = True -%} + bool parameter = {{ vars_1.var_4 }}''' + output_text = '''parameter = 14.4 + bool parameter = True''' + datavars_module = Variables({'vars_1': + Variables({'var_1': 12, + 'var_2': 1.2})}) + template_engine = TemplateEngine(parameters_set=PARAMETERS_SET, + datavars_module=datavars_module) + template_engine.process_template_from_string(input_template) + text = template_engine.template_text + assert text == output_text