Skip to content

Commit

Permalink
Support bun run --if-present (#6248)
Browse files Browse the repository at this point in the history
* Support --if-present
Closes #5670

* More robust tests, handle more cases
  • Loading branch information
Electroid authored Oct 4, 2023
1 parent aa8ccce commit 9308e1b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub const Arguments = struct {
clap.parseParam("--inspect <STR>? Activate Bun's Debugger") catch unreachable,
clap.parseParam("--inspect-wait <STR>? Activate Bun's Debugger, wait for a connection before executing") catch unreachable,
clap.parseParam("--inspect-brk <STR>? Activate Bun's Debugger, set breakpoint on first line of code and wait") catch unreachable,
clap.parseParam("--if-present Exit if the entrypoint does not exist") catch unreachable,
clap.parseParam("<POS>... ") catch unreachable,
};

Expand Down Expand Up @@ -538,6 +539,7 @@ pub const Arguments = struct {
ctx.preloads = preloads;
}

ctx.runtime_options.if_present = args.flag("--if-present");
ctx.runtime_options.smol = args.flag("--smol");
if (args.option("--inspect")) |inspect_flag| {
ctx.runtime_options.debugger = if (inspect_flag.len == 0)
Expand Down Expand Up @@ -1022,6 +1024,7 @@ pub const Command = struct {
pub const RuntimeOptions = struct {
smol: bool = false,
debugger: Debugger = .{ .unspecified = {} },
if_present: bool = false,
};

pub const Context = struct {
Expand Down Expand Up @@ -1666,6 +1669,10 @@ pub const Command = struct {
Global.exit(1);
}

if (ctx.runtime_options.if_present) {
return;
}

if (was_js_like) {
Output.prettyErrorln("<r><red>error<r><d>:<r> module not found \"<b>{s}<r>\"", .{
ctx.positionals[0],
Expand Down
4 changes: 4 additions & 0 deletions src/cli/run_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,10 @@ pub const RunCommand = struct {
}
}

if (ctx.runtime_options.if_present) {
return true;
}

if (comptime log_errors) {
Output.prettyError("<r><red>error<r><d>:<r> missing script \"<b>{s}<r>\"\n", .{script_name_to_search});
Global.exit(1);
Expand Down
119 changes: 119 additions & 0 deletions test/cli/run/if-present.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { describe, test, expect, beforeAll } from "bun:test";
import { spawnSync } from "bun";
import { bunEnv, bunExe, tempDirWithFiles } from "harness";

let cwd: string;

beforeAll(() => {
cwd = tempDirWithFiles("--if-present", {
"present.js": "console.log('Here!');",
"package.json": JSON.stringify({
"name": "present",
"scripts": {
"present": "echo 'Here!'",
},
}),
});
});

describe("bun", () => {
test("should error with missing script", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "notpresent"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toMatch(/script not found/);
expect(exitCode).toBe(1);
});
test("should error with missing module", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "./notpresent.js"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toMatch(/module not found/);
expect(exitCode).toBe(1);
});
test("should error with missing file", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "/path/to/notpresent.txt"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toMatch(/file not found/);
expect(exitCode).toBe(1);
});
});

describe("bun --if-present", () => {
test("should not error with missing script", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "--if-present", "notpresent"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toBeEmpty();
expect(exitCode).toBe(0);
});
test("should not error with missing module", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "--if-present", "./notpresent.js"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toBeEmpty();
expect(exitCode).toBe(0);
});
test("should not error with missing file", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "--if-present", "/path/to/notpresent.txt"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toBeEmpty();
expect(stderr.toString()).toBeEmpty();
expect(exitCode).toBe(0);
});
test("should run present script", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "run", "present"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toMatch(/Here!/);
expect(stderr.toString()).not.toBeEmpty();
expect(exitCode).toBe(0);
});
test("should run present module", () => {
const { exitCode, stdout, stderr } = spawnSync({
cwd,
cmd: [bunExe(), "run", "present.js"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString()).toMatch(/Here!/);
expect(stderr.toString()).toBeEmpty();
expect(exitCode).toBe(0);
});
});
2 changes: 1 addition & 1 deletion test/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function hideFromStackTrace(block: CallableFunction) {
});
}

export function tempDirWithFiles(basename: string, files: Record<string, string | Record<string, string>>) {
export function tempDirWithFiles(basename: string, files: Record<string, string | Record<string, string>>): string {
var fs = require("fs");
var path = require("path");
var { tmpdir } = require("os");
Expand Down

0 comments on commit 9308e1b

Please sign in to comment.