Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions tools/check_all_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ def get_changed_files_from_git(repo_root: pathlib.Path) -> Optional[List[str]]:
base_refs = ["upstream/main", "origin/main", "main", "HEAD~1"]
for base in base_refs:
try:
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"))]
cmd = ["git", "diff", "--name-only", "-z", f"{base}...HEAD"]
out = subprocess.check_output(
cmd, cwd=repo_root, encoding="utf-8", stderr=subprocess.DEVNULL
)
lines = [path for path in out.split("\0") if path.endswith((".md", ".ipynb"))]
Comment on lines +308 to +312

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

According to the Repository Style Guide (under Internal tooling (tools/)), changes to internal scripts under tools/ are only accepted when they fix a concrete bug actively affecting existing notebooks, Markdown files, or CI checks on the main branch. If this PR only addresses theoretical or synthetic edge cases (such as files with non-ASCII or special characters in their names that do not currently exist on main), we recommend closing this PR. Could you clarify if there are any existing files on main currently affected by this issue?

References
  1. Changes to internal scripts under tools/ are only accepted when they fix a concrete bug actively affecting existing notebooks, Markdown files, or CI checks on the main branch. If a PR only addresses theoretical or synthetic edge cases that do not occur on main, call it out in the review and recommend closing it. (link)

# A successful empty diff still establishes the comparison base.
logger.info("Detected %d changed file(s) against %s", len(lines), base)
return lines
Expand Down
73 changes: 68 additions & 5 deletions tools/test_check_all_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import contextlib
import io
import json
import locale
import pathlib
import subprocess
import sys
Expand Down Expand Up @@ -351,11 +352,11 @@ def test_github_blob_url_with_query_params_and_casing(self) -> None:

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"):
for diff in ("", "tools/check_all_links.py\0"):
with self.subTest(diff=diff):
with mock.patch(
"tools.check_all_links.subprocess.check_output",
side_effect=[diff, "quickstarts/guide.md\n"],
side_effect=[diff, "quickstarts/guide.md\0"],
):
self.assertEqual(get_changed_files_from_git(self.repo_root), [])

Expand All @@ -365,7 +366,7 @@ def test_changed_files_falls_back_when_base_is_unavailable(self) -> None:
"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",
"README.md\0tools/check_all_links.py\0quickstarts/sample.ipynb\0",
],
):
self.assertEqual(
Expand All @@ -375,7 +376,7 @@ def test_changed_files_falls_back_when_base_is_unavailable(self) -> None:

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"):
for diff in ("", "tools/check_all_links.py\0"):
with self.subTest(diff=diff):
output = io.StringIO()
with (
Expand All @@ -384,7 +385,7 @@ def test_changed_cli_skips_unrelated_broken_links(self) -> None:
side_effect=[
subprocess.CalledProcessError(128, "git diff"),
diff,
"quickstarts/guide.md\n",
"quickstarts/guide.md\0",
],
),
mock.patch.object(
Expand All @@ -399,6 +400,68 @@ def test_changed_cli_skips_unrelated_broken_links(self) -> None:
self.assertEqual(exit_context.exception.code, 0, output.getvalue())
self.assertIn("No modified .md or .ipynb files detected", output.getvalue())

def test_changed_files_preserves_special_characters(self) -> None:
"""Git paths must not be quoted, split on newlines, or stripped."""
paths = [
"quickstarts/指南.ipynb",
"quickstarts/line\nbreak.md",
"quickstarts/tab\tname.md",
'quickstarts/quoted"name.md',
" leading-space.md",
]
with mock.patch(
"tools.check_all_links.subprocess.check_output",
return_value="\0".join(paths + ["tools/check_all_links.py", ""]),
) as git_diff:
self.assertEqual(get_changed_files_from_git(self.repo_root), paths)
self.assertEqual(
git_diff.call_args.args[0],
["git", "diff", "--name-only", "-z", "upstream/main...HEAD"],
)

def test_changed_cli_audits_unicode_filename(self) -> None:
"""A changed Unicode filename must not hide a broken link from the CLI."""
target = self.quickstarts_dir / "指南.md"
target.write_text("[Broken](missing.md)\n", encoding="utf-8")
output = io.StringIO()
with (
mock.patch(
"tools.check_all_links.subprocess.check_output",
return_value="quickstarts/指南.md\0",
),
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, 1, output.getvalue())
self.assertIn("quickstarts/指南.md", output.getvalue())
self.assertIn("File does not exist: missing.md", output.getvalue())

def test_changed_files_decodes_utf8_with_non_utf8_locale(self) -> None:
"""Unicode paths must survive subprocess decoding under an ASCII locale."""
path = "quickstarts/指南.md"
raw_output = (path + "\0").encode("utf-8")
check_output = subprocess.check_output
original_locale = locale.setlocale(locale.LC_CTYPE)
self.addCleanup(locale.setlocale, locale.LC_CTYPE, original_locale)
locale.setlocale(locale.LC_CTYPE, "C")

def git_diff_output(cmd, **kwargs):
return check_output(
[sys.executable, "-c", f"import sys; sys.stdout.buffer.write({raw_output!r})"],
**kwargs,
)

with mock.patch(
"tools.check_all_links.subprocess.check_output", side_effect=git_diff_output
):
self.assertEqual(get_changed_files_from_git(self.repo_root), [path])

Comment on lines +403 to +464

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

According to the Repository Style Guide (under Internal tooling (tools/)), we do not want to multiply or maintain unit test suites for internal helper scripts, and we should ask to remove any new unit test files or unit test additions submitted for tools/. Please remove these new unit test additions (test_changed_files_preserves_special_characters, test_changed_cli_audits_unicode_filename, and test_changed_files_decodes_utf8_with_non_utf8_locale).

References
  1. Internal helper scripts under tools/ do not need unit tests, and we do not want to multiply or maintain unit test suites (tools/test_*.py). Never ask authors to add unit tests for tools/ changes, and ask to remove any new unit test files or unit test additions submitted for tools/. (link)

def test_changed_files_returns_none_when_no_base_is_available(self) -> None:
"""Unavailable comparison bases must be distinct from an empty diff."""
with (
Expand Down
Loading