From 83de958acc63478d2adffdcce8a36aee340accb0 Mon Sep 17 00:00:00 2001 From: Soumyaditya Masanta Date: Sat, 28 Nov 2026 23:47:52 +0530 Subject: [PATCH] Add integration tests and zimcheck --- .github/workflows/DailyTests.yaml | 57 ++++++++++++ .github/workflows/Tests.yaml | 49 ++++++++++ CHANGELOG.md | 5 ++ pyproject.toml | 1 + tests-integration/README.md | 4 + tests-integration/conftest.py | 17 ++++ tests-integration/test_zim_content.py | 124 ++++++++++++++++++++++++++ 7 files changed, 257 insertions(+) create mode 100644 .github/workflows/DailyTests.yaml create mode 100644 tests-integration/README.md create mode 100644 tests-integration/conftest.py create mode 100644 tests-integration/test_zim_content.py diff --git a/.github/workflows/DailyTests.yaml b/.github/workflows/DailyTests.yaml new file mode 100644 index 0000000..2589230 --- /dev/null +++ b/.github/workflows/DailyTests.yaml @@ -0,0 +1,57 @@ +name: Daily Tests + +on: + schedule: + - cron: "0 4 * * *" + workflow_dispatch: + +jobs: + run-integration-tests: + runs-on: ubuntu-24.04 + + steps: + - name: Retrieve source code + uses: actions/checkout@v6 + + - name: Build the Docker image + run: | + docker build -t local-devdocs . + + - name: Run scraper + run: | + mkdir -p output + # The scraper does not retry failed HTTP requests, so retry the whole run + for attempt in 1 2 3; do + if docker run --rm -v "$PWD/output:/output" local-devdocs devdocs2zim \ + --slug git \ + --file-name-format "tests_en_devdocs_{clean_slug}" \ + --name-format "tests_en_devdocs_{clean_slug}" \ + --title-format "{full_name} Test" \ + --description-format "Test ZIM for {full_name}"; then + exit 0 + fi + echo "Scraper failed on attempt $attempt" + sleep 30 + done + exit 1 + + - name: Archive ZIM + uses: actions/upload-artifact@v6 + with: + name: tests_en_devdocs_git.zim + path: output/tests_en_devdocs_git.zim + retention-days: 30 + + - name: Run zimcheck + run: | + docker run --rm -v "$PWD/output:/output:ro" \ + ghcr.io/openzim/zim-tools:3.8.0 zimcheck /output/tests_en_devdocs_git.zim + + - name: Run integration test suite + run: | + docker run --rm \ + -v "$PWD/tests-integration:/src/tests-integration:ro" \ + -v "$PWD/output:/output:ro" \ + -e ZIM_FILE_PATH=/output/tests_en_devdocs_git.zim \ + local-devdocs \ + bash -c "pip install --no-cache-dir pytest && pytest -v -p no:cacheprovider /src/tests-integration" diff --git a/.github/workflows/Tests.yaml b/.github/workflows/Tests.yaml index 4725215..ded7682 100644 --- a/.github/workflows/Tests.yaml +++ b/.github/workflows/Tests.yaml @@ -62,3 +62,52 @@ jobs: - name: Ensure we can start the Docker image run: | docker run --rm testimage + + run-integration-tests: + runs-on: ubuntu-24.04 + steps: + - name: Retrieve source code + uses: actions/checkout@v6 + + - name: Build the Docker image + run: | + docker build -t local-devdocs . + + - name: Run scraper + run: | + mkdir -p output + # The scraper does not retry failed HTTP requests, so retry the whole run + for attempt in 1 2 3; do + if docker run --rm -v "$PWD/output:/output" local-devdocs devdocs2zim \ + --slug git \ + --file-name-format "tests_en_devdocs_{clean_slug}" \ + --name-format "tests_en_devdocs_{clean_slug}" \ + --title-format "{full_name} Test" \ + --description-format "Test ZIM for {full_name}"; then + exit 0 + fi + echo "Scraper failed on attempt $attempt" + sleep 30 + done + exit 1 + + - name: Archive ZIM + uses: actions/upload-artifact@v6 + with: + name: tests_en_devdocs_git.zim + path: output/tests_en_devdocs_git.zim + retention-days: 7 + + - name: Run zimcheck + run: | + docker run --rm -v "$PWD/output:/output:ro" \ + ghcr.io/openzim/zim-tools:3.8.0 zimcheck /output/tests_en_devdocs_git.zim + + - name: Run integration test suite + run: | + docker run --rm \ + -v "$PWD/tests-integration:/src/tests-integration:ro" \ + -v "$PWD/output:/output:ro" \ + -e ZIM_FILE_PATH=/output/tests_en_devdocs_git.zim \ + local-devdocs \ + bash -c "pip install --no-cache-dir pytest && pytest -v -p no:cacheprovider /src/tests-integration" diff --git a/CHANGELOG.md b/CHANGELOG.md index 74ae055..1057557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add an integration test suite that scrapes the Git docs, checks the ZIM with + zimcheck and runs in the Tests workflow and every day (#72) + ### Fixed - Fix prettier and eslint check in zimui QA CI (#59) diff --git a/pyproject.toml b/pyproject.toml index 2e7bbfa..32bdb71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -193,6 +193,7 @@ ban-relative-imports = "all" [tool.ruff.lint.per-file-ignores] # Tests can use magic values, assertions, and relative imports "tests/**/*" = ["PLR2004", "S101", "TID252"] +"tests-integration/**/*" = ["PLR2004", "S101", "TID252"] [tool.pytest.ini_options] minversion = "7.3" diff --git a/tests-integration/README.md b/tests-integration/README.md new file mode 100644 index 0000000..a0d5c05 --- /dev/null +++ b/tests-integration/README.md @@ -0,0 +1,4 @@ +This folder contains integration tests, checking the ZIM built by the scraper from end +to end. + +They are meant to be run from the scraper Docker image, from GitHub workflow(s). diff --git a/tests-integration/conftest.py b/tests-integration/conftest.py new file mode 100644 index 0000000..8290ab9 --- /dev/null +++ b/tests-integration/conftest.py @@ -0,0 +1,17 @@ +import os +from pathlib import Path + +import pytest +from zimscraperlib.zim import Archive + +ZIM_FILE_PATH = Path(os.environ["ZIM_FILE_PATH"]) + + +@pytest.fixture(scope="module") +def zim_file_path() -> Path: + return ZIM_FILE_PATH + + +@pytest.fixture(scope="module") +def zim_fh() -> Archive: + return Archive(ZIM_FILE_PATH) diff --git a/tests-integration/test_zim_content.py b/tests-integration/test_zim_content.py new file mode 100644 index 0000000..1a3b075 --- /dev/null +++ b/tests-integration/test_zim_content.py @@ -0,0 +1,124 @@ +import json +from pathlib import Path +from typing import Any + +import pytest +from zimscraperlib.zim import Archive + +# Text the scraper writes in place of a page which is missing from DevDocs +MISSING_PAGE_TEXT = "This documentation is missing." + + +def test_is_file(zim_file_path: Path): + """Ensure ZIM file exists""" + assert zim_file_path.exists() + assert zim_file_path.is_file() + + +def test_zim_main_page(zim_fh: Archive): + """Ensure main page redirects to the landing page""" + + main_entry = zim_fh.main_entry + assert main_entry.is_redirect + assert main_entry.get_redirect_entry().path == "index" + + +def test_zim_metadata(zim_fh: Archive): + """Ensure ZIM metadata are present and correct""" + + assert zim_fh.get_text_metadata("Name") == "tests_en_devdocs_git" + assert zim_fh.get_text_metadata("Title") == "Git Test" + assert zim_fh.get_text_metadata("Description") == "Test ZIM for Git" + assert zim_fh.get_text_metadata("Language") == "eng" + assert zim_fh.get_text_metadata("Publisher") == "openZIM" + assert zim_fh.get_text_metadata("Creator") == "DevDocs" + assert "devdocs2zim " in zim_fh.get_text_metadata("Scraper") + + +def test_zim_tags(zim_fh: Archive): + """Ensure expected tags are present""" + + tags = zim_fh.get_text_metadata("Tags").split(";") + for expected_tag in ["devdocs", "git"]: + assert expected_tag in tags + + +def test_zim_illustration(zim_fh: Archive): + """Ensure the 48x48 illustration is a PNG image""" + + illustration = zim_fh.get_metadata("Illustration_48x48@1") + assert illustration.startswith(b"\x89PNG") + + +@pytest.mark.parametrize( + "item_path,expected_mimetype", + [ + pytest.param("index", "text/html", id="index"), + pytest.param("git-add", "text/html", id="git-add"), + pytest.param("git-commit", "text/html", id="git-commit"), + pytest.param("gitglossary", "text/html", id="gitglossary"), + pytest.param("navbar.json", "application/json", id="navbar.json"), + pytest.param("licenses.txt", "text/plain", id="licenses.txt"), + pytest.param("application.css", "text/css", id="application.css"), + ], +) +def test_zim_content_expected_files( + zim_fh: Archive, item_path: str, expected_mimetype: str +): + """Ensure expected files are present with the right mimetype""" + + item = zim_fh.get_item(item_path) + assert item.mimetype == expected_mimetype + assert len(item.content) > 0 + + +@pytest.mark.parametrize("item_path", ["assets/index.js", "assets/index.css"]) +def test_zim_content_zimui_assets(zim_fh: Archive, item_path: str): + """Ensure the ZIM UI assets are present""" + + assert len(zim_fh.get_item(item_path).content) > 0 + + +def _navbar_hrefs(node: dict[str, Any]) -> list[str]: + hrefs = [node["href"]] if "href" in node else [] + for child in node.get("children", []): + hrefs.extend(_navbar_hrefs(child)) + return hrefs + + +def test_zim_navbar(zim_fh: Archive): + """Ensure every page linked in the navbar is in the ZIM""" + + navbar = json.loads(zim_fh.get_content("navbar.json")) + assert navbar["name"] == "Git" + assert navbar["landingHref"] == "index" + hrefs = _navbar_hrefs(navbar) + assert hrefs + for href in hrefs: + assert zim_fh.has_entry_by_path(href.split("#")[0]), href + + +def test_zim_no_missing_page(zim_fh: Archive): + """Ensure no page has been replaced by the missing page message""" + + checked_pages = 0 + for entry_id in range(zim_fh.entry_count): + entry = zim_fh.get_entry_by_id(entry_id) + if entry.is_redirect: + continue + item = entry.get_item() + if item.mimetype != "text/html": + continue + assert MISSING_PAGE_TEXT not in bytes(item.content).decode(), entry.path + checked_pages += 1 + # Make sure the test did not pass without checking any page + assert checked_pages > 0 + + +def test_zim_search(zim_fh: Archive): + """Ensure full-text search and title suggestions are working""" + + assert zim_fh.has_fulltext_index + assert zim_fh.has_title_index + assert "git-commit" in zim_fh.get_search_results("commit", 0, 10) + assert "git-commit" in zim_fh.get_suggestions("git com", 0, 10)