aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGravatar Ashcon Partovi <ashcon@partovi.net> 2023-10-03 17:39:45 -0700
committerGravatar GitHub <noreply@github.com> 2023-10-03 17:39:45 -0700
commit9308e1bf09637a75d4c99ddd056addd7fbb00f6f (patch)
tree4e9f1c737269e5d0e308942cf8678494166b0956 /test
parentaa8ccce952f5dfa91c84f282b5abbb184c17dfc2 (diff)
downloadbun-9308e1bf09637a75d4c99ddd056addd7fbb00f6f.tar.gz
bun-9308e1bf09637a75d4c99ddd056addd7fbb00f6f.tar.zst
bun-9308e1bf09637a75d4c99ddd056addd7fbb00f6f.zip
Support `bun run --if-present` (#6248)
* Support --if-present Closes #5670 * More robust tests, handle more cases
Diffstat (limited to 'test')
-rw-r--r--test/cli/run/if-present.test.ts119
-rw-r--r--test/harness.ts2
2 files changed, 120 insertions, 1 deletions
diff --git a/test/cli/run/if-present.test.ts b/test/cli/run/if-present.test.ts
new file mode 100644
index 000000000..120970b46
--- /dev/null
+++ b/test/cli/run/if-present.test.ts
@@ -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);
+ });
+});
diff --git a/test/harness.ts b/test/harness.ts
index 16d04c1b4..77b4b4f43 100644
--- a/test/harness.ts
+++ b/test/harness.ts
@@ -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");