Skip to content

Commit 5841d2e

Browse files
Kaap10potiuk
andauthored
fix(dev): support stdin '-' and normalize path separators in ci-pytest-batches.py (#1767)
* fix(dev): support stdin '-' and normalize path separators in ci-pytest-batches.py * docs(dev): mention `-` for stdin in ci-pytest-batches.py - module docstring: `--changed-file` names a file, or `-` to read the changed paths from stdin - `--help` text for `--changed-file`: add "or - for stdin" Generated-by: Claude Code (Opus 5.5) * style(setup): format test_probe_1_ssh_agent.py for ruff-format --------- Co-authored-by: Jarek Potiuk <potiuk@apache.org>
1 parent 6ae7930 commit 5841d2e

4 files changed

Lines changed: 26 additions & 12 deletions

File tree

‎plugins/magpie-setup/skills/isolated-setup-doctor/tests/test_probe_1_ssh_agent.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def run_probe(self) -> str:
6969
"SSH_AUTH_SOCK": str(self.sock_path),
7070
"TMPDIR": str(self.tmp),
7171
}
72-
result = subprocess.run(
73-
["bash", str(PROBE)], env=env, capture_output=True, text=True, check=True
74-
)
72+
result = subprocess.run(["bash", str(PROBE)], env=env, capture_output=True, text=True, check=True)
7573
return result.stdout.strip()
7674

7775
def test_counts_listed_identities(self) -> None:

‎tools/dev/ci-pytest-batches.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
Prints a JSON array of `{"key", "name", "paths", "packages"}` for the workflow's
3636
matrix (`paths` and `packages` space-separated, in the same order; `packages` are
3737
the members' project names, so a job syncs only what its members declare).
38-
`--changed-file` names a file listing the changed paths; omit it for a full run.
38+
`--changed-file` names a file listing the changed paths, or `-` to read them from stdin;
39+
omit it for a full run.
3940
`--paths` prints the picked member paths one per line instead, for the local
4041
`workspace-pytest` hook, so a commit runs the same members a pull request would.
4142
"""
@@ -209,16 +210,18 @@ def batches(paths: list[str], weights: dict[str, int]) -> list[dict[str, str]]:
209210
def main(argv: list[str] | None = None) -> int:
210211
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
211212
parser.add_argument("--root", type=Path, default=Path("."))
212-
parser.add_argument("--changed-file", type=Path, help="file listing a pull request's changed paths")
213+
parser.add_argument(
214+
"--changed-file", type=Path, help="file listing a pull request's changed paths, or - for stdin"
215+
)
213216
parser.add_argument("--paths", action="store_true", help="print the picked member paths, one per line")
214217
args = parser.parse_args(argv)
215218
changed = None
216219
if args.changed_file:
217-
changed = [
218-
line.strip()
219-
for line in args.changed_file.read_text(encoding="utf-8").splitlines()
220-
if line.strip()
221-
]
220+
if str(args.changed_file) == "-":
221+
raw_lines = sys.stdin.read().splitlines()
222+
else:
223+
raw_lines = args.changed_file.read_text(encoding="utf-8").splitlines()
224+
changed = [line.strip().replace("\\", "/") for line in raw_lines if line.strip()]
222225
paths = select(args.root, changed)
223226
if args.paths:
224227
for path in paths:

‎tools/dev/run-workspace-check.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ fi
162162
if [ "$CHECK_KEY" = "pytest" ] && [ "$#" -gt 0 ]; then
163163
# The list goes through stdin, not a temp file: a sandboxed agent may
164164
# not be able to write the system temp directory.
165-
applicable=$(printf '%s\n' "$@" | python3 tools/dev/ci-pytest-batches.py --changed-file /dev/stdin --paths)
165+
applicable=$(printf '%s\n' "$@" | python3 tools/dev/ci-pytest-batches.py --changed-file - --paths | tr -d '\r')
166166
status=$?
167167
if [ "$status" -ne 0 ]; then
168168
exit "$status"

‎tools/dev/tests/test_ci_pytest_batches.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from __future__ import annotations
2121

2222
import importlib.util
23+
import io
2324
import json
2425
from pathlib import Path
2526
from types import ModuleType
@@ -91,7 +92,12 @@ def test_a_change_behind_a_member_symlink_picks_the_member(tmp_path):
9192
root = _workspace(tmp_path, {"tools/a": PYTEST, "tools/b": PYTEST})
9293
(root / "plugins" / "shared").mkdir(parents=True)
9394
(root / "tools" / "a" / "src").mkdir()
94-
(root / "tools" / "a" / "src" / "shared").symlink_to(root / "plugins" / "shared")
95+
try:
96+
(root / "tools" / "a" / "src" / "shared").symlink_to(
97+
root / "plugins" / "shared", target_is_directory=True
98+
)
99+
except OSError as exc:
100+
pytest.skip(f"symlinks not supported on this platform: {exc}")
95101
assert cpb.select(root, ["plugins/shared/x.py"]) == ["tools/a"]
96102

97103

@@ -135,6 +141,13 @@ def test_the_shared_toolchain_does_not_make_a_dependent(tmp_path):
135141
assert cpb.select(root, ["tools/dev/src/z.py"]) == ["tools/dev"]
136142

137143

144+
def test_the_cli_reads_stdin_with_dash(tmp_path, monkeypatch, capsys):
145+
root = _workspace(tmp_path, {"tools/a": PYTEST, "tools/b": PYTEST})
146+
monkeypatch.setattr("sys.stdin", io.StringIO("tools\\a\\src\\x.py\ntools/b/tests/test_y.py\n"))
147+
assert cpb.main(["--root", str(root), "--changed-file", "-", "--paths"]) == 0
148+
assert capsys.readouterr().out == "tools/a\ntools/b\n"
149+
150+
138151
def test_the_cli_prints_the_picked_paths(tmp_path, capsys):
139152
root = _workspace(tmp_path, {"tools/a": PYTEST, "tools/b": PYTEST})
140153
changed = tmp_path / "changed.txt"

0 commit comments

Comments
 (0)