Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tools/check_all_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ def get_changed_files_from_git(repo_root: pathlib.Path) -> List[str]:
cmd = ["git", "diff", "--name-only", f"{base}...HEAD"]
out = subprocess.check_output(cmd, cwd=repo_root, text=True, stderr=subprocess.DEVNULL)
lines = [line.strip() for line in out.splitlines() if line.strip().endswith((".md", ".ipynb"))]
if lines:
logger.info("Detected %d changed file(s) against %s", len(lines), base)
return lines
# A successful empty diff still establishes the comparison base.
logger.info("Detected %d changed file(s) against %s", len(lines), base)
return lines
Comment thread
BichengWang marked this conversation as resolved.
except Exception:
continue
logger.warning("Could not determine base branch for --changed. Falling back to all files.")
Expand Down
57 changes: 57 additions & 0 deletions tools/test_check_all_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@

"""Unit tests for tools/check_all_links.py link verification logic."""

import contextlib
import io
import json
import pathlib
import subprocess
import sys
import tempfile
import unittest
from unittest import mock

from tools.check_all_links import (
LinkCheckerConfig,
extract_anchors_from_content,
extract_links_from_file,
get_changed_files_from_git,
get_file_anchors,
main,
normalize_cell_source,
run_link_audit,
slugify_heading,
Expand Down Expand Up @@ -289,6 +296,56 @@ def test_github_blob_url_with_query_params_and_casing(self) -> None:
self.assertIn("nonexistent.md", err)
self.assertNotIn("?raw=true", err)

def test_changed_files_stops_at_available_base_without_documents(self) -> None:
"""An empty document diff must not select changes from an older base."""
for diff in ("", "tools/check_all_links.py\n"):
with self.subTest(diff=diff):
with mock.patch(
"tools.check_all_links.subprocess.check_output",
side_effect=[diff, "quickstarts/guide.md\n"],
):
self.assertEqual(get_changed_files_from_git(self.repo_root), [])

def test_changed_files_falls_back_when_base_is_unavailable(self) -> None:
"""An unavailable upstream ref still falls back to origin and filters paths."""
with mock.patch(
"tools.check_all_links.subprocess.check_output",
side_effect=[
subprocess.CalledProcessError(128, "git diff"),
"README.md\ntools/check_all_links.py\nquickstarts/sample.ipynb\n",
],
):
self.assertEqual(
get_changed_files_from_git(self.repo_root),
["README.md", "quickstarts/sample.ipynb"],
)

def test_changed_cli_skips_unrelated_broken_links(self) -> None:
"""No document changes against origin must not audit an older broken guide."""
for diff in ("", "tools/check_all_links.py\n"):
with self.subTest(diff=diff):
output = io.StringIO()
with (
mock.patch(
"tools.check_all_links.subprocess.check_output",
side_effect=[
subprocess.CalledProcessError(128, "git diff"),
diff,
"quickstarts/guide.md\n",
],
),
mock.patch.object(
sys,
"argv",
["check_all_links.py", "--repo-dir", str(self.repo_root), "--changed"],
),
contextlib.redirect_stdout(output),
self.assertRaises(SystemExit) as exit_context,
):
main()
self.assertEqual(exit_context.exception.code, 0, output.getvalue())
self.assertIn("No modified .md or .ipynb files detected", output.getvalue())


if __name__ == "__main__":
unittest.main()
Loading