Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/runtime/shell/builtin/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ impl Rm {
if flag.len() > 2 && flag[1] == b'-' {
return match flag {
b"--preserve-root" | b"--no-preserve-root" => RmParseFlag::ContinueParsing,
b"--force" => {
opts.force = true;
opts.prompt_behaviour = PromptBehaviour::Never;
RmParseFlag::ContinueParsing
}
b"--recursive" => {
opts.recursive = true;
RmParseFlag::ContinueParsing
Expand Down
40 changes: 40 additions & 0 deletions test/js/bun/shell/commands/rm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,46 @@ foo/
}
});

test("--force is the long form of -f", async () => {
await using tempdir = tempDir("rmlongforce", {
"existent.txt": "",
"after-interactive.txt": "",
"dir/sub/file.txt": "",
"mixed/file.txt": "",
"kept.txt": "",
});
const run = async (cmd: $.ShellPromise) => {
const { stdout, stderr, exitCode } = await cmd.quiet();
return { stdout: stdout.toString(), stderr: stderr.toString(), exitCode };
};
const ok = { stdout: "", stderr: "", exitCode: 0 };

// Nonexistent operand: ignored, same as -f.
expect(await run($`rm --force ${tempdir}/non_existent.txt`)).toEqual(ok);

// Existing operand is still removed.
expect(await run($`rm --force ${tempdir}/existent.txt`)).toEqual(ok);
expect(existsSync(`${tempdir}/existent.txt`)).toBeFalse();

// Like -f, a later --force cancels an earlier -i (which is otherwise rejected).
expect(await run($`rm -i --force ${tempdir}/after-interactive.txt`)).toEqual(ok);
expect(existsSync(`${tempdir}/after-interactive.txt`)).toBeFalse();

// Combined with other long flags, in either order, with missing operands mixed in.
expect(await run($`rm --force --recursive ${tempdir}/dir ${tempdir}/non_existent_dir`)).toEqual(ok);
expect(existsSync(`${tempdir}/dir`)).toBeFalse();
expect(await run($`rm -r --force ${tempdir}/mixed`)).toEqual(ok);
expect(existsSync(`${tempdir}/mixed`)).toBeFalse();

// Only the exact spelling is accepted.
expect(await run($`rm --forc ${tempdir}/kept.txt`)).toEqual({
stdout: "",
stderr: "rm: illegal option -- -\n",
exitCode: 1,
});
expect(existsSync(`${tempdir}/kept.txt`)).toBeTrue();
});

test("dir", async () => {
const files = {
"existent.txt": "",
Expand Down
Loading