def test_successful_path_in_main(tmp_path): """ Being anal retentive here to get the last line covered. :) """ template = tmp_path / "good_path.mdt" template.write_text("just normal text") markplates.process_template(template) runner = click.testing.CliRunner() result = runner.invoke(markplates.main, [str(template)]) assert result.exit_code == 0 assert result.output == "just normal text\n"
def test_import_bad_name(tmp_path): source_file, source = __load_source() # must match simple_source template = tmp_path / "t_import.mdt" template.write_text( '{{ set_path("%s") }}{{ import_function("%s", "not_present") }}' % (source_file.parent, source_file.name)) with pytest.raises(Exception): markplates.process_template(template)
def test_bad_import(tmp_path): # test failing import template = tmp_path / "bad_import.mdt" template.write_text('{{ import_source("no_file.py") }}') with pytest.raises(FileNotFoundError): markplates.process_template(template, False) # test successful import template = tmp_path / "good_import.mdt" template.write_text('{{ import_source("%s") }}' % __file__) markplates.process_template(template, False)
def test_import_func(tmp_path): source_file, source = __load_source() expected = ("".join(source[9:12])).rstrip() template = tmp_path / "t_import.mdt" template.write_text( '{{ set_path("%s") }}{{ import_function("%s", "area") }}' % (source_file.parent, source_file.name)) fred = markplates.process_template(template) assert fred == expected
def test_add_language(tmp_path): source_file, source = __load_source() expected = "".join(source[9:12]) expected = "```python\n" + expected + "```" template = tmp_path / "t_import.mdt" template.write_text( '{{ set_path("%s") }}{{ import_function("%s", "area", "Python", False) }}' % (source_file.parent, source_file.name)) fred = markplates.process_template(template) assert fred == expected
def test_all_spaces(tmp_path): expected_result = "before\nafter" template = tmp_path / "t_import.mdt" with open(template, "w") as temp: temp.write('before{{ import_repl("""\n') temp.write("\n") temp.write("\n") temp.write("\n") temp.write('"""\n') temp.write(") }}after\n") result = markplates.process_template(template, False) assert result == expected_result
def test_import_full(tmp_path, counting_lines): lines = counting_lines(3) # expect all lines except the first expected_result = "".join(lines[1:]).rstrip() file_name = "fake_source.py" source_file = tmp_path / file_name source_file.write_text("".join(lines)) template = tmp_path / "t_import.mdt" template.write_text( '{{ set_path("%s") }}{{ import_source("%s") }}' % (tmp_path, file_name) ) fred = markplates.process_template(template, False) assert fred == expected_result
def process(tmp_path, lines, language=None, filename=False): """ Utility to create a fake file and process a template. """ file_name = "fake_source.py" source_file = tmp_path / file_name source_file.write_text("\n".join(lines)) # need to special case language. If it's present, we want it in "". If # it's None, we don't if language: language = '"%s"' % language template = tmp_path / "t_import.mdt" template.write_text( '{{ set_path("%s") }}{{ import_source("%s", ["1-$"], %s, %s) }}' % (tmp_path, file_name, language, filename) ) return markplates.process_template(template, False)
def test_function_format(tmp_path): expected_result = ( """before\n>>> def func():\n... pass\n\n>>> pass\nafter""") template = tmp_path / "t_import.mdt" with open(template, "w") as temp: temp.write("before\n") temp.write('{{ import_repl("""\n') temp.write("def func():\n") temp.write(" pass\n") temp.write("\n") temp.write("pass\n") temp.write('"""\n') temp.write(") }}\n") temp.write("after\n") result = markplates.process_template(template, False) assert result == expected_result
def test_bad_path(tmp_path): # test setpath to non existent directory template = tmp_path / "bad_path.mdt" template.write_text('{{ set_path("/does/not/exist") }}') with pytest.raises(FileNotFoundError): markplates.process_template(template, False) # test with filename template = tmp_path / "good_path.mdt" template.write_text('{{ set_path("%s") }}' % __file__) with pytest.raises(FileNotFoundError): markplates.process_template(template, False) # test with proper path good_dir = pathlib.Path(__file__).parent template = tmp_path / "good_path.mdt" template.write_text('{{ set_path("%s") }}' % good_dir) markplates.process_template(template, False)
def test_syntax_error(tmp_path): template = tmp_path / "t_import.mdt" template.write_text('{{ import_repl("""++++++""") }}') result = markplates.process_template(template, False) assert "SyntaxError" in result
def test_bad_import(tmp_path): template = tmp_path / "t_import.mdt" template.write_text('{{ import_repl("""os.path()""") }}') result = markplates.process_template(template, False) assert "NameError" in result