Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown/tabbed_sections: Raise exception for missing tab name.
Pradyumna Sinha18b36e5b8cd9
diff --git a/templates/zerver/tests/markdown/test_tabbed_sections_missing_tabs.md b/templates/zerver/tests/markdown/test_tabbed_sections_missing_tabs.mdnew file mode 100644index 00000000000..023d0464a07--- /dev/null+++ b/templates/zerver/tests/markdown/test_tabbed_sections_missing_tabs.md@@ -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}diff --git a/zerver/lib/markdown/tabbed_sections.py b/zerver/lib/markdown/tabbed_sections.pyindex ce7c866a526..594f39a6c5d 100644--- a/zerver/lib/markdown/tabbed_sections.py+++ b/zerver/lib/markdown/tabbed_sections.py@@ -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]]:diff --git a/zerver/tests/test_templates.py b/zerver/tests/test_templates.pyindex 851e9d27dbe..0fa61f41b34 100644--- a/zerver/tests/test_templates.py+++ b/zerver/tests/test_templates.py@@ -92,6 +92,15 @@ footerexpected_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 = {