diff options
Diffstat (limited to 'test/cli/run')
-rw-r--r-- | test/cli/run/empty-file.js | 0 | ||||
-rw-r--r-- | test/cli/run/empty-file.test.ts | 15 | ||||
-rw-r--r-- | test/cli/run/env.test.ts | 10 | ||||
-rw-r--r-- | test/cli/run/if-present.test.ts | 119 | ||||
-rw-r--r-- | test/cli/run/log-test.test.ts | 2 | ||||
-rw-r--r-- | test/cli/run/require-cache-bug-5188.js | 22 | ||||
-rw-r--r-- | test/cli/run/require-cache-fixture-b.cjs | 7 | ||||
-rw-r--r-- | test/cli/run/require-cache-fixture.cjs | 10 | ||||
-rw-r--r-- | test/cli/run/require-cache.test.js | 15 | ||||
-rw-r--r-- | test/cli/run/require-cache.test.ts | 27 | ||||
-rw-r--r-- | test/cli/run/run-cjs.test.ts | 2 | ||||
-rw-r--r-- | test/cli/run/run-extensionless.test.ts | 31 | ||||
-rw-r--r-- | test/cli/run/run-unicode.test.ts | 21 |
13 files changed, 264 insertions, 17 deletions
diff --git a/test/cli/run/empty-file.js b/test/cli/run/empty-file.js new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/test/cli/run/empty-file.js diff --git a/test/cli/run/empty-file.test.ts b/test/cli/run/empty-file.test.ts new file mode 100644 index 000000000..f29162bf7 --- /dev/null +++ b/test/cli/run/empty-file.test.ts @@ -0,0 +1,15 @@ +import { it, expect } from "bun:test"; +import { bunEnv, bunExe } from "harness"; +import { join } from "path"; + +it("should execute empty scripts", () => { + const { stdout, stderr, exitCode } = Bun.spawnSync({ + cmd: [bunExe(), "run", "--bun", join(import.meta.dir, "empty-file.js")], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + expect(stdout.toString()).toBeEmpty(); + expect(stderr.toString()).toBeEmpty(); + expect(exitCode).toBe(0); +}); diff --git a/test/cli/run/env.test.ts b/test/cli/run/env.test.ts index 3ed300477..1c2d51e8e 100644 --- a/test/cli/run/env.test.ts +++ b/test/cli/run/env.test.ts @@ -385,6 +385,16 @@ test(".env with zero length strings", () => { expect(stdout).toBe("||0|0"); }); +test("process with zero length environment variable", () => { + const dir = tempDirWithFiles("process-issue-zerolength", { + "index.ts": "console.log(`'${process.env.TEST_ENV_VAR}'`);", + }); + const { stdout } = bunRun(`${dir}/index.ts`, { + TEST_ENV_VAR: "", + }); + expect(stdout).toBe("''"); +}); + test(".env in a folder doesn't throw an error", () => { const dir = tempDirWithFiles("dotenv-issue-3670", { ".env": { 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/cli/run/log-test.test.ts b/test/cli/run/log-test.test.ts index 55c7d6d86..eb15dd592 100644 --- a/test/cli/run/log-test.test.ts +++ b/test/cli/run/log-test.test.ts @@ -32,7 +32,7 @@ it("should log .env by default", async () => { env: bunEnv, }); - expect(stderr?.toString().includes(".env")).toBe(true); + expect(stderr?.toString().includes(".env")).toBe(false); }); function writeDirectoryTree(base: string, paths: Record<string, any>) { diff --git a/test/cli/run/require-cache-bug-5188.js b/test/cli/run/require-cache-bug-5188.js new file mode 100644 index 000000000..5b14e6b4c --- /dev/null +++ b/test/cli/run/require-cache-bug-5188.js @@ -0,0 +1,22 @@ +const { strictEqual, ok } = require("assert"); + +Loader.registry.set("bad", { evaluated: false }); +strictEqual(require.cache.bad, undefined); +ok(!Object.hasOwn(require.cache, "bad")); +ok(!Object.getOwnPropertyNames(require.cache).includes("bad")); + +// hard to simplify this test case, but importing this would cause require.cache.extract to be set +require("msgpackr-extract"); + +strictEqual(require.cache["extract"], undefined); +ok(!("extract" in require.cache)); // https://github.com/oven-sh/bun/issues/5898 +ok(!Object.hasOwnProperty.call(require.cache, "extract")); +ok(!Object.getOwnPropertyNames(require.cache).includes("extract")); + +for (const key of Object.keys(require.cache)) { + if (!require.cache[key]) { + throw new Error("require.cache has an undefined value that was in it's keys"); + } +} + +console.log("--pass--"); diff --git a/test/cli/run/require-cache-fixture-b.cjs b/test/cli/run/require-cache-fixture-b.cjs index c3f2c8a26..525d5b3db 100644 --- a/test/cli/run/require-cache-fixture-b.cjs +++ b/test/cli/run/require-cache-fixture-b.cjs @@ -6,3 +6,10 @@ if (require.main === module) { console.error(__filename, module.id); throw new Error("require.main === module"); } + +if (module.parent == null || typeof module.parent !== "object") { + console.error(module.parent); + throw new Error("module.parent == null"); +} + +module.exports = { x: module.parent }; diff --git a/test/cli/run/require-cache-fixture.cjs b/test/cli/run/require-cache-fixture.cjs index 69f8cebeb..4efb1cd0b 100644 --- a/test/cli/run/require-cache-fixture.cjs +++ b/test/cli/run/require-cache-fixture.cjs @@ -8,6 +8,11 @@ if (require.main !== module) { throw new Error("require.main !== module"); } +if (module.parent !== null) { + console.error(module.parent); + throw new Error("module.parent !== null"); +} + if (process.mainModule !== module) { console.error(__filename, module.id); throw new Error("process.mainModule !== module"); @@ -25,6 +30,11 @@ if (__dirname !== resolve(module.filename, "../")) { const foo = require("./require-cache-fixture-b.cjs"); +if (foo.x !== module) { + console.error(__filename, foo); + throw new Error("foo !== module"); +} + exports.foo = foo; var res = require.resolve; diff --git a/test/cli/run/require-cache.test.js b/test/cli/run/require-cache.test.js deleted file mode 100644 index e20470f9d..000000000 --- a/test/cli/run/require-cache.test.js +++ /dev/null @@ -1,15 +0,0 @@ -import { test, expect } from "bun:test"; -import { bunEnv, bunExe } from "harness"; -import { join } from "path"; - -// This also tests __dirname and __filename -test("require.cache", () => { - const { stdout, exitCode } = Bun.spawnSync({ - cmd: [bunExe(), "run", join(import.meta.dir, "require-cache-fixture.cjs")], - env: bunEnv, - stderr: "inherit", - }); - - expect(stdout.toString().trim().endsWith("--pass--")).toBe(true); - expect(exitCode).toBe(0); -}); diff --git a/test/cli/run/require-cache.test.ts b/test/cli/run/require-cache.test.ts new file mode 100644 index 000000000..77bf5708c --- /dev/null +++ b/test/cli/run/require-cache.test.ts @@ -0,0 +1,27 @@ +import { test, expect } from "bun:test"; +import { bunEnv, bunExe } from "harness"; +import { join } from "path"; + +// This also tests __dirname and __filename +test("require.cache", () => { + const { stdout, exitCode } = Bun.spawnSync({ + cmd: [bunExe(), "run", join(import.meta.dir, "require-cache-fixture.cjs")], + env: bunEnv, + stderr: "inherit", + }); + + expect(stdout.toString().trim()).toEndWith("--pass--"); + expect(exitCode).toBe(0); +}); + +// https://github.com/oven-sh/bun/issues/5188 +test("require.cache does not include unevaluated modules", () => { + const { stdout, exitCode } = Bun.spawnSync({ + cmd: [bunExe(), "run", join(import.meta.dir, "require-cache-bug-5188.js")], + env: bunEnv, + stderr: "inherit", + }); + + expect(stdout.toString().trim()).toEndWith("--pass--"); + expect(exitCode).toBe(0); +}); diff --git a/test/cli/run/run-cjs.test.ts b/test/cli/run/run-cjs.test.ts index 34c77d106..4c0d37002 100644 --- a/test/cli/run/run-cjs.test.ts +++ b/test/cli/run/run-cjs.test.ts @@ -4,7 +4,7 @@ import { bunEnv, bunExe } from "harness"; import { tmpdir } from "os"; import { join } from "path"; -test.todo("running a commonjs module works", async () => { +test("running a commonjs module works", async () => { const dir = join(realpathSync(tmpdir()), "bun-run-test1"); mkdirSync(dir, { recursive: true }); await Bun.write(join(dir, "index1.js"), "module.exports = 1; console.log('hello world');"); diff --git a/test/cli/run/run-extensionless.test.ts b/test/cli/run/run-extensionless.test.ts new file mode 100644 index 000000000..642c274d9 --- /dev/null +++ b/test/cli/run/run-extensionless.test.ts @@ -0,0 +1,31 @@ +import { expect, test } from "bun:test"; +import { mkdirSync, realpathSync } from "fs"; +import { bunEnv, bunExe } from "harness"; +import { writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; + +test("running extensionless file works", async () => { + const dir = join(realpathSync(tmpdir()), "bun-run-test1"); + mkdirSync(dir, { recursive: true }); + await Bun.write(join(dir, "cool"), "const x: Test = 2; console.log('hello world');"); + let { stdout } = Bun.spawnSync({ + cmd: [bunExe(), join(dir, "./cool")], + cwd: dir, + env: bunEnv, + }); + expect(stdout.toString("utf8")).toEqual("hello world\n"); +}); + +test("running shebang typescript file works", async () => { + const dir = join(realpathSync(tmpdir()), "bun-run-test2"); + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, "cool"), `#!${bunExe()}\nconst x: Test = 2; console.log('hello world');`, { mode: 0o777 }); + + let { stdout } = Bun.spawnSync({ + cmd: [join(dir, "./cool")], + cwd: dir, + env: bunEnv, + }); + expect(stdout.toString("utf8")).toEqual("hello world\n"); +}); diff --git a/test/cli/run/run-unicode.test.ts b/test/cli/run/run-unicode.test.ts new file mode 100644 index 000000000..c1a9c2466 --- /dev/null +++ b/test/cli/run/run-unicode.test.ts @@ -0,0 +1,21 @@ +import { expect, test } from "bun:test"; +import { mkdirSync, realpathSync } from "fs"; +import { bunEnv, bunExe } from "harness"; +import { tmpdir } from "os"; +import { join } from "path"; + +test("running a weird filename works", async () => { + const troll = "💥'\"​\n"; + const dir = join(realpathSync(tmpdir()), "bun-run-test" + troll); + mkdirSync(dir, { recursive: true }); + console.log("dir", dir); + // i this it's possible that the filesystem rejects the path + await Bun.write(join(dir, troll + ".js"), "console.log('hello world');"); + let { stdout } = Bun.spawnSync({ + cmd: [bunExe(), join(dir, troll + ".js")], + cwd: dir, + env: bunEnv, + stdio: ["ignore", "pipe", "inherit"], + }); + expect(stdout.toString("utf8")).toEqual("hello world\n"); +}); |