Repository navigation
fix(cli): reject unrecognized nonempty patch input #1183
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
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,5 @@ | ||
| --- | ||
| "hunkdiff": patch | ||
| --- | ||
|
|
||
| Reject nonempty patch input without recognizable file changes instead of silently opening an empty review. |
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,57 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { loadAppBootstrap } from "./loaders"; | ||
|
|
||
| const textPatch = | ||
| "diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-before\n+after\n"; | ||
|
|
||
| /** Load explicit patch text through the same input boundary as files and stdin. */ | ||
| function loadTestPatch(text: string) { | ||
| return loadAppBootstrap({ kind: "patch", text, options: {} }); | ||
| } | ||
|
|
||
| describe("explicit patch input validation", () => { | ||
| test.each(["not a patch\n", "error: upstream command failed\n", "diff --git\n"])( | ||
| "rejects nonempty input without any recognized files (%s)", | ||
| async (text) => { | ||
| await expect(loadTestPatch(text)).rejects.toThrow( | ||
| "No recognizable file changes in patch input", | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| test.each(["", " \n\t\r\n"])("preserves intentionally empty input (%s)", async (text) => { | ||
| expect((await loadTestPatch(text)).changeset.files).toHaveLength(0); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ["unified", textPatch], | ||
| ["traditional", "--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-before\n+after\n"], | ||
| [ | ||
| "mail envelope", | ||
| "From 0123456789 Mon Sep 17 00:00:00 2001\nSubject: [PATCH] Update\n\n" + | ||
| textPatch + | ||
| "-- \n2.50.0\n", | ||
| ], | ||
| ["mode only", "diff --git a/a.txt b/a.txt\nold mode 100644\nnew mode 100755\n"], | ||
| [ | ||
| "rename only", | ||
| "diff --git a/a.txt b/b.txt\nsimilarity index 100%\nrename from a.txt\nrename to b.txt\n", | ||
| ], | ||
| [ | ||
| "empty added file", | ||
| "diff --git a/empty.txt b/empty.txt\nnew file mode 100644\nindex 0000000..e69de29\n", | ||
| ], | ||
| [ | ||
| "binary", | ||
| "diff --git a/a.bin b/a.bin\nindex 1234567..7654321 100644\nBinary files a/a.bin and b/a.bin differ\n", | ||
| ], | ||
| [ | ||
| "ANSI colored", | ||
| textPatch | ||
| .replace("-before", "\u001b[31m-before\u001b[0m") | ||
| .replace("+after", "\u001b[32m+after\u001b[0m"), | ||
| ], | ||
| ])("preserves supported %s patches", async (_name, text) => { | ||
| expect((await loadTestPatch(text)).changeset.files).toHaveLength(1); | ||
| }); | ||
| }); |
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
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,70 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test"; | ||
| import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join, resolve } from "node:path"; | ||
|
|
||
| const entrypoint = resolve("packages/hunk/src/main.tsx"); | ||
| const tempDirs: string[] = []; | ||
|
|
||
| afterEach(() => { | ||
| for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| /** Spawn the real CLI without a terminal so validation must precede renderer takeover. */ | ||
| function runTestPatch(args: readonly string[], input = "") { | ||
| return Bun.spawnSync([process.execPath, "run", entrypoint, "patch", ...args, "--no-extensions"], { | ||
| stdin: Buffer.from(input), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| // Bound exit regressions at the process level; a synchronous spawn blocks test timers. | ||
| timeout: 10_000, | ||
| killSignal: "SIGKILL", | ||
| env: { ...process.env, HUNK_DISABLE_UPDATE_NOTICE: "1" }, | ||
| }); | ||
|
benvinegar marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| describe("patch input CLI contract", () => { | ||
| test.each([{ args: [] }, { args: ["-"] }])( | ||
| "rejects arbitrary nonempty stdin (%j)", | ||
| ({ args }) => { | ||
| const result = runTestPatch(args, "not a patch\n"); | ||
| expect(result.exitCode).toBe(1); | ||
| expect(Buffer.from(result.stderr).toString()).toContain( | ||
| "No recognizable file changes in patch input (stdin patch)", | ||
| ); | ||
| expect(Buffer.from(result.stderr).toString()).toContain( | ||
| "check the command producing the patch", | ||
| ); | ||
| expect(Buffer.from(result.stdout).toString()).not.toContain("\u001b[?1049h"); | ||
| }, | ||
| ); | ||
|
|
||
| test("rejects arbitrary nonempty patch files", () => { | ||
| const dir = mkdtempSync(join(tmpdir(), "hunk-invalid-patch-test-")); | ||
| tempDirs.push(dir); | ||
| const file = join(dir, "invalid.patch"); | ||
| writeFileSync(file, "not a patch\n"); | ||
| const result = runTestPatch([file]); | ||
| expect(result.exitCode).toBe(1); | ||
| expect(Buffer.from(result.stderr).toString()).toContain( | ||
| "No recognizable file changes in patch input", | ||
| ); | ||
| expect(Buffer.from(result.stderr).toString()).toContain("invalid.patch"); | ||
| }); | ||
|
|
||
| test("accepts empty stdin", () => { | ||
| const result = runTestPatch([], ""); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(Buffer.from(result.stderr).toString()).toBe(""); | ||
| }); | ||
|
|
||
| test("accepts a mail-enveloped textual patch", () => { | ||
| const result = runTestPatch( | ||
| [], | ||
| "From 0123456789 Mon Sep 17 00:00:00 2001\nSubject: [PATCH] Update\n\ndiff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-before\n+after\n", | ||
| ); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(Buffer.from(result.stderr).toString()).toBe(""); | ||
| expect(Buffer.from(result.stdout).toString()).toContain("after"); | ||
| }); | ||
| }); | ||
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,34 @@ | ||
| import { afterEach, expect, setDefaultTimeout, test } from "bun:test"; | ||
| import { writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { createPtyHarness } from "./harness"; | ||
|
|
||
| const harness = createPtyHarness(); | ||
|
|
||
| setDefaultTimeout(20_000); | ||
|
|
||
| afterEach(() => { | ||
| harness.cleanup(); | ||
| }); | ||
|
|
||
| test("invalid patch files report failure and exit even after probing terminal colors", async () => { | ||
| const fixture = harness.createSidebarJumpRepoFixture(); | ||
| const patch = join(fixture.dir, "invalid.patch"); | ||
| writeFileSync(patch, "not a patch\n"); | ||
| const command = harness.buildHunkCommand(["patch", patch, "--no-extensions"]); | ||
| const session = await harness.launchShellCommand({ | ||
| command: `${command}; result=$?; printf '\\nPATCH_EXIT:%s\\n' "$result"`, | ||
| cwd: fixture.dir, | ||
| cols: 120, | ||
| rows: 24, | ||
| }); | ||
|
|
||
| try { | ||
| const frame = await session.waitForText(/PATCH_EXIT:1/, { timeout: 5_000 }); | ||
| expect(frame).toContain("No recognizable file changes in patch input"); | ||
| expect(frame).not.toContain("No files match"); | ||
| expect(session.getRawOutput()).not.toContain("\u001b[?1049h"); | ||
| } finally { | ||
| session.close(); | ||
| } | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.