markdown/tabbed_sections: Raise exception for missing tab name.

This fixes the issue  where 'None' would appear in the rendered
html in case of a missing tab display_name. Now,
'test-help-documentation' will fail in case of any tab display_name
being missing.

In case of a tab_section with no tabs, currently a single tab with
the name 'null_tab' gets added. Added the display name 'None' for
'null_tab', to keep in line with the existing behaviour.

Fixes #19822
This commit is contained in:
Pradyumna Sinha 2021-09-27 21:15:24 +00:00 committed by Tim Abbott
parent 78692e9c14
commit 18b36e5b8c
3 changed files with 30 additions and 4 deletions

View File

@ -0,0 +1,11 @@
# Heading
{start_tabs}
{tab|ios}
iOS instructions
{tab|minix}
Minix instructions. We expect an exception because the minix tab doesn't have a declared label.
{end_tabs}

View File

@ -27,7 +27,7 @@ NAV_BAR_TEMPLATE = """
""".strip()
NAV_LIST_ITEM_TEMPLATE = """
<li data-language="{data_language}" tabindex="0">{name}</li>
<li data-language="{data_language}" tabindex="0">{label}</li>
""".strip()
DIV_TAB_CONTENT_TEMPLATE = """
@ -141,10 +141,16 @@ class TabbedSectionsPreprocessor(Preprocessor):
def generate_nav_bar(self, tab_section: Dict[str, Any]) -> str:
li_elements = []
for tab in tab_section["tabs"]:
li = NAV_LIST_ITEM_TEMPLATE.format(
data_language=tab.get("tab_name"), name=TAB_SECTION_LABELS.get(tab.get("tab_name"))
)
tab_name = tab.get("tab_name")
tab_label = TAB_SECTION_LABELS.get(tab_name)
if tab_label is None:
raise ValueError(
f"Tab '{tab_name}' is not present in TAB_SECTION_LABELS in zerver/lib/markdown/tabbed_sections.py"
)
li = NAV_LIST_ITEM_TEMPLATE.format(data_language=tab_name, label=tab_label)
li_elements.append(li)
return NAV_BAR_TEMPLATE.format(tabs="\n".join(li_elements))
def parse_tabs(self, lines: List[str]) -> Optional[Dict[str, Any]]:

View File

@ -92,6 +92,15 @@ footer
expected_html_sans_whitespace = expected_html.replace(" ", "").replace("\n", "")
self.assertEqual(content_sans_whitespace, expected_html_sans_whitespace)
def test_markdown_tabbed_sections_missing_tabs(self) -> None:
template = get_template("tests/test_markdown.html")
context = {
"markdown_test_file": "zerver/tests/markdown/test_tabbed_sections_missing_tabs.md",
}
expected_regex = "^Tab 'minix' is not present in TAB_SECTION_LABELS in zerver/lib/markdown/tabbed_sections.py$"
with self.assertRaisesRegex(ValueError, expected_regex):
template.render(context)
def test_markdown_nested_code_blocks(self) -> None:
template = get_template("tests/test_markdown.html")
context = {