From 0fabcd2358b88b873f339ef1b22dcd02406c957d Mon Sep 17 00:00:00 2001 From: Darragh Elliott Date: Thu, 7 May 2026 14:31:22 +0000 Subject: [PATCH] feat: 100% coverage for process file tests --- .../lib/process_file_test.py | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/build/fsfe_website_build_tests_pre_commit/lib/process_file_test.py b/build/fsfe_website_build_tests_pre_commit/lib/process_file_test.py index c2325e05b7..990f672869 100644 --- a/build/fsfe_website_build_tests_pre_commit/lib/process_file_test.py +++ b/build/fsfe_website_build_tests_pre_commit/lib/process_file_test.py @@ -4,11 +4,16 @@ import textwrap from pathlib import Path +from tempfile import TemporaryDirectory +from typing import TYPE_CHECKING import pytest from fsfe_website_build.lib.process_file import process_file from lxml import etree +if TYPE_CHECKING: + from pytest_mock import MockFixture + @pytest.fixture def sample_xsl_transformer(tmp_path: Path) -> etree.XSLT: @@ -75,3 +80,89 @@ def process_file_link_rewrites_test( # we only need to care about the first one link_node = etree.fromstring(result_doc).xpath("//a[@href and @test_url]")[0] assert link_node.get("href") == link_out + + +def xmllist_processing_test(sample_xsl_transformer: etree.XSLT) -> None: + """Process something that has an XMLLIST.""" + with TemporaryDirectory() as tmp: + source = Path(tmp) / "source" + source.mkdir() + + lang_folder = source / "global" / "languages" + lang_folder.mkdir(parents=True, exist_ok=True) + + en_lang_file = lang_folder / "en" + en_lang_file.write_text("English\n") + + action_dir = source / "news" + action_dir.mkdir() + + action_file = action_dir / "news.en.xhtml" + action_file.write_text("News") + + list_file = action_dir / ".news.xmllist" + list_file.write_text("global/data/sidebar\n") + + sidebar_dir = source / "global" / "data" / "sidebar" + sidebar_dir.mkdir(parents=True) + sidebar_en = sidebar_dir / ".sidebar.en.xml" + sidebar_en.write_text("Link") + + infile = action_dir / "news.de.xhtml" + + result = process_file(source, infile, sample_xsl_transformer) + assert result is not None + + +def missing_translation_fallback_test(sample_xsl_transformer: etree.XSLT) -> None: + """Process a file where it does not exist in the correct language.""" + with TemporaryDirectory() as tmp: + source = Path(tmp) / "source" + source.mkdir() + + lang_folder = source / "global" / "languages" + lang_folder.mkdir(parents=True, exist_ok=True) + + en_lang_file = lang_folder / "en" + en_lang_file.write_text("English\n") + + de_lang_file = lang_folder / "de" + de_lang_file.write_text("Deutsch\n") + + action_dir = source / "news" + action_dir.mkdir() + + en_file = action_dir / "news.en.xhtml" + en_file.write_text("News") + + infile = action_dir / "news.de.xhtml" + + result = process_file(source, infile, sample_xsl_transformer) + assert result is not None + + +def detect_invalid_xml_from_transformation_test(mocker: MockFixture) -> None: + """Check that it detects invalid XML being returned""" + with TemporaryDirectory() as tmp: + source = Path(tmp) / "source" + source.mkdir() + lang_folder = source / "global" / "languages" + lang_folder.mkdir(parents=True, exist_ok=True) + + en_lang_file = lang_folder / "en" + en_lang_file.write_text("English\n") + + action_dir = source / "news" + action_dir.mkdir() + + infile = action_dir / "news.en.xhtml" + infile.write_text("News") + + mock_result = mocker.MagicMock() + mock_result.xpath.side_effect = AssertionError("bad xml") + + mock_transform = mocker.MagicMock() + mock_transform.return_value = mock_result + + result = process_file(source, infile, mock_transform) + assert result == str(mock_result)