Skip to content

Commit 442eaac

Browse files
msyycCopilot
andauthored
[python] Fix test_sensitive_word failing on Windows (#10300)
## Problem `test_sensitive_word` in `test_unbranded.py` fails on Windows CI because the `check_sensitive_word` function uses PowerShell `Select-String` which outputs **relative paths**, while the regex expects **absolute paths** from `folder.as_posix()`. This causes the function to always return `[]` on Windows. On Linux, `grep` outputs absolute paths so the test passes. ## Fix Replace the platform-specific shell commands (`powershell Select-String` / `grep`) with pure Python using `pathlib.rglob` + `read_text`. This is: - **Cross-platform** - works identically on Windows and Linux - **Simpler** - no shell escaping, regex parsing, or platform branching - **Reliable** - no dependency on shell output format --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ced6d35 commit 442eaac

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
changeKind: internal
3+
packages:
4+
- "@typespec/http-client-python"
5+
---
6+
Fix `test_sensitive_word` failing on Windows by replacing shell-based search with pure Python `pathlib` implementation.

packages/http-client-python/tests/mock_api/unbranded/test_unbranded.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55
import os
6-
import re
7-
from subprocess import getoutput
86
from pathlib import Path
97
import traceback
108
from importlib import import_module
@@ -33,21 +31,25 @@ def test_track_back(client: ScalarClient):
3331
assert "microsoft" not in track_back
3432

3533

36-
def check_sensitive_word(folder: Path, word: str) -> str:
37-
special_folders = ["__pycache__", "pytest_cache"]
38-
if os.name == "nt":
39-
skip_folders = "|".join(special_folders)
40-
output = getoutput(
41-
f"powershell \"ls -r -Path {folder} | where fullname -notmatch '{skip_folders}' | Select-String -Pattern '{word}'\""
42-
).replace("\\", "/")
43-
else:
44-
skip_folders = "{" + ",".join(special_folders) + "}"
45-
output = getoutput(f"grep -ri --exclude-dir={skip_folders} {word} {folder}")
34+
_SKIP_DIRS = {"__pycache__", "pytest_cache", ".pytest_cache"}
4635

36+
37+
def check_sensitive_word(folder: Path, word: str) -> list[str]:
38+
"""Search for a word in all files under folder, return top-level subfolder names that contain it."""
4739
result = set()
48-
for item in re.findall(f"{folder.as_posix()}[^:]+", output.replace("\n", "")):
49-
result.add(Path(item).relative_to(folder).parts[0])
50-
return sorted(list(result))
40+
for path in folder.rglob("*"):
41+
if not path.is_file():
42+
continue
43+
# Skip special directories
44+
if _SKIP_DIRS & set(path.relative_to(folder).parts):
45+
continue
46+
try:
47+
content = path.read_text(encoding="utf-8", errors="ignore")
48+
except (OSError, UnicodeDecodeError):
49+
continue
50+
if word.lower() in content.lower():
51+
result.add(path.relative_to(folder).parts[0])
52+
return sorted(result)
5153

5254

5355
def test_sensitive_word():

0 commit comments

Comments
 (0)