Browse Source

Variables representation is fixed #25

master
Иванов Денис 2 years ago
parent
commit
103315f32d
  1. 13
      calculate/templates/template_engine.py
  2. 5
      calculate/templates/template_processor.py
  3. 2
      calculate/utils/package.py
  4. 19
      tests/templates/test_template_engine.py
  5. 34
      tests/templates/test_template_executor.py
  6. BIN
      tests/templates/testfiles/test_executor_root/etc.backup/append_replace_file_testfiles/logo.png
  7. 6
      tests/utils/test_package.py

13
calculate/templates/template_engine.py

@ -628,6 +628,7 @@ class ParametersProcessor:
try:
result = self.package_atom_parser.parse_package_parameter(
package_atom)
print("")
return result
except PackageAtomError as error:
if error.errno != NOTEXIST:
@ -962,9 +963,10 @@ class CalculateExtension(Extension):
ASSIGN, APPEND, REMOVE = range(3)
def __init__(self, environment, parameters_processor: ParametersProcessor,
datavars_module=Variables()):
datavars_module=Variables(), chroot_path="/"):
super().__init__(environment)
self.environment = environment
self.package_atom_parser = PackageAtomParser(chroot_path=chroot_path)
self.environment.globals.update({'pkg': self.pkg})
@ -1454,15 +1456,13 @@ class CalculateExtension(Extension):
аргументов нет, или версию пакета в аргументе функции. Если аргументов
нет, а шаблон не привязан к какому-либо пакету, или если указанного в
аргументе пакета нет -- функция возвращает пустой объект Version().'''
package_atom_parser = PackageAtomParser()
if args:
package_atom = args[0]
try:
atom_name = package_atom_parser.parse_package_parameter(
atom_name = self.package_atom_parser.parse_package_parameter(
package_atom)
return atom_name.version
except PackageAtomError:
except PackageAtomError as error:
return Version()
else:
# package = context.parent['__parameters__'].package
@ -1505,7 +1505,8 @@ class TemplateEngine:
self.calculate_extension = CalculateExtension(
self.environment,
self.parameters_processor,
datavars_module=datavars_module)
datavars_module=datavars_module,
chroot_path=chroot_path)
self.environment.add_extension(self.calculate_extension)
self.environment.context_class = CalculateContext

5
calculate/templates/template_processor.py

@ -258,9 +258,8 @@ class TemplateWrapper:
if self.parameters.source is True and self.parameters.mirror:
self.remove_original = True
else:
if self.parameters.mirror:
raise TemplateExecutorError("target file does not exist, while"
" 'mirror' parameter is set")
if self.parameters.append and self.parameters.append == "replace":
self.remove_original = True
self.target_type = None
if self.format_class is not None and self.format_class.EXECUTABLE:

2
calculate/utils/package.py

@ -72,7 +72,7 @@ class Version:
"Can't initialize Version object using '{0}'"
" value with type {1}".format(version_value,
type(version_value)))
self._string = value['string']
self._string = value['string'].strip('-')
self._value = value['value']
self._literal = value['literal']
self._suffix = value['suffix']

19
tests/templates/test_template_engine.py

@ -143,13 +143,14 @@ class TestTemplateEngine():
def test_if_an_input_template_contains_pkg_function_with_existing_package_as_its_argument__it_works_correctly_and_pkg_function_returns_version_value(self):
input_template = '''{% calculate name = 'filename', force -%}
{% if pkg('test-category/test-package') < 2.7 -%}
{% if pkg('test-category/test-package') < 2.3 -%}
pkg() works correctly.
{%- else -%}
pkg() does not work correctly.
{%- endif -%}'''
output_text = 'pkg() works correctly.'
template_engine = TemplateEngine(appends_set=APPENDS_SET)
template_engine = TemplateEngine(appends_set=APPENDS_SET,
chroot_path=CHROOT_PATH)
template_engine.process_template_from_string(input_template, FILE)
text = template_engine.template_text
@ -163,7 +164,8 @@ class TestTemplateEngine():
pkg() does not work correctly.
{%- endif -%}'''
output_text = 'pkg() works correctly.'
template_engine = TemplateEngine(appends_set=APPENDS_SET)
template_engine = TemplateEngine(appends_set=APPENDS_SET,
chroot_path=CHROOT_PATH)
template_engine.process_template_from_string(input_template, FILE)
text = template_engine.template_text
@ -226,11 +228,13 @@ parameter_2 = {{ vars_1.var_1 }}'''
Variables({'var_1': 'second'})})
template_engine_1 = TemplateEngine(appends_set=APPENDS_SET,
datavars_module=datavars_module_1)
datavars_module=datavars_module_1,
chroot_path=CHROOT_PATH)
template_engine_1.process_template_from_string(input_template_1, DIR)
template_engine_2 = TemplateEngine(appends_set=APPENDS_SET,
datavars_module=datavars_module_2)
datavars_module=datavars_module_2,
chroot_path=CHROOT_PATH)
template_engine_2.process_template_from_string(input_template_2, DIR)
text_1 = template_engine_1.template_text
@ -245,14 +249,15 @@ parameter_2 = {{ vars_1.var_1 }}'''
'test-category/test-package',
'var_2': 1.2})})
input_template = '''{% calculate name = 'filename', force -%}
{% if pkg(vars_1.var_1) < 2.7 -%}
{% if pkg(vars_1.var_1) < 4.1 -%}
pkg() works correctly.
{%- else -%}
pkg() does not work correctly.
{%- endif -%}'''
output_text = 'pkg() works correctly.'
template_engine = TemplateEngine(appends_set=APPENDS_SET,
datavars_module=datavars_module)
datavars_module=datavars_module,
chroot_path=CHROOT_PATH)
template_engine.process_template_from_string(input_template, FILE)
text = template_engine.template_text

34
tests/templates/test_template_executor.py

@ -2977,9 +2977,6 @@ AttributeError: module 'os' has no attribute 'suspicious_attribute'
counter += 1
def test_using_mirror_for_coping_files(self):
pass
def test_raw_format_error(self):
target_path = join_paths(
CHROOT_PATH,
'/etc/append_replace_file_testfiles/file_2.png')
@ -2988,28 +2985,21 @@ AttributeError: module 'os' has no attribute 'suspicious_attribute'
'/etc/append_replace_file_testfiles/logo.png')
parameters_object = ParametersContainer({'package': test_package_name,
'append': 'join',
'append': 'replace',
'format': 'raw',
'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)
# template_executor._append_join_file(template_wrapper)
# assert os.path.exists(target_path)
# assert os.path.exists(join_paths(
# CONFIG_ARCHIVE_PATH,
# '/etc/append_join_file_testfiles/file_13'))
# assert '/etc/append_join_file_testfiles/file_13'\
# not in template_wrapper.target_package
# with open(template_wrapper.output_path, 'r') as output_file:
# output_file_text = output_file.read()
# assert output_file_text == output_text
template_wrapper = TemplateWrapper(
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)
def test_raw_format_error(self):
pass
def test_to_remove_changed_testfiles(self):
shutil.rmtree(os.path.join(CHROOT_PATH, 'etc'))

BIN
tests/templates/testfiles/test_executor_root/etc.backup/append_replace_file_testfiles/logo.png

After

Width: 240  |  Height: 240  |  Size: 11 KiB

6
tests/utils/test_package.py

@ -43,7 +43,7 @@ class TestContents:
assert version_1 > version_2
def test_if_two_Version_objects_compared_using_gt_operation_both_of_them_have_p_suffix_and_the_right_version_value_is_less_than_left_version__the_result_of_the_comparing_would_be_True(self):
version_1 = Version('2.5.6_p2-r1')
version_1 = Version('-2.5.6_p2-r1')
version_2 = Version('2.5.6_p1-r1')
assert version_1 > version_2
@ -76,6 +76,10 @@ class TestContents:
version = Version('4.1')
assert not version >> ('5.1.2', '6.1.12')
def test_version_representation(self):
version_1 = Version('-2.5.6_p2-r1')
assert str(version_1) == '2.5.6_p2-r1'
def test_if_ContentsParser_is_used_for_parsing_of_a_correct_contents_file_text_and_then_is_used_to_get_text_of_the_source_text__the_source_text_and_the_parser_s_output_will_be_the_same(self):
parser = ContentsParser()
contents_text = '''dir /usr

Loading…
Cancel
Save