Repository navigation
fix(hooks): the push hook skips the repo tests on a triage branch #1821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b86e61f
test: reproduce #1817 — the push hook runs the repo tests on a bug/<n…
V3RON 64d6a51
Merge remote-tracking branch 'origin/bug/1817-repro' into bug/1817
V3RON bf4e938
fix(hooks): the push hook skips the repo tests on a triage branch (#1…
V3RON 40f4449
fix(hooks): the push-hook test runs the step's own block inside a git…
V3RON File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { execFileSync, spawnSync } from "node:child_process"; | ||
| import { | ||
| chmodSync, | ||
| existsSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| readFileSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { dirname, join } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); | ||
| const LEFTHOOK = join(ROOT, "node_modules", ".bin", "lefthook"); | ||
|
|
||
| // why: the stand-in pnpm fails as `vitest run` does when the branch carries a failing | ||
| // *.repo.test.ts, so the step's exit is the hook's verdict on such a push. | ||
| function pushRepoTests(branch: string) { | ||
| // why: a git hook runs with GIT_DIR set, which would aim the scratch repo's git calls at this checkout. | ||
| for (const name of Object.keys(process.env)) { | ||
| if (name.startsWith("GIT_")) delete process.env[name]; | ||
| } | ||
| const repo = mkdtempSync(join(tmpdir(), "push-hook-")); | ||
| execFileSync("git", ["init", "-q", "-b", branch, repo]); | ||
| execFileSync( | ||
| "git", | ||
| ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "x"], | ||
| { | ||
| cwd: repo, | ||
| }, | ||
| ); | ||
| // why: `lefthook dump` drops a skip it has judged against this checkout's branch, and `--command` ignores one, so the | ||
| // scratch repo gets the step's own block from lefthook.yml and the hook runs there on the scratch branch. | ||
| const block = /^ {4}repo-tests:\n(?: {5,}.*\n|\s*\n)*/m.exec( | ||
| readFileSync(join(ROOT, "lefthook.yml"), "utf8"), | ||
| ); | ||
| writeFileSync(join(repo, "lefthook.yml"), `pre-push:\n commands:\n${block?.[0]}`); | ||
| const bin = join(repo, ".bin"); | ||
| mkdirSync(bin); | ||
| const calls = join(repo, "pnpm-calls"); | ||
| writeFileSync(join(bin, "pnpm"), `#!/bin/sh\necho "$@" >> "${calls}"\nexit 1\n`); | ||
| chmodSync(join(bin, "pnpm"), 0o755); | ||
| const { LEFTHOOK: _off, LEFTHOOK_EXCLUDE: _excluded, ...inherited } = process.env; | ||
| const env = { ...inherited, PATH: `${bin}:${process.env.PATH}` }; | ||
| const result = spawnSync( | ||
| LEFTHOOK, | ||
| ["run", "pre-push", "--force", "--no-auto-install", "--no-tty"], | ||
| { cwd: repo, env, encoding: "utf8" }, | ||
| ); | ||
| return { | ||
| status: result.status, | ||
| pnpm: existsSync(calls) ? readFileSync(calls, "utf8") : null, | ||
| }; | ||
| } | ||
|
|
||
| describe("the push hook's repo-tests step", () => { | ||
| it.each(["bug/7-repro", "wip/7-repro"])( | ||
| "does not run the repo tests on %s, whose reproduction fails by design", | ||
| (branch) => { | ||
| expect(pushRepoTests(branch)).toEqual({ status: 0, pnpm: null }); | ||
| }, | ||
| ); | ||
|
|
||
| it("runs every repo test on any other branch and refuses the push when one fails", () => { | ||
| expect(pushRepoTests("task/7")).toEqual({ | ||
| status: 1, | ||
| pnpm: "exec vitest run --project unit .repo.test.ts\n", | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disable commit signing for the fixture commit.
The helper isolates only
GIT_*variables. The developer's globalcommit.gpgSign=truestill applies to the scratch commit on line 30. That commit uses the synthetic identityt@t. If no usable signing key matches that identity,git commitfails andexecFileSyncthrows. The test fails, and therepo-testsstep blocks pushes for that developer.Add
-c commit.gpgSign=falseto the fixture command.src/review-inputs.test.ts:59already uses this override for its fixture commits. The override changes only the scratch commit and leaves the branch-skip assertions unchanged.Suggested fix
Committable suggestion