From 494462e48e0e77d4051a405305941b2dbb65e070 Mon Sep 17 00:00:00 2001 From: LI <2484593937@qq.com> Date: Mon, 26 Oct 2026 10:00:43 +0800 Subject: [PATCH] Fix literal Markdown file selection in --no-glob mode --- .github/workflows/python-code-quality.yml | 2 + python/scripts/check_md_code_blocks.py | 2 +- .../tests/test_check_md_code_blocks.py | 81 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 python/scripts/tests/test_check_md_code_blocks.py diff --git a/.github/workflows/python-code-quality.yml b/.github/workflows/python-code-quality.yml index bc6afaf4bf7..cc9479c0fcf 100644 --- a/.github/workflows/python-code-quality.yml +++ b/.github/workflows/python-code-quality.yml @@ -108,6 +108,8 @@ jobs: os: ${{ runner.os }} env: UV_CACHE_DIR: /tmp/.uv-cache + - name: Test Markdown file selection + run: uv run --locked --group tool-markdown python -m unittest discover -s scripts/tests -p test_check_md_code_blocks.py - name: Run samples checks run: uv run poe check -S - name: Run markdown code lint diff --git a/python/scripts/check_md_code_blocks.py b/python/scripts/check_md_code_blocks.py index 43d8328376e..cd4714bfa57 100644 --- a/python/scripts/check_md_code_blocks.py +++ b/python/scripts/check_md_code_blocks.py @@ -44,7 +44,7 @@ def expand_file_patterns(patterns: list[str], skip_glob: bool = False) -> list[s # When skip_glob is True, treat patterns as literal file paths # Only include if it's a markdown file if pattern.endswith(".md"): - matches = glob.glob(pattern, recursive=False) + matches = glob.glob(glob.escape(pattern), recursive=False) all_files.extend(matches) else: # Handle both relative and absolute paths with glob expansion diff --git a/python/scripts/tests/test_check_md_code_blocks.py b/python/scripts/tests/test_check_md_code_blocks.py new file mode 100644 index 00000000000..67391e4c823 --- /dev/null +++ b/python/scripts/tests/test_check_md_code_blocks.py @@ -0,0 +1,81 @@ +# Copyright (c) Microsoft. All rights reserved. + +"""Regression tests for literal Markdown file selection.""" + +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +from scripts.check_md_code_blocks import expand_file_patterns + + +class MarkdownFileSelectionTests(unittest.TestCase): + def setUp(self) -> None: + self.temp_dir = tempfile.TemporaryDirectory() + self.addCleanup(self.temp_dir.cleanup) + self.root = Path(self.temp_dir.name) + + def markdown(self, name: str) -> str: + path = self.root / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("```python\nprint('example')\n```\n", encoding="utf-8") + return str(path) + + def test_literal_bracketed_filename(self) -> None: + literal = self.markdown("guide[1].md") + self.assertEqual(expand_file_patterns([literal], skip_glob=True), [literal]) + + def test_literal_filename_does_not_select_glob_match(self) -> None: + literal = self.markdown("guide[1].md") + self.markdown("guide1.md") + self.assertEqual(expand_file_patterns([literal], skip_glob=True), [literal]) + + def test_literal_bracketed_directory(self) -> None: + literal = self.markdown("docs[1]/guide.md") + self.markdown("docs1/guide.md") + self.assertEqual(expand_file_patterns([literal], skip_glob=True), [literal]) + + def test_literal_mode_preserves_file_filtering(self) -> None: + ordinary = self.markdown("guide.md") + non_markdown = self.markdown("notes.txt") + missing = str(self.root / "missing.md") + self.assertEqual( + expand_file_patterns([ordinary, non_markdown, missing], skip_glob=True), + [ordinary], + ) + + def test_literal_mode_sorts_and_deduplicates(self) -> None: + first = self.markdown("a.md") + last = self.markdown("z.md") + self.assertEqual(expand_file_patterns([last, first, last], skip_glob=True), [first, last]) + + def test_default_mode_still_expands_brackets(self) -> None: + pattern = self.markdown("guide[1].md") + match = self.markdown("guide1.md") + self.assertEqual(expand_file_patterns([pattern]), [match]) + + def test_default_mode_still_expands_recursive_patterns(self) -> None: + first = self.markdown("guide.md") + nested = self.markdown("nested/guide.md") + pattern = str(self.root / "**" / "*.md") + self.assertEqual(expand_file_patterns([pattern]), sorted([first, nested])) + + def test_cli_checks_the_literal_file(self) -> None: + literal = self.markdown("guide[1].md") + other = self.markdown("guide1.md") + script = Path(__file__).resolve().parents[1] / "check_md_code_blocks.py" + result = subprocess.run( + [sys.executable, "-B", str(script), "--no-glob", literal], + capture_output=True, + text=True, + check=False, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn(literal, result.stderr) + self.assertNotIn(other, result.stderr) + + +if __name__ == "__main__": + unittest.main()