diff --git a/calculate/templates/template_processor.py b/calculate/templates/template_processor.py index ace9ee4..e8289ce 100644 --- a/calculate/templates/template_processor.py +++ b/calculate/templates/template_processor.py @@ -1017,7 +1017,11 @@ class TemplateExecutor: # Тогда также обновляем архив. output_paths.append(template_object.archive_path) - if template_object.target_type is not None and not replace: + # TODO Сделать это как-нибудь получше + if replace: + input_text = '' + elif (template_object.target_type is not None + or template_object.parameters.source): # Если целевой файл есть и нет параметра replace -- # используем текст целевого файла. if (not input_path.startswith(self.cl_config_archive_path) diff --git a/tests/templates/test_template_executor.py b/tests/templates/test_template_executor.py index 1869f8f..c4facf3 100644 --- a/tests/templates/test_template_executor.py +++ b/tests/templates/test_template_executor.py @@ -2279,6 +2279,11 @@ class TestTemplateExecutor: chroot_path=CHROOT_PATH, config_archive_path=CONFIG_ARCHIVE_PATH) template_executor._append_replace_file(template_wrapper) + # template_executor.execute_template(target_path, + # parameters_object, + # FILE, + # '/path/to/template', + # save_changes=False) assert os.path.exists(target_path) assert '/etc/append_after_file_testfiles/file_0'\ @@ -2991,14 +2996,51 @@ AttributeError: module 'os' has no attribute 'suspicious_attribute' 'mirror': True}) template_wrapper = TemplateWrapper( - target_path, - parameters_object, FILE, - '/path/to/template', - template_text='', - chroot_path=CHROOT_PATH, - config_archive_path=CONFIG_ARCHIVE_PATH) + target_path, + parameters_object, FILE, + '/path/to/template', + template_text='', + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) + template_executor._append_join_file(template_wrapper) + assert os.path.exists(target_path) + + def test_using_regex_template_with_source(self): + target_path = join_paths( + CHROOT_PATH, + '/etc/append_join_file_testfiles/file_18') + source_path = join_paths( + CHROOT_PATH, + '/etc/append_join_file_testfiles/file_17') + template = '''root +myroot''' + expected_output = ('#' + '-' * 79 + '\n' + + '# Modified by Calculate Utilities 4.0\n' + + '# Processed template files:\n' + + f'# /path/to/template\n' + + '#' + '-' * 79 +'\n' + + "myroot:x:0:0:myroot:/myroot:/bin/bash\n" + + "bin:x:1:1:bin:/bin:/bin/false\n" + + "daemon:x:2:2:daemon:/sbin:/bin/false\n" + + "adm:x:3:4:adm:/var/adm:/bin/false\n") + + parameters_object = ParametersContainer({'package': test_package_name, + 'append': 'join', + 'source': source_path, + 'format': 'regex'}) + + template_wrapper = TemplateWrapper( + target_path, + parameters_object, FILE, + '/path/to/template', + template_text=template, + chroot_path=CHROOT_PATH, + config_archive_path=CONFIG_ARCHIVE_PATH) template_executor._append_join_file(template_wrapper) assert os.path.exists(target_path) + with open(target_path, 'r') as out_file: + out_text = out_file.read() + assert out_text == expected_output def test_to_remove_changed_testfiles(self): shutil.rmtree(os.path.join(CHROOT_PATH, 'etc')) diff --git a/tests/templates/testfiles/test_executor_root/etc.backup/append_join_file_testfiles/file_17 b/tests/templates/testfiles/test_executor_root/etc.backup/append_join_file_testfiles/file_17 new file mode 100644 index 0000000..c4c01fe --- /dev/null +++ b/tests/templates/testfiles/test_executor_root/etc.backup/append_join_file_testfiles/file_17 @@ -0,0 +1,4 @@ +root:x:0:0:root:/root:/bin/bash +bin:x:1:1:bin:/bin:/bin/false +daemon:x:2:2:daemon:/sbin:/bin/false +adm:x:3:4:adm:/var/adm:/bin/false