-
Notifications
You must be signed in to change notification settings - Fork 5k
shell: accept -- end-of-options delimiter in builtins
#33995
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
Open
robobun
wants to merge
5
commits into
main
Choose a base branch
from
farm/e4dff553/shell-double-dash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
863f00c
shell: accept `--` end-of-options delimiter in builtins
robobun 98f79cf
[autofix.ci] apply automated fixes
autofix-ci[bot] 46cc3eb
shell: share the leading `--` check via Builtin::operand_start()
robobun 183ca3f
shell: handle `--` in pwd, exit, export too
robobun 695a6ed
test(shell): make the export -- test distinguish fixed vs unfixed
robobun 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
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 |
|---|---|---|
|
|
@@ -89,6 +89,9 @@ impl Seq { | |
| idx += 1; | ||
| continue; | ||
| } | ||
| if arg == b"--" { | ||
| idx += 1; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
|
|
||
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,192 @@ | ||
| // POSIX Utility Syntax Guideline 10: `--` ends option parsing; any following | ||
| // arguments are operands even if they begin with `-`. | ||
| import { $ } from "bun"; | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { createTestBuilder } from "../test_builder"; | ||
| import { sortedShellOutput } from "../util"; | ||
| const TestBuilder = createTestBuilder(import.meta.path); | ||
|
|
||
| $.nothrow(); | ||
|
|
||
| describe("-- end-of-options delimiter", () => { | ||
| describe("rm", () => { | ||
| TestBuilder.command`touch a; rm -- a` | ||
| .ensureTempDir() | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .doesNotExist("a") | ||
| .runAsTest("rm -- file"); | ||
|
|
||
| TestBuilder.command`touch a; rm -v -- a` | ||
| .ensureTempDir() | ||
| .stdout("a\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .doesNotExist("a") | ||
| .runAsTest("rm -v -- file applies flag before --"); | ||
|
|
||
| TestBuilder.command`touch ./-f; rm -- -f` | ||
| .ensureTempDir() | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .doesNotExist("-f") | ||
| .runAsTest("rm -- -f treats -f as an operand"); | ||
|
|
||
| TestBuilder.command`rm --` | ||
| .ensureTempDir() | ||
| .stderr("usage: rm [-f | -i] [-dIPRrvWx] file ...\n unlink [--] file\n") | ||
| .exitCode(1) | ||
| .runAsTest("rm -- with no operands shows usage"); | ||
| }); | ||
|
|
||
| describe("mv", () => { | ||
| TestBuilder.command`echo hi > a; mv -- a b` | ||
| .ensureTempDir() | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .doesNotExist("a") | ||
| .fileEquals("b", "hi\n") | ||
| .runAsTest("mv -- src dst"); | ||
|
|
||
| TestBuilder.command`echo hi > ./-n; mv -- -n out` | ||
| .ensureTempDir() | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .doesNotExist("-n") | ||
| .fileEquals("out", "hi\n") | ||
| .runAsTest("mv -- -n out treats -n as an operand"); | ||
|
|
||
| TestBuilder.command`mv -- a` | ||
| .ensureTempDir() | ||
| .stderr("usage: mv [-f | -i | -n] [-hv] source target\n mv [-f | -i | -n] [-v] source ... directory\n") | ||
| .exitCode(1) | ||
| .runAsTest("mv -- with one operand shows usage"); | ||
| }); | ||
|
|
||
| describe("mkdir", () => { | ||
| TestBuilder.command`mkdir -- d; ls` | ||
| .ensureTempDir() | ||
| .stdout("d\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("mkdir -- dir"); | ||
|
|
||
| TestBuilder.command`mkdir -p -- a/b; ls a` | ||
| .ensureTempDir() | ||
| .stdout("b\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("mkdir -p -- nested applies flag before --"); | ||
|
|
||
| TestBuilder.command`mkdir -- -p; ls` | ||
| .ensureTempDir() | ||
| .stdout("-p\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("mkdir -- -p treats -p as an operand"); | ||
| }); | ||
|
|
||
| describe("touch", () => { | ||
| TestBuilder.command`touch -- t; ls` | ||
| .ensureTempDir() | ||
| .stdout("t\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("touch -- file"); | ||
|
|
||
| TestBuilder.command`touch -- -a; ls` | ||
| .ensureTempDir() | ||
| .stdout("-a\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("touch -- -a treats -a as an operand"); | ||
| }); | ||
|
|
||
| describe("ls", () => { | ||
| TestBuilder.command`touch x; ls --` | ||
| .ensureTempDir() | ||
| .stdout("x\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("ls -- with no operands lists cwd"); | ||
|
|
||
| TestBuilder.command`touch a b; ls -- a b` | ||
| .ensureTempDir() | ||
| .stdout(str => expect(sortedShellOutput(str)).toEqual(["a", "b"])) | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("ls -- file file"); | ||
|
|
||
| TestBuilder.command`touch ./-a; ls -- -a` | ||
| .ensureTempDir() | ||
| .stdout("-a\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("ls -- -a treats -a as an operand"); | ||
| }); | ||
|
|
||
| describe("seq", () => { | ||
| TestBuilder.command`seq -- 2`.stdout("1\n2\n").stderr("").exitCode(0).runAsTest("seq -- 2"); | ||
|
|
||
| TestBuilder.command`seq -s , -- 3`.stdout("1,2,3,").stderr("").exitCode(0).runAsTest("seq -s , -- 3"); | ||
|
|
||
| TestBuilder.command`seq --` | ||
| .stderr("usage: seq [-w] [-f format] [-s string] [-t string] [first [incr]] last\n") | ||
| .exitCode(1) | ||
| .runAsTest("seq -- with no operands shows usage"); | ||
| }); | ||
|
|
||
| describe("cd", () => { | ||
| TestBuilder.command`mkdir sub; cd -- sub && echo ok` | ||
| .ensureTempDir() | ||
| .stdout("ok\n") | ||
| .stderr("") | ||
| .exitCode(0) | ||
| .runAsTest("cd -- dir"); | ||
|
|
||
| TestBuilder.command`cd -- && echo ok`.stdout("ok\n").stderr("").exitCode(0).runAsTest("cd -- with no operand"); | ||
| }); | ||
|
|
||
| describe("basename", () => { | ||
| TestBuilder.command`basename -- /a/b`.stdout("b\n").stderr("").exitCode(0).runAsTest("basename -- path"); | ||
|
|
||
| TestBuilder.command`basename --` | ||
| .stderr("usage: basename string\n") | ||
| .exitCode(1) | ||
| .runAsTest("basename -- with no operand shows usage"); | ||
| }); | ||
|
|
||
| describe("dirname", () => { | ||
| TestBuilder.command`dirname -- /a/b`.stdout("/a\n").stderr("").exitCode(0).runAsTest("dirname -- path"); | ||
|
|
||
| TestBuilder.command`dirname --` | ||
| .stderr("usage: dirname string\n") | ||
| .exitCode(1) | ||
| .runAsTest("dirname -- with no operand shows usage"); | ||
| }); | ||
|
|
||
| describe("which", () => { | ||
| TestBuilder.command`which -- bun_nope_not_a_thing` | ||
| .stdout(str => { | ||
| expect(str).not.toContain("--"); | ||
| expect(str).toContain("bun_nope_not_a_thing not found\n"); | ||
| }) | ||
| .stderr("") | ||
| .exitCode(1) | ||
| .runAsTest("which -- name does not treat -- as an operand"); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| describe("yes", () => { | ||
| test("yes -- outputs 'y'", async () => { | ||
| const buffer = Buffer.alloc(6); | ||
| await $`yes -- > ${buffer}`; | ||
| expect(buffer.toString()).toEqual("y\ny\ny\n"); | ||
| }); | ||
|
|
||
| test("yes -- -n outputs '-n'", async () => { | ||
| const buffer = Buffer.alloc(6); | ||
| await $`yes -- -n > ${buffer}`; | ||
| expect(buffer.toString()).toEqual("-n\n-n\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.
Uh oh!
There was an error while loading. Please reload this page.