Skip to content

Commit 2d5f0f3

Browse files
authored
refactor(test): reduce compiled-worker fixture scaffolding (#134300)
* refactor(test): reduce compiled-worker fixture scaffolding * fix(pr): join Git readers before releasing operations Replace asynchronous worktree-state query substitutions with joined foreground pipelines. Propagate reader failures while preserving check-ignore no-match results and conservative operation-lock supervision.
1 parent 738ec3c commit 2d5f0f3

6 files changed

Lines changed: 325 additions & 418 deletions

‎scripts/pr-lib/worktree.sh‎

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,48 +59,46 @@ refuse_review_transition() {
5959
return 1
6060
}
6161

62+
# Foreground pipelines join Git readers and propagate failures through pipefail;
63+
# process substitutions can outlive a successful guard and discard reader failures.
6264
require_no_foreign_untracked() {
6365
local pr="$1"
64-
local foreign=()
6566
local file
66-
while IFS= read -r -d '' file; do
67-
case "$file" in
68-
.local|.local/*) ;;
69-
*) foreign+=("$file") ;;
70-
esac
71-
done < <(git ls-files --others --exclude-standard -z)
72-
[ "${#foreign[@]}" -eq 0 ] || refuse_review_transition "$pr" "untracked files are not owned by scripts/pr."
67+
git ls-files --others --exclude-standard -z |
68+
while IFS= read -r -d '' file; do
69+
case "$file" in .local|.local/*) continue ;; esac
70+
refuse_review_transition "$pr" "untracked files are not owned by scripts/pr."
71+
return 1
72+
done
7373
}
7474

7575
require_no_ignored_transition_paths() {
7676
local pr="$1"
7777
local source="$2"
7878
local target="$3"
79-
local file ignored
80-
while IFS= read -r -d '' file; do
81-
case "$file" in
82-
.local|.local/*)
83-
refuse_review_transition "$pr" "the journaled transition touches the reserved .local artifact namespace."
84-
return 1
85-
;;
86-
esac
87-
done < <(git diff --name-only --no-renames -z "$source" "$target")
79+
local file
80+
git diff --name-only --no-renames -z "$source" "$target" |
81+
while IFS= read -r -d '' file; do
82+
case "$file" in
83+
.local|.local/*)
84+
refuse_review_transition "$pr" "the journaled transition touches the reserved .local artifact namespace."
85+
return 1
86+
;;
87+
esac
88+
done || return 1
8889

8990
# Ask Git about every transition path at once. Per-path ignored-file scans
9091
# become prohibitively slow when a PR is far behind main.
91-
if IFS= read -r -d '' ignored < <(
92-
git check-ignore -z --stdin < <(git diff --name-only --no-renames -z "$source" "$target") |
93-
while IFS= read -r -d '' candidate; do
94-
# check-ignore also reports matching paths that do not exist. Only an
95-
# existing ignored entry can be overwritten by the transition.
96-
if [ -e "$candidate" ] || [ -L "$candidate" ]; then
97-
printf '%s\0' "$candidate"
98-
fi
99-
done
100-
); then
101-
refuse_review_transition "$pr" "ignored file '$ignored' would be overwritten by the journaled transition."
102-
return 1
103-
fi
92+
git diff --name-only --no-renames -z "$source" "$target" |
93+
{ git check-ignore -z --stdin || [ "$?" -eq 1 ]; } |
94+
while IFS= read -r -d '' file; do
95+
# check-ignore also reports paths that do not exist. Only an existing
96+
# ignored entry can be overwritten by the transition.
97+
if [ -e "$file" ] || [ -L "$file" ]; then
98+
refuse_review_transition "$pr" "ignored file '$file' would be overwritten by the journaled transition."
99+
return 1
100+
fi
101+
done
104102
}
105103

106104
validate_review_transition_state() {
@@ -120,12 +118,13 @@ validate_review_transition_state() {
120118

121119
# A path changed from source is owned only when its index mode and blob match target.
122120
local file
123-
while IFS= read -r -d '' file; do
124-
if ! git diff --cached --quiet "$target" -- ":(literal)$file"; then
125-
refuse_review_transition "$pr" "'$file' is neither its journaled source nor target entry."
126-
return 1
127-
fi
128-
done < <(git diff --cached --name-only --no-renames -z "$source")
121+
git diff --cached --name-only --no-renames -z "$source" |
122+
while IFS= read -r -d '' file; do
123+
if ! git diff --cached --quiet "$target" -- ":(literal)$file"; then
124+
refuse_review_transition "$pr" "'$file' is neither its journaled source nor target entry."
125+
return 1
126+
fi
127+
done
129128
}
130129

131130
write_review_transition_journal() {

‎test/scripts/pr-operation-lock.test.ts‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,25 @@ describe("scripts/pr process-group platform guard", () => {
782782

783783
const describePosix = process.platform === "win32" ? describe.skip : describe;
784784
describePosix("scripts/pr per-PR operation lock", () => {
785+
it.each([
786+
["ls-files --others --exclude-standard -z", "require_no_foreign_untracked"],
787+
["diff --name-only --no-renames -z", "require_no_ignored_transition_paths"],
788+
["check-ignore -z --stdin", "require_no_ignored_transition_paths"],
789+
["diff --cached --name-only --no-renames -z", "validate_review_transition_state"],
790+
])("rejects failed %s reads in %s", (query, guard) => {
791+
const repoDir = createRepo();
792+
const head = refOid(repoDir, "HEAD");
793+
const result = runLockShell(repoDir, [
794+
"git() {",
795+
` case "$*" in ${JSON.stringify(query)}*) echo 'fixture query failed' >&2; return 7 ;; esac`,
796+
' command git "$@"',
797+
"}",
798+
`${guard} 42 ${head} ${head} || exit $?`,
799+
]);
800+
expect(result.stderr).toContain("fixture query failed");
801+
expect(result.status, result.stdout + result.stderr).not.toBe(0);
802+
});
803+
785804
it.each([
786805
...(["healthy", "first", "second"] as const).flatMap((failure) =>
787806
[false, true]
@@ -2061,27 +2080,36 @@ describePosix("scripts/pr per-PR operation lock", () => {
20612080
await cleanupRecordedProcessGroup(daemonPidFile, daemonPgid);
20622081
}
20632082
});
2064-
it("joins worktree-list producers before releasing a successful operation lock", async () => {
2083+
it("joins Git read producers before releasing a successful operation lock", async () => {
20652084
const repoDir = createRepo();
20662085
const producerExited = join(repoDir, "worktree-producer-exited");
20672086
const result = await runSupervisedOperation(repoDir, "joined-worktree-operation.sh", [
20682087
"acquire_pr_operation_lock 42",
20692088
"git() {",
2070-
' if [ "$1" = worktree ] && [ "$2" = list ]; then',
2071-
" printf 'worktree %s\\0branch refs/heads/pr-42\\0\\0' \"$PWD\"",
2072-
" exec 1>&-",
2073-
" sleep 0.1",
2074-
" : >worktree-producer-exited",
2075-
" return 0",
2076-
" fi",
2077-
' command git "$@"',
2089+
" local result=0",
2090+
' case "$*" in',
2091+
' "worktree list"*) printf \'worktree %s\\0branch refs/heads/pr-42\\0\\0\' "$PWD" ;;',
2092+
' "ls-files --others --exclude-standard -z"|"diff --name-only --no-renames -z "*|"diff --cached --name-only --no-renames -z "*) ;;',
2093+
' "check-ignore -z --stdin") result=1 ;;',
2094+
' *) command git "$@"; return $? ;;',
2095+
" esac",
2096+
" exec 1>&-",
2097+
" sleep 0.1",
2098+
" : >worktree-producer-exited",
2099+
' return "$result"',
20782100
"}",
20792101
'worktree_is_registered "$PWD"',
20802102
"test -f worktree-producer-exited",
20812103
"rm worktree-producer-exited",
20822104
'resolved="$(worktree_path_for_branch pr-42)"',
20832105
'test "$resolved" = "$PWD"',
20842106
"test -f worktree-producer-exited",
2107+
'head="$(git rev-parse HEAD)"',
2108+
"for guard in require_no_foreign_untracked require_no_ignored_transition_paths validate_review_transition_state; do",
2109+
" rm worktree-producer-exited",
2110+
' "$guard" 42 "$head" "$head" || exit $?',
2111+
" test -f worktree-producer-exited",
2112+
"done",
20852113
]);
20862114
expect(result.status, result.stdout + "\n" + result.stderr).toBe(0);
20872115
expect(existsSync(producerExited)).toBe(true);

0 commit comments

Comments
 (0)