def test_loop(examples_dir): with open('{examples_dir}/loop.yml'.format(examples_dir=examples_dir), 'r') as inf: template = Template.parse(inf, 'yaml') ctx = Context(LOOP_TEST_CONTEXT) output = template.render(ctx) assert (output.strip() == ''' domains: - apiVersion: v1 kind: DomainName metadata: index: 0 name: hernekeit.to - apiVersion: v1 kind: DomainName metadata: index: 1 name: vii.na - apiVersion: v1 kind: DomainName metadata: index: 2 name: teli.ne - apiVersion: v1 kind: DomainName metadata: index: 3 name: johann.es '''.strip())
def generate(self): appimage_builder_yml_template_path = os.path.realpath( os.path.join(os.path.dirname(__file__), 'templates', 'AppImageBuilder.yml.in')) with open(appimage_builder_yml_template_path, 'r') as filedata: appimage_builder_yml_template = Template.parse(filedata, 'yaml') appimage_builder_yml_ctx = Context({ 'app_info_id': self.app_info_id, 'app_info_name': self.app_info_name, 'app_info_icon': self.app_info_icon, 'app_info_version': self.app_info_version, 'app_info_exec': self.app_info_exec, 'app_info_exec_args': self.app_info_exec_args, 'runtime_generator': self.runtime_generator, 'runtime_env': self.runtime_env, 'apt_arch': self.apt_arch, 'apt_sources': self.apt_sources, 'apt_includes': self.apt_includes, 'apt_excludes': self.apt_excludes, 'files_excludes': self.files_excludes, 'appimage_arch': self.appimage_arch, }) rendered_yml = appimage_builder_yml_template.render( appimage_builder_yml_ctx) logging.info(rendered_yml) with open('AppImageBuilder.yml', 'w') as f: f.write(rendered_yml) self.logger.info("Recipe generation completed.") self.logger.info( "Please manually fill any blank field left before calling appimage-builder" )
def test_format(): template = Template.parse( '!Format "This tests formatting. {person.name!s} {chance:.0%} == {chance:.5f}"', 'yaml' ) ctx = Context({'person': {'name': 'Pipimi'}, 'chance': .99}) output = template.enrich(ctx)[0] assert output == 'This tests formatting. Pipimi 99% == 0.99000'
def test_compose_tag(): comp = Compose({ 'tags': ['Base64', 'Format'], 'value': 'hello {thing}', }) ctx = Context(CONTEXT) output = comp.enrich(ctx) assert base64.b64decode(output).decode() == 'hello world'
def generate(self): template_file_name = "apt.yml.in" if self.apt_includes else "files.yml.in" appimage_builder_yml_template_path = os.path.join( os.path.dirname(__file__), "templates", template_file_name) with open(appimage_builder_yml_template_path, "r") as filedata: appimage_builder_yml_template = Template.parse(filedata, "yaml") appimage_builder_yml_ctx = Context({ "app_info_id": self.app_info_id, "app_info_name": self.app_info_name, "app_info_icon": self.app_info_icon, "app_info_version": self.app_info_version, "app_info_exec": self.app_info_exec, "app_info_exec_args": self.app_info_exec_args, "runtime_generator": self.runtime_generator, "runtime_env": self.runtime_env, "apt_arch": self.apt_arch, "apt_sources": self.apt_sources, "apt_includes": self.apt_includes, "apt_excludes": self.apt_excludes, "files_includes": self.files_include, "files_excludes": self.files_exclude, "appimage_arch": self.appimage_arch, "appimage_update_information": self.appimage_update_information, }) rendered_yml = appimage_builder_yml_template.render( appimage_builder_yml_ctx) logging.info(rendered_yml) with open("AppImageBuilder.yml", "w") as f: f.write( "# appimage-builder recipe see https://appimage-builder.readthedocs.io for details\n" ) f.write(rendered_yml) self.logger.info("Recipe generation completed.") self.logger.info( "Please manually fill any blank field left before calling appimage-builder" )
def test_typeop(tag, val, result): resolved = Template.parse("!%s,Lookup 'a'" % tag).enrich( Context({'a': val}))[0] assert resolved == result, '{tag}({val!r}) returned {resolved}, expected {result}'.format( tag=tag, val=val, resolved=resolved, result=result, )
def test_compose_syntax(format): source = { 'yaml': '!Base64,Format "hello {thing}"', 'json': '{"!Base64,Format": "hello {thing}"}', } template = Template.parse(source[format], format) ctx = Context(CONTEXT) output = template.enrich(ctx)[0] assert base64.b64decode(output).decode() == 'hello world'
def test_hash_variable(h): algo, expected = h VARIABLES = """ helloVar: Hello """ assert Template.parse(f'!{algo},Var helloVar').enrich( Context(VARIABLES)) == [expected]
def test_loop_user_friendliness(): template = Template( [Loop({ 'over': 'domain_names', 'template': Var('item') })]) ctx = Context(LOOP_TEST_CONTEXT) with pytest.raises(ValueError) as ei: template.render(ctx) assert 'did you mean' in str(ei.value)
def test_lookup_all(): template = Template.parse( ''' first_names: !LookupAll people[*].first_name last_names: !LookupAll people[*].last_name ''', 'yaml', ) ctx = Context(LOOKUP_TEST_CONTEXT) output = template.render(ctx) assert (output.strip() == ''' first_names: - Minnie - Popuko last_names: - Duck - Pipimi '''.strip())
def test_as_documents(): template = Template.parse( ''' !Loop over: !Var domain_names as: item as_documents: true template: name: !Var item ''', 'yaml') output = template.render(Context(LOOP_TEST_CONTEXT)) assert (output.strip() == ''' name: hernekeit.to --- name: vii.na --- name: teli.ne --- name: johann.es '''.strip())
def test_lookup(): template = Template.parse( ''' people: !Loop over: !Var people as: person template: - !Lookup person.last_name - !Lookup person.first_name ''', 'yaml', ) ctx = Context(LOOKUP_TEST_CONTEXT) output = template.render(ctx) assert (output.strip() == ''' people: - - Duck - Minnie - - Pipimi - Popuko '''.strip())
def test_lookup_no_match(): template = Template.parse('''!Lookup people..nep''', 'yaml') with pytest.raises(KeyError) as ei: ctx = Context(LOOKUP_TEST_CONTEXT) template.render(ctx) assert 'no matches for' in str(ei.value)
def test_merge(): assert Template.parse(TEMPLATE).enrich(Context()) == [{"foo": 5, "bar": 7}]
def test_typeop(tag, val, result): resolved = Template.parse(f"!{tag},Lookup 'a'").enrich(Context({'a': val}))[0] assert resolved == result, f'{tag}({val!r}) returned {resolved}, expected {result}'
def test_concat(): assert Template.parse(TEMPLATE).enrich(Context()) == [[1, 2, 3, 4, 5, 6]]
def test_flavortown(): assert Template.parse(FLAVORTOWN_YAML).enrich( Context()) == [FLAVORTOWN_RESULT]
def test_base64(): assert Template.parse('!Base64 foobar').enrich(Context()) == ['Zm9vYmFy']
def test_emrichen_json(): c = Context(VARIABLES) t = Template.parse(TEMPLATE_JSON, 'json') output = t.render(c) assert output == EXPECTED