def test_active(self): nav_cfg = [ {'Home': 'index.md'}, {'API Guide': [ {'Running': 'api-guide/running.md'}, {'Testing': 'api-guide/testing.md'}, {'Debugging': 'api-guide/debugging.md'}, {'Advanced': [ {'Part 1': 'api-guide/advanced/part-1.md'}, ]}, ]}, {'About': [ {'Release notes': 'about/release-notes.md'}, {'License': 'about/license.md'} ]} ] cfg = load_config(nav=nav_cfg, site_url='http://example.com/') files = Files([ File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/running.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/testing.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/debugging.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/advanced/part-1.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('about/release-notes.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('about/license.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), ]) site_navigation = get_navigation(files, cfg) # Confirm nothing is active self.assertTrue(all(page.active is False for page in site_navigation.pages)) self.assertTrue(all(item.active is False for item in site_navigation.items)) # Activate site_navigation.items[1].children[3].children[0].active = True # Confirm ancestors are activated self.assertTrue(site_navigation.items[1].children[3].children[0].active) self.assertTrue(site_navigation.items[1].children[3].active) self.assertTrue(site_navigation.items[1].active) # Confirm non-ancestors are not activated self.assertFalse(site_navigation.items[0].active) self.assertFalse(site_navigation.items[1].children[0].active) self.assertFalse(site_navigation.items[1].children[1].active) self.assertFalse(site_navigation.items[1].children[2].active) self.assertFalse(site_navigation.items[2].active) self.assertFalse(site_navigation.items[2].children[0].active) self.assertFalse(site_navigation.items[2].children[1].active) # Deactivate site_navigation.items[1].children[3].children[0].active = False # Confirm ancestors are deactivated self.assertFalse(site_navigation.items[1].children[3].children[0].active) self.assertFalse(site_navigation.items[1].children[3].active) self.assertFalse(site_navigation.items[1].active)
def test_files(self): fs = [ File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.html', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.js', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.css', '/path/to/docs', '/path/to/site', use_directory_urls=True) ] files = Files(fs) self.assertEqual([f for f in files], fs) self.assertEqual(len(files), 6) self.assertEqual(files.documentation_pages(), [fs[0], fs[1]]) self.assertEqual(files.static_pages(), [fs[2]]) self.assertEqual(files.media_files(), [fs[3], fs[4], fs[5]]) self.assertEqual(files.javascript_files(), [fs[4]]) self.assertEqual(files.css_files(), [fs[5]]) self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3]) self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3]) self.assertEqual(files.get_file_from_path('missing.jpg'), None) self.assertTrue(fs[2].src_path in files) self.assertTrue(fs[2].src_path in files) extra_file = File('extra.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) self.assertFalse(extra_file.src_path in files) files.append(extra_file) self.assertEqual(len(files), 7) self.assertTrue(extra_file.src_path in files) self.assertEqual(files.documentation_pages(), [fs[0], fs[1], extra_file])
def test_md_file_nested(self): f = File('foo/bar.md', '/path/to/docs', '/path/to/site', use_directory_urls=False) self.assertPathsEqual(f.src_path, 'foo/bar.md') self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.md') self.assertPathsEqual(f.dest_path, 'foo/bar.html') self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.html') self.assertEqual(f.url, 'foo/bar.html') self.assertEqual(f.name, 'bar') self.assertTrue(f.is_documentation_page()) self.assertFalse(f.is_static_page()) self.assertFalse(f.is_media_file()) self.assertFalse(f.is_javascript()) self.assertFalse(f.is_css())
def test_md_readme_index_file_use_directory_urls(self): f = File('README.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) self.assertPathsEqual(f.src_path, 'README.md') self.assertPathsEqual(f.abs_src_path, '/path/to/docs/README.md') self.assertPathsEqual(f.dest_path, 'index.html') self.assertPathsEqual(f.abs_dest_path, '/path/to/site/index.html') self.assertEqual(f.url, '.') self.assertEqual(f.name, 'index') self.assertTrue(f.is_documentation_page()) self.assertFalse(f.is_static_page()) self.assertFalse(f.is_media_file()) self.assertFalse(f.is_javascript()) self.assertFalse(f.is_css())
def test_page(title, filename, config): test_page = Page( title, File(filename, config['docs_dir'], config['site_dir'], config['use_directory_urls']), config) test_page.content = """ <h1 id="heading-1">Heading 1</h1> <p>Content 1</p> <h2 id="heading-2">Heading 2</h1> <p>Content 2</p> <h3 id="heading-3">Heading 3</h1> <p>Content 3</p>""" test_page.markdown = dedent(""" # Heading 1 ## Heading 2 ### Heading 3""") test_page.toc = get_toc(get_markdown_toc(test_page.markdown)) return test_page
def test_nav_no_directory_urls(self): nav_cfg = [ {'Home': 'index.md'}, {'About': 'about.md'} ] expected = dedent(""" Page(title='Home', url='/index.html') Page(title='About', url='/about.html') """) cfg = load_config(nav=nav_cfg, use_directory_urls=False, site_url='http://example.com/') files = Files( [File(list(item.values())[0], cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']) for item in nav_cfg] ) site_navigation = get_navigation(files, cfg) self.assertEqual(str(site_navigation).strip(), expected) self.assertEqual(len(site_navigation.items), 2) self.assertEqual(len(site_navigation.pages), 2) self.assertEqual(repr(site_navigation.homepage), "Page(title='Home', url='/index.html')")
def test_build_page_plugin_error(self, site_dir, mock_write_file): cfg = load_config(site_dir=site_dir, nav=['index.md'], plugins=[]) files = Files([ File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']) ]) nav = get_navigation(files, cfg) page = files.documentation_pages()[0].page # Fake populate page page.title = 'Title' page.markdown = 'page content' page.content = '<p>page content</p>' with self.assertLogs('mkdocs', level='ERROR') as cm: self.assertRaises(PluginError, build._build_page, page, cfg, files, nav, cfg['theme'].get_env()) self.assertEqual( cm.output, ["ERROR:mkdocs.commands.build:Error building page 'index.md':"]) self.assert_mock_called_once(mock_write_file)
def test_build_page_dirty_not_modified(self, site_dir, mock_write_file): cfg = load_config(site_dir=site_dir, nav=['testing.md'], plugins=[]) files = Files([ File('testing.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']) ]) nav = get_navigation(files, cfg) page = files.documentation_pages()[0].page # Fake populate page page.title = 'Title' page.markdown = 'page content' page.content = '<p>page content</p>' build._build_page(page, cfg, files, nav, cfg['theme'].get_env(), dirty=True) self.assert_mock_called_once(mock_write_file)
def test_nested_ungrouped_no_titles_windows(self): nav_cfg = [ 'index.md', 'about\\contact.md', 'about\\sub\\license.md', ] expected = dedent(""" Page(title=[blank], url='/') Page(title=[blank], url='/about/contact/') Page(title=[blank], url='/about/sub/license/') """) cfg = load_config(nav=nav_cfg, site_url='http://example.com/') files = Files( [File(item, cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']) for item in nav_cfg] ) site_navigation = get_navigation(files, cfg) self.assertEqual(str(site_navigation).strip(), expected) self.assertEqual(len(site_navigation.items), 3) self.assertEqual(len(site_navigation.pages), 3)
def test_nested_ungrouped_nav(self): nav_cfg = [ {'Home': 'index.md'}, {'Contact': 'about/contact.md'}, {'License Title': 'about/sub/license.md'}, ] expected = dedent(""" Page(title='Home', url='/') Page(title='Contact', url='/about/contact/') Page(title='License Title', url='/about/sub/license/') """) cfg = load_config(nav=nav_cfg, site_url='http://example.com/') files = Files( [File(list(item.values())[0], cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']) for item in nav_cfg] ) site_navigation = get_navigation(files, cfg) self.assertEqual(str(site_navigation).strip(), expected) self.assertEqual(len(site_navigation.items), 3) self.assertEqual(len(site_navigation.pages), 3)
def test_files_append_remove_src_paths(self): fs = [ File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.md', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.html', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.js', '/path/to/docs', '/path/to/site', use_directory_urls=True), File('foo/bar.css', '/path/to/docs', '/path/to/site', use_directory_urls=True) ] files = Files(fs) self.assertEqual(len(files), 6) self.assertEqual(len(files.src_paths), 6) extra_file = File('extra.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) self.assertFalse(extra_file.src_path in files) files.append(extra_file) self.assertEqual(len(files), 7) self.assertEqual(len(files.src_paths), 7) self.assertTrue(extra_file.src_path in files) files.remove(extra_file) self.assertEqual(len(files), 6) self.assertEqual(len(files.src_paths), 6) self.assertFalse(extra_file.src_path in files)
def test_indented_nav(self): nav_cfg = [ {'Home': 'index.md'}, {'API Guide': [ {'Running': 'api-guide/running.md'}, {'Testing': 'api-guide/testing.md'}, {'Debugging': 'api-guide/debugging.md'}, {'Advanced': [ {'Part 1': 'api-guide/advanced/part-1.md'}, ]}, ]}, {'About': [ {'Release notes': 'about/release-notes.md'}, {'License': '/license.html'} ]}, {'External': 'https://example.com/'} ] expected = dedent(""" Page(title='Home', url='/') Section(title='API Guide') Page(title='Running', url='/api-guide/running/') Page(title='Testing', url='/api-guide/testing/') Page(title='Debugging', url='/api-guide/debugging/') Section(title='Advanced') Page(title='Part 1', url='/api-guide/advanced/part-1/') Section(title='About') Page(title='Release notes', url='/about/release-notes/') Link(title='License', url='/license.html') Link(title='External', url='https://example.com/') """) cfg = load_config(nav=nav_cfg, site_url='http://example.com/') files = Files([ File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/running.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/testing.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/debugging.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('api-guide/advanced/part-1.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), File('about/release-notes.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls']), ]) site_navigation = get_navigation(files, cfg) self.assertEqual(str(site_navigation).strip(), expected) self.assertEqual(len(site_navigation.items), 4) self.assertEqual(len(site_navigation.pages), 6) self.assertEqual(repr(site_navigation.homepage), "Page(title='Home', url='/')") self.assertIsNone(site_navigation.items[0].parent) self.assertEqual(site_navigation.items[0].ancestors, []) self.assertIsNone(site_navigation.items[1].parent) self.assertEqual(site_navigation.items[1].ancestors, []) self.assertEqual(len(site_navigation.items[1].children), 4) self.assertEqual(repr(site_navigation.items[1].children[0].parent), "Section(title='API Guide')") self.assertEqual(site_navigation.items[1].children[0].ancestors, [site_navigation.items[1]]) self.assertEqual(repr(site_navigation.items[1].children[1].parent), "Section(title='API Guide')") self.assertEqual(site_navigation.items[1].children[1].ancestors, [site_navigation.items[1]]) self.assertEqual(repr(site_navigation.items[1].children[2].parent), "Section(title='API Guide')") self.assertEqual(site_navigation.items[1].children[2].ancestors, [site_navigation.items[1]]) self.assertEqual(repr(site_navigation.items[1].children[3].parent), "Section(title='API Guide')") self.assertEqual(site_navigation.items[1].children[3].ancestors, [site_navigation.items[1]]) self.assertEqual(len(site_navigation.items[1].children[3].children), 1) self.assertEqual(repr(site_navigation.items[1].children[3].children[0].parent), "Section(title='Advanced')") self.assertEqual(site_navigation.items[1].children[3].children[0].ancestors, [site_navigation.items[1].children[3], site_navigation.items[1]]) self.assertIsNone(site_navigation.items[2].parent) self.assertEqual(len(site_navigation.items[2].children), 2) self.assertEqual(repr(site_navigation.items[2].children[0].parent), "Section(title='About')") self.assertEqual(site_navigation.items[2].children[0].ancestors, [site_navigation.items[2]]) self.assertEqual(repr(site_navigation.items[2].children[1].parent), "Section(title='About')") self.assertEqual(site_navigation.items[2].children[1].ancestors, [site_navigation.items[2]]) self.assertIsNone(site_navigation.items[3].parent) self.assertEqual(site_navigation.items[3].ancestors, []) self.assertIsNone(site_navigation.items[3].children)
def test_copy_file(self, src_dir, dest_dir): file = File('test.txt', src_dir, dest_dir, use_directory_urls=False) dest_path = os.path.join(dest_dir, 'test.txt') self.assertPathNotExists(dest_path) file.copy_file() self.assertPathIsFile(dest_path)
def test_get_relative_url(self): to_files = [ 'index.md', 'foo/index.md', 'foo/bar/index.md', 'foo/bar/baz/index.md', 'foo.md', 'foo/bar.md', 'foo/bar/baz.md' ] to_file_urls = [ 'index.html', 'foo/index.html', 'foo/bar/index.html', 'foo/bar/baz/index.html', 'foo.html', 'foo/bar.html', 'foo/bar/baz.html' ] from_file = File('img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=False) expected = [ 'img.jpg', # img.jpg relative to . '../img.jpg', # img.jpg relative to foo/ '../../img.jpg', # img.jpg relative to foo/bar/ '../../../img.jpg', # img.jpg relative to foo/bar/baz/ 'img.jpg', # img.jpg relative to foo.html '../img.jpg', # img.jpg relative to foo/bar.html '../../img.jpg' # img.jpg relative to foo/bar/baz.html ] for i, filename in enumerate(to_files): file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) self.assertEqual(from_file.url, 'img.jpg') self.assertEqual(file.url, to_file_urls[i]) self.assertEqual(from_file.url_relative_to(file.url), expected[i]) self.assertEqual(from_file.url_relative_to(file), expected[i]) from_file = File('foo/img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=False) expected = [ 'foo/img.jpg', # foo/img.jpg relative to . 'img.jpg', # foo/img.jpg relative to foo/ '../img.jpg', # foo/img.jpg relative to foo/bar/ '../../img.jpg', # foo/img.jpg relative to foo/bar/baz/ 'foo/img.jpg', # foo/img.jpg relative to foo.html 'img.jpg', # foo/img.jpg relative to foo/bar.html '../img.jpg' # foo/img.jpg relative to foo/bar/baz.html ] for i, filename in enumerate(to_files): file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) self.assertEqual(from_file.url, 'foo/img.jpg') self.assertEqual(file.url, to_file_urls[i]) self.assertEqual(from_file.url_relative_to(file.url), expected[i]) self.assertEqual(from_file.url_relative_to(file), expected[i]) from_file = File('index.html', '/path/to/docs', '/path/to/site', use_directory_urls=False) expected = [ 'index.html', # index.html relative to . '../index.html', # index.html relative to foo/ '../../index.html', # index.html relative to foo/bar/ '../../../index.html', # index.html relative to foo/bar/baz/ 'index.html', # index.html relative to foo.html '../index.html', # index.html relative to foo/bar.html '../../index.html' # index.html relative to foo/bar/baz.html ] for i, filename in enumerate(to_files): file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) self.assertEqual(from_file.url, 'index.html') self.assertEqual(file.url, to_file_urls[i]) self.assertEqual(from_file.url_relative_to(file.url), expected[i]) self.assertEqual(from_file.url_relative_to(file), expected[i]) from_file = File('file.html', '/path/to/docs', '/path/to/site', use_directory_urls=False) expected = [ 'file.html', # file.html relative to . '../file.html', # file.html relative to foo/ '../../file.html', # file.html relative to foo/bar/ '../../../file.html', # file.html relative to foo/bar/baz/ 'file.html', # file.html relative to foo.html '../file.html', # file.html relative to foo/bar.html '../../file.html' # file.html relative to foo/bar/baz.html ] for i, filename in enumerate(to_files): file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False) self.assertEqual(from_file.url, 'file.html') self.assertEqual(file.url, to_file_urls[i]) self.assertEqual(from_file.url_relative_to(file.url), expected[i]) self.assertEqual(from_file.url_relative_to(file), expected[i])
def test_create_search_index(self): html_content = """ <h1 id="heading-1">Heading 1</h1> <p>Content 1</p> <h2 id="heading-2">Heading 2</h1> <p>Content 2</p> <h3 id="heading-3">Heading 3</h1> <p>Content 3</p> """ base_cfg = load_config() pages = [ Page( 'Home', File('index.md', base_cfg['docs_dir'], base_cfg['site_dir'], base_cfg['use_directory_urls']), base_cfg), Page( 'About', File('about.md', base_cfg['docs_dir'], base_cfg['site_dir'], base_cfg['use_directory_urls']), base_cfg) ] md = dedent(""" # Heading 1 ## Heading 2 ### Heading 3 """) toc = get_toc(get_markdown_toc(md)) full_content = ''.join("""Heading{0}Content{0}""".format(i) for i in range(1, 4)) plugin = search.SearchPlugin() errors, warnings = plugin.load_config({}) for page in pages: # Fake page.read_source() and page.render() page.markdown = md page.toc = toc page.content = html_content index = search_index.SearchIndex(**plugin.config) index.add_entry_from_context(page) self.assertEqual(len(index._entries), 4) loc = page.url self.assertEqual(index._entries[0]['title'], page.title) self.assertEqual(strip_whitespace(index._entries[0]['text']), full_content) self.assertEqual(index._entries[0]['location'], loc) self.assertEqual(index._entries[1]['title'], "Heading 1") self.assertEqual(index._entries[1]['text'], "Content 1") self.assertEqual(index._entries[1]['location'], "{}#heading-1".format(loc)) self.assertEqual(index._entries[2]['title'], "Heading 2") self.assertEqual(strip_whitespace(index._entries[2]['text']), "Content2") self.assertEqual(index._entries[2]['location'], "{}#heading-2".format(loc)) self.assertEqual(index._entries[3]['title'], "Heading 3") self.assertEqual(strip_whitespace(index._entries[3]['text']), "Content3") self.assertEqual(index._entries[3]['location'], "{}#heading-3".format(loc))