def test_duplicate_keys_error(): with pytest.raises(ValueError) as excinfo: cli.build_template_context({ 'ONE_TWO_THREE': 'one', 'ONE_TWO': 'two', 'oneTwoThree': 'failure' }) assert ( 'oneTwoThree is defined multiple times' in str(excinfo.value) or 'ONE_TWO_THREE is defined multiple times' in str(excinfo.value) ) # This is a slightly different code path. If the duplication # is in the middle of the largest similar key, it's caught in # a different place. with pytest.raises(ValueError) as excinfo: cli.build_template_context({ 'ONE_TWO_THREE': 'one', 'ONE_TWO': 'two', 'oneTwo': 'failure' }) assert ( 'oneTwo is defined multiple times' in str(excinfo.value) or 'ONE_TWO is defined multiple times' in str(excinfo.value) )
def test_single_underscore(): context = cli.build_template_context({'_': 'underscore'}) assert len(context.keys()) == 1 assert context['_'] == 'underscore' context = cli.build_template_context({'______': 'underscore'}) assert len(context.keys()) == 1 assert context['_'] == 'underscore'
def test_parse_simple(): context = cli.build_template_context({'A': 1, 'B': 2, 'C': 3}) assert len(context.keys()) == 3 assert context['a'] == 1 assert context['b'] == 2 assert context['c'] == 3
def test_many_underscores(): context = cli.build_template_context({ '__TEST_VARIABLE__SEVEN__': 'weee' }) assert len(context.keys()) == 1 assert context['test']['variable']['seven'] == 'weee'
def test_parse_object(): context = cli.build_template_context({'ONE_TWO_THREE': 4}) assert len(context.keys()) == 1 assert 'two' in context['one'].keys() assert 'three' in context['one']['two'].keys() assert context['one']['two']['three'] == 4
def test_filter_base64(common_environment): tmpfile = NamedTemporaryFile() templateFile = os.path.join(TEST_TEMPLATE_PATH, os.path.join("filters", "base64.jinja")) cli.render(templateFile, tmpfile.name, cli.build_template_context(common_environment), cli.parse_arguments(['-o', tmpfile.name, templateFile])) output = open(tmpfile.name) assert output.read().strip() == """CAMEL_CASE_VARIABLE_ENCODED=aGFuZGxldGhpc3Rvbw==
def test_extensions(common_environment): tmpfile = NamedTemporaryFile() templateFile = os.path.join(TEST_TEMPLATE_PATH, "extensions.jinja") cli.render(templateFile, tmpfile.name, cli.build_template_context(common_environment), cli.parse_arguments(['-o', tmpfile.name, templateFile])) output = open(tmpfile.name) assert output.read() == "en_US.UTF-8" output.close() tmpfile.close()
def test_error(common_environment, capsys): tmpfile = NamedTemporaryFile() templateFile = os.path.join(TEST_TEMPLATE_PATH, "error.jinja") with pytest.raises(TemplateSyntaxError): cli.render(templateFile, tmpfile.name, cli.build_template_context(common_environment), cli.parse_arguments(['-o', tmpfile.name, templateFile])) captured = capsys.readouterr() assert "error.jinja" in captured.err assert " 9: >>" in captured.err
def test_filter_boolean(common_environment): tmpfile = NamedTemporaryFile() templateFile = os.path.join(TEST_TEMPLATE_PATH, os.path.join("filters", "boolean.jinja")) cli.render(templateFile, tmpfile.name, cli.build_template_context(common_environment), cli.parse_arguments(['-o', tmpfile.name, templateFile])) output = open(tmpfile.name) lines = output.readlines() for num, line in enumerate(lines, start=1): assert len(line) == 0 or line.strip() == 'cool', ("test %d should have been truthy" % num)
def test_filter_readfile(common_environment): tmpfile = NamedTemporaryFile() test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates', 'test.data') context = cli.build_template_context(dict(common_environment, TEST_DATA_PATH=test_data_path)) templateFile = os.path.join(TEST_TEMPLATE_PATH, os.path.join("filters", "readfile.jinja")) cli.render(templateFile, tmpfile.name, context, cli.parse_arguments(['-o', tmpfile.name, templateFile])) output = open(tmpfile.name) assert output.read() == "TEST DATA\n"
def test_simple_template(common_environment, common_rendered): # The tmp_path fixture is not relaible across Python versions. # Do this ourselves. tmpfile = NamedTemporaryFile() templateFile = os.path.join(TEST_TEMPLATE_PATH, "simple.jinja") cli.render(templateFile, tmpfile.name, cli.build_template_context(common_environment), cli.parse_arguments(['-o', tmpfile.name, templateFile])) output = open(tmpfile.name) assert output.read().strip() == common_rendered output.close() tmpfile.close()
def test_include(common_environment, common_rendered): tmpfile = NamedTemporaryFile() templateFile = os.path.join(TEST_TEMPLATE_PATH, "include.jinja") cli.render(templateFile, tmpfile.name, cli.build_template_context(common_environment), cli.parse_arguments(['-o', tmpfile.name, '-b', os.path.join( os.path.dirname(__file__), 'templates'), templateFile])) output = open(tmpfile.name) assert output.read().strip() == "top\n" + common_rendered output.close() tmpfile.close()
def render_directory(srcdir, dstdir, context, directory, recursive=False): templatedir = os.path.join(TEST_TEMPLATE_PATH, directory, "templates") rendereddir = os.path.join(TEST_TEMPLATE_PATH, directory, "rendered") copy_tree(templatedir, srcdir) if recursive: args = cli.parse_arguments(['-r', '-o', srcdir, dstdir]) else: args = cli.parse_arguments(['-o', srcdir, dstdir]) cli.render(srcdir, dstdir, cli.build_template_context(context), args) return templatedir, rendereddir
def test_undefined(common_environment): tmpfile = NamedTemporaryFile() templateFile = os.path.join(TEST_TEMPLATE_PATH, "undefined.jinja") cli.render(templateFile, tmpfile.name, cli.build_template_context(common_environment), cli.parse_arguments(['-o', tmpfile.name, templateFile])) output = open(tmpfile.name) assert """7 term -> program -> foo(X) term -> foo(X) -> bar foo(X) -> bar -> baz term -> foo is not defined term -> foo -> bar is not defined""" == output.read()