Skip to content

Commit

Permalink
shell: implement 'true' and 'false' builtin commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Mar 30, 2024
1 parent b8389f3 commit 232c849
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/shell/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3926,6 +3926,8 @@ pub const Interpreter = struct {
mv: Mv,
ls: Ls,
exit: Exit,
true: True,
false: False,
};

const Result = @import("../result.zig").Result;
Expand All @@ -3943,6 +3945,8 @@ pub const Interpreter = struct {
mv,
ls,
exit,
true,
false,

pub fn parentType(this: Kind) type {
_ = this;
Expand All @@ -3962,6 +3966,8 @@ pub const Interpreter = struct {
.mv => "usage: mv [-f | -i | -n] [-hv] source target\n mv [-f | -i | -n] [-v] source ... directory\n",
.ls => "usage: ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]\n",
.exit => "usage: exit [n]\n",
.true => "",
.false => "",
};
}

Expand All @@ -3979,6 +3985,8 @@ pub const Interpreter = struct {
.mv => "mv",
.ls => "ls",
.exit => "exit",
.true => "true",
.false => "false",
};
}

Expand Down Expand Up @@ -4141,6 +4149,8 @@ pub const Interpreter = struct {
.mv => this.callImplWithType(Mv, Ret, "mv", field, args_),
.ls => this.callImplWithType(Ls, Ret, "ls", field, args_),
.exit => this.callImplWithType(Exit, Ret, "exit", field, args_),
.true => this.callImplWithType(True, Ret, "true", field, args_),
.false => this.callImplWithType(False, Ret, "false", field, args_),
};
}

Expand Down Expand Up @@ -8710,6 +8720,40 @@ pub const Interpreter = struct {
_ = this;
}
};

pub const True = struct {
bltn: *Builtin,

pub fn start(this: *@This()) Maybe(void) {
this.bltn.done(0);
return Maybe(void).success;
}

pub fn onIOWriterChunk(_: *@This(), _: usize, _: ?JSC.SystemError) void {
// no IO is done
}

pub fn deinit(this: *@This()) void {
_ = this;
}
};

pub const False = struct {
bltn: *Builtin,

pub fn start(this: *@This()) Maybe(void) {
this.bltn.done(1);
return Maybe(void).success;
}

pub fn onIOWriterChunk(_: *@This(), _: usize, _: ?JSC.SystemError) void {
// no IO is done
}

pub fn deinit(this: *@This()) void {
_ = this;
}
};
};

/// This type is reference counted, but deinitialization is queued onto the event loop
Expand Down Expand Up @@ -9719,6 +9763,8 @@ pub const IOWriterChildPtr = struct {
Interpreter.Builtin.Touch.ShellTouchOutputTask,
Interpreter.Builtin.Cat,
Interpreter.Builtin.Exit,
Interpreter.Builtin.True,
Interpreter.Builtin.False,
shell.subproc.PipeReader.CapturedWriter,
});

Expand Down
13 changes: 13 additions & 0 deletions test/js/bun/shell/commands/false.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";

describe("false", async () => {
TestBuilder.command`false`.exitCode(1).runAsTest("works");

TestBuilder.command`false 3 5`.exitCode(1).runAsTest("works with arguments");

TestBuilder.command`false --help`.exitCode(1).runAsTest("works with --help");

TestBuilder.command`false --version`.exitCode(1).runAsTest("works with --version");
});
13 changes: 13 additions & 0 deletions test/js/bun/shell/commands/true.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { $ } from "bun";
import { describe, test, expect } from "bun:test";
import { TestBuilder } from "../test_builder";

describe("true", async () => {
TestBuilder.command`true`.exitCode(0).runAsTest("works");

TestBuilder.command`true 3 5`.exitCode(0).runAsTest("works with arguments");

TestBuilder.command`true --help`.exitCode(0).runAsTest("works with --help");

TestBuilder.command`true --version`.exitCode(0).runAsTest("works with --version");
});

0 comments on commit 232c849

Please sign in to comment.