Skip to content

add unit tests for build scripts#1642

Open
tejas0077 wants to merge 2 commits intomicrosoft:mainfrom
tejas0077:maint/build-script-tests-clean
Open

add unit tests for build scripts#1642
tejas0077 wants to merge 2 commits intomicrosoft:mainfrom
tejas0077:maint/build-script-tests-clean

Conversation

@tejas0077
Copy link
Copy Markdown
Contributor

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 logic
  • test_validate_docs.py — 14 tests covering TOC parsing, missing file detection, and orphaned file checks
  • test_generate_rss.py — 10 tests covering date extraction and blog markdown parsing
  • test_check_links.py — 13 tests covering URL extraction, fragment stripping, and relative URL resolution

Added 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_docs TOC parsing / missing-file validation / orphan detection.
  • Added unit tests for prepare_package frontend build and packaging copy behavior.
  • Added unit tests for generate_rss date extraction and blog markdown parsing, plus check_links URL 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.

Comment thread tests/unit/build_scripts/test_prepare_package.py
Comment on lines +28 to +30
import subprocess
(tmp_path / "package.json").write_text("{}")
responses = [
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +39 to +41
import subprocess
(tmp_path / "package.json").write_text("{}")
responses = [
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXED

Comment thread tests/unit/build_scripts/test_validate_docs.py
Comment thread tests/unit/build_scripts/test_check_links.py
Comment on lines +4 to +11
import tempfile
from pathlib import Path

import pytest

from build_scripts.generate_rss import extract_date_from_filename, parse_blog_markdown


Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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()

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXED

Comment on lines +4 to +8
import tempfile
from pathlib import Path

import pytest

Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
import tempfile
from pathlib import Path
import pytest
from pathlib import Path

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXED

Comment on lines +4 to +10
import shutil
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MAINT add tests for build scripts

3 participants