Conversation
…cs, generate_rss, check_links)
There was a problem hiding this comment.
Pull request overview
This PR adds unit tests under tests/unit/build_scripts/ to cover several Python build/validation scripts, improving reliability across platforms and catching regressions in packaging/docs/link-check tooling.
Changes:
- Added unit tests for
validate_docsTOC parsing / missing-file validation / orphan detection. - Added unit tests for
prepare_packagefrontend build and packaging copy behavior. - Added unit tests for
generate_rssdate extraction and blog markdown parsing, pluscheck_linksURL extraction and resolution helpers.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
tests/unit/build_scripts/test_prepare_package.py |
Tests frontend build failure/success paths and frontend dist copy behavior. |
tests/unit/build_scripts/test_validate_docs.py |
Tests TOC parsing, referenced-file validation, and orphaned doc detection. |
tests/unit/build_scripts/test_generate_rss.py |
Tests filename date parsing and blog markdown parsing helpers. |
tests/unit/build_scripts/test_check_links.py |
Tests URL extraction, fragment stripping, and relative URL resolution. |
| import subprocess | ||
| (tmp_path / "package.json").write_text("{}") | ||
| responses = [ |
There was a problem hiding this comment.
Inline import import subprocess inside the test function violates the repo style guide rule that imports must be at the top of the file (unless breaking a circular dependency). Move this import to the module import section.
| import subprocess | ||
| (tmp_path / "package.json").write_text("{}") | ||
| responses = [ |
There was a problem hiding this comment.
Inline import import subprocess inside the test function violates the repo style guide rule that imports must be at the top of the file (unless breaking a circular dependency). Move this import to the module import section.
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from build_scripts.generate_rss import extract_date_from_filename, parse_blog_markdown | ||
|
|
||
|
|
There was a problem hiding this comment.
Importing build_scripts.generate_rss executes substantial module-level code (generates/writes the RSS file and may sys.exit) rather than just defining functions. These unit tests import from that module, so they can become flaky and can write into the repo’s doc/_build during a unit test run. Consider refactoring generate_rss.py so executable logic is under a main() guarded by if __name__ == "__main__", or adjust tests to import the functions without executing the script body.
| import tempfile | |
| from pathlib import Path | |
| import pytest | |
| from build_scripts.generate_rss import extract_date_from_filename, parse_blog_markdown | |
| import ast | |
| from collections.abc import Callable | |
| from pathlib import Path | |
| from typing import Any | |
| import pytest | |
| def _load_generate_rss_functions() -> tuple[Callable[[str], str], Callable[[Path], tuple[str, str]]]: | |
| """Load the generate_rss helpers without executing the script body.""" | |
| script_path = Path(__file__).resolve().parents[3] / "build_scripts" / "generate_rss.py" | |
| source = script_path.read_text(encoding="utf-8") | |
| parsed_module = ast.parse(source, filename=str(script_path)) | |
| target_functions = {"extract_date_from_filename", "parse_blog_markdown"} | |
| selected_nodes: list[ast.stmt] = [] | |
| for node in parsed_module.body: | |
| if isinstance(node, (ast.Import, ast.ImportFrom)): | |
| selected_nodes.append(node) | |
| continue | |
| if isinstance(node, ast.FunctionDef) and node.name in target_functions: | |
| selected_nodes.append(node) | |
| safe_module = ast.Module(body=selected_nodes, type_ignores=[]) | |
| namespace: dict[str, Any] = {} | |
| exec(compile(safe_module, filename=str(script_path), mode="exec"), namespace) | |
| return namespace["extract_date_from_filename"], namespace["parse_blog_markdown"] | |
| extract_date_from_filename, parse_blog_markdown = _load_generate_rss_functions() |
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
There was a problem hiding this comment.
Unused imports: tempfile and pytest are imported but never used in this test module. Please remove them to avoid lint failures and keep the test focused.
| import tempfile | |
| from pathlib import Path | |
| import pytest | |
| from pathlib import Path |
| import shutil | ||
| import tempfile | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
There was a problem hiding this comment.
Unused imports: shutil, tempfile, and pytest are imported but never used in this test module. This will fail linting (and adds noise); please remove them.
| import shutil | |
| import tempfile | |
| from pathlib import Path | |
| from unittest.mock import MagicMock, patch | |
| import pytest | |
| from pathlib import Path | |
| from unittest.mock import MagicMock, patch |
… safe rss loading
Closes #1570
Adds unit tests for 4 build scripts as requested by @romanlutz in #1569.
test_prepare_package.py— 9 tests covering npm lookup, install/build failures, and frontend copy logictest_validate_docs.py— 14 tests covering TOC parsing, missing file detection, and orphaned file checkstest_generate_rss.py— 10 tests covering date extraction and blog markdown parsingtest_check_links.py— 13 tests covering URL extraction, fragment stripping, and relative URL resolutionAdded 46 unit tests across 4 new test files.
All 65 tests in tests/unit/build_scripts/ pass locally:
python -m pytest tests/unit/build_scripts/ -v
65 passed
No documentation changes required.
No JupyText changes required.