def test_include_filepath_error(page, tmp_path): page_content = '''# Header {% include "/path/to/file/that/does/not/exists" %} ''' page_filepath = tmp_path / 'example.md' page_filepath.write_text(page_content) with pytest.raises(FileNotFoundError): on_page_markdown(page_content, page(page_filepath))
def test_include_markdown_filepath_error(page, tmp_path): page_content = '''{% include-markdown "/path/to/file/that/does/not/exists" start="<!--start-here-->" end="<!--end-here-->" %}''' page_filepath = tmp_path / 'example.md' page_filepath.write_text(page_content) with pytest.raises(FileNotFoundError): on_page_markdown(page_content, page(page_filepath))
def test_include_markdown_invalid_bool_option(opt_name, page, tmp_path): page_filepath = tmp_path / 'example.md' page_content = textwrap.dedent(f'''{{% include-markdown "{page_filepath}" {opt_name}=invalidoption %}}''') page_filepath.write_text(page_content) with pytest.raises(ValueError) as excinfo: on_page_markdown(page_content, page(page_filepath)) expected_exc_message = (f'Unknown value for \'{opt_name}\'.' ' Possible values are: true, false') assert expected_exc_message == str(excinfo.value)
def test_no_filename(self, directive, page, tmp_path, caplog): filename = 'includer.md' # shouldn't raise errors on_page_markdown( f'\n\n{{% {directive} %}}', page(tmp_path / filename), tmp_path, ) assert caplog.records[0].msg == ( f"Found no path passed including with '{directive}' directive" f' at {filename}:3') assert len(caplog.records) == 1
def test_invalid_exclude_argument(directive, page, tmp_path, caplog): drectory_to_include = tmp_path / 'exclude_double_quote_escapes' drectory_to_include.mkdir() page_to_include_filepath = drectory_to_include / 'included.md' page_to_include_filepath.write_text('Content that should be included\n') page_to_exclude_filepath = drectory_to_include / 'igno"re"d.md' page_to_exclude_filepath.write_text('Content that should be excluded\n') includer_glob = os.path.join(str(drectory_to_include), '*.md') result = on_page_markdown( f'''{{% {directive} "{includer_glob}" comments=false exclude= %}}''', page(tmp_path / 'includer.md'), tmp_path, ) assert result == ('Content that should be excluded\n' 'Content that should be included\n') assert len(caplog.records) == 1 assert caplog.records[0].msg == ( f"Invalid empty 'exclude' argument in '{directive}' directive" ' at includer.md:1')
def test_invalid_start_end_arguments(directive, page, caplog, tmp_path): page_to_include_filepath = tmp_path / 'included.md' included_content = '''Content that should be ignored <!-- start --> Content to include <!-- end --> More content that should be ignored ''' page_to_include_filepath.write_text(included_content) result = on_page_markdown( f''' {{% {directive} "{page_to_include_filepath}" comments=false start='' end="" %}}''', page(tmp_path / 'includer.md'), tmp_path, ) assert result == f'\n{included_content}' records_messages = [record.msg for record in caplog.records] expected_args = ['start', 'end'] for arg_name in expected_args: assert (f"Invalid empty '{arg_name}' argument in '{directive}'" ' directive at includer.md:2') in records_messages assert len(records_messages) == len(expected_args)
def test_include(includer_schema, content_to_include, expected_result, page, tmp_path): included_filepath = tmp_path / 'included.md' includer_filepath = tmp_path / 'includer.md' included_filepath.write_text(content_to_include) includer_filepath.write_text( content_to_include.replace('{filepath}', included_filepath.as_posix())) page_content = includer_schema.replace( '{filepath}', included_filepath.as_posix()) includer_filepath.write_text(page_content) assert on_page_markdown( page_content, page(included_filepath)) == expected_result
def test_nested_include(first_includer_content, second_includer_content, included_content, expected_result, page, tmp_path): first_includer_filepath = tmp_path / 'first-includer.txt' second_includer_filepath = tmp_path / 'second-includer.txt' included_filepath = tmp_path / 'included.txt' first_includer_content = first_includer_content.replace( '{filepath}', second_includer_filepath.as_posix()) second_includer_content = second_includer_content.replace( '{filepath}', included_filepath.as_posix()) first_includer_filepath.write_text(first_includer_content) second_includer_filepath.write_text(second_includer_content) included_filepath.write_text(included_content) assert on_page_markdown(first_includer_content, page(first_includer_filepath)) == expected_result
def test_include( includer_schema, content_to_include, expected_result, expected_warnings_schemas, page, caplog, tmp_path, ): included_filepath = tmp_path / 'included.md' includer_filepath = tmp_path / 'includer.md' included_filepath.write_text(content_to_include) includer_filepath.write_text( content_to_include.replace('{filepath}', included_filepath.as_posix()), ) # assert content page_content = includer_schema.replace( '{filepath}', included_filepath.as_posix(), ) includer_filepath.write_text(page_content) assert on_page_markdown( page_content, page(includer_filepath), tmp_path, ) == expected_result # assert warnings expected_warnings_schemas = expected_warnings_schemas or [] expected_warnings = [ msg_schema.replace( '{filepath}', str(includer_filepath.relative_to(tmp_path)), ).replace( '{included_filepath}', str(included_filepath.relative_to(tmp_path)), ) for msg_schema in expected_warnings_schemas ] for record in caplog.records: assert record.msg in expected_warnings assert len(expected_warnings_schemas) == len(caplog.records)
def test_non_existent_filename(self, directive, page, tmp_path, caplog): page_content = f'''{{% {directive} "/path/to/file/that/does/not/exists" start="<!--start-here-->" end="<!--end-here-->" %}}''' page_filepath = tmp_path / 'example.md' page_filepath.write_text(page_content) assert on_page_markdown( page_content, page(page_filepath), tmp_path, ) == '' assert len(caplog.records) == 1 assert re.match(r'No files found including ', caplog.records[0].msg)
def test_unescaped_single_quotes( self, filename, directive, page, tmp_path, ): included_content = 'Foo\n' page_to_include_filepath = tmp_path / filename page_to_include_filepath.write_text(included_content) result = on_page_markdown( f'''{{% {directive} "{page_to_include_filepath}" {'comments=false' if directive == 'include-markdown' else ''} %}}''', page(tmp_path / 'includer.md'), tmp_path, ) assert result == included_content
def test_start_end_mixed_quotes(directive, page, caplog, tmp_path): page_to_include_filepath = tmp_path / 'included.md' page_to_include_filepath.write_text('''Content that should be ignored <!-- "s'tar't" --> Content to include <!-- 'en"d' --> More content that should be ignored ''') result = on_page_markdown( f'''{{% {directive} "{page_to_include_filepath}" comments=false start='<!-- "s\\'tar\\'t" -->' end="<!-- 'en\\"d' -->" %}}''', page(tmp_path / 'includer.md'), tmp_path, ) assert result == '\nContent to include\n' assert len(caplog.records) == 0
def test_exclude_double_quote_escapes(directive, page, tmp_path): drectory_to_include = tmp_path / 'exclude_double_quote_escapes' drectory_to_include.mkdir() page_to_include_filepath = drectory_to_include / 'included.md' page_to_include_filepath.write_text('Content that should be included\n') page_to_exclude_filepath = drectory_to_include / 'igno"re"d.md' page_to_exclude_filepath.write_text('Content that should be excluded\n') page_to_exclude_escaped_filepath = str(page_to_exclude_filepath, ).replace( '"', '\\"') includer_glob = os.path.join(str(drectory_to_include), '*.md') result = on_page_markdown( f'''{{% {directive} "{includer_glob}" comments=false exclude="{page_to_exclude_escaped_filepath}" %}}''', page(tmp_path / 'includer.md'), tmp_path, ) assert result == 'Content that should be included\n'
def test_invalid_bool_arguments(directive, arguments, page, tmp_path, caplog): for argument_name in arguments: page_to_include_filepath = tmp_path / 'included.md' page_to_include_filepath.write_text('Included\n') filename = 'includer.md' result = on_page_markdown( f'''{{% {directive} "{page_to_include_filepath}" {argument_name}=invalidoption %}}''', page(tmp_path / filename), tmp_path, ) assert result == '' assert len(caplog.records) == 1 assert caplog.records[0].msg == ( f"Invalid value for '{argument_name}' argument of '{directive}'" f' directive at {filename}:1. Possible values are true or false.') caplog.clear()
def test_not_escaped_double_quotes( self, directive, filename, page, tmp_path, caplog, ): page_to_include_filepath = tmp_path / filename page_to_include_filepath.write_text('Foo\n') result = on_page_markdown( f'{{% {directive} "{page_to_include_filepath}" %}}', page(tmp_path / 'includer.md'), tmp_path, ) assert not result assert len(caplog.records) == 1 assert re.match( r'^No files found including ', caplog.records[0].msg, )
def test_escaped_single_quotes( self, filename, directive, page, tmp_path, ): included_content = 'Foo\n' page_to_include_filepath = tmp_path / filename page_to_include_filepath.write_text(included_content) # escape filename passed as argument escaped_page_to_include_filepath = str( page_to_include_filepath, ).replace("'", "\\'") result = on_page_markdown( f'''{{% {directive} '{escaped_page_to_include_filepath}' {'comments=false' if directive == 'include-markdown' else ''} %}}''', page(tmp_path / 'includer.md'), tmp_path, ) assert result == included_content
def test_nested_include_relpath(page, tmp_path): first_includer_filepath = tmp_path / 'first-includer.txt' docs_path = tmp_path / 'docs' docs_path.mkdir() second_includer_filepath = docs_path / 'second-includer.txt' included_filepath = tmp_path / 'included.txt' first_includer_content = '''# Header {% include-markdown "docs/second-includer.txt" comments=false %} ''' first_includer_filepath.write_text(first_includer_content) second_includer_content = '''Text from second includer. {% include-markdown "../included.txt" comments=false %} ''' second_includer_filepath.write_text(second_includer_content) included_filepath.write_text('Included content.') expected_result = '''# Header Text from second includer. Included content. ''' assert on_page_markdown(first_includer_content, page(first_includer_filepath)) == expected_result
def test_include_markdown_relative_rewrite(page, tmp_path, rewrite_relative_urls): option_value = '' if rewrite_relative_urls is None else ( 'rewrite_relative_urls=' + rewrite_relative_urls) includer_path = tmp_path / 'includer.md' includer_path.write_text( textwrap.dedent(f''' # Heading {{% include-markdown "docs/page.md" start="<!--start-here-->" end="<!--end-here-->" {option_value} %}} ''')) (tmp_path / 'docs').mkdir() included_file_path = tmp_path / 'docs' / 'page.md' included_file_path.write_text( textwrap.dedent(''' # Subpage Heading <!--start-here--> Here's [a link](page2.md) and here's an image: ![](image.png) Here's a [reference link][ref-link]. [ref-link]: page3.md <!--end-here--> ''')) output = on_page_markdown(includer_path.read_text(), page(str(includer_path))) if rewrite_relative_urls in ['true', None]: assert output == textwrap.dedent(''' # Heading <!-- BEGIN INCLUDE docs/page.md <!--start-here--> <!--end-here--> --> Here's [a link](docs/page2.md) and here's an image: ![](docs/image.png) Here's a [reference link][ref-link]. [ref-link]: docs/page3.md <!-- END INCLUDE --> ''') # noqa: E501 else: # include without rewriting assert output == textwrap.dedent(''' # Heading <!-- BEGIN INCLUDE docs/page.md <!--start-here--> <!--end-here--> --> Here's [a link](page2.md) and here's an image: ![](image.png) Here's a [reference link][ref-link]. [ref-link]: page3.md <!-- END INCLUDE --> ''') # noqa: E501