diff options
author | 2023-07-11 19:14:34 -0700 | |
---|---|---|
committer | 2023-07-11 19:14:34 -0700 | |
commit | cbb88672f217a90db1aa1eb29cd92d5d9035b22b (patch) | |
tree | 43a00501f3cde495967e116f0b660777051551f8 /test/cli/install | |
parent | 1f900cff453700b19bca2acadfe26da4468c1282 (diff) | |
parent | 34b0e7a2bbd8bf8097341cdb0075d0908283e834 (diff) | |
download | bun-cbb88672f217a90db1aa1eb29cd92d5d9035b22b.tar.gz bun-cbb88672f217a90db1aa1eb29cd92d5d9035b22b.tar.zst bun-cbb88672f217a90db1aa1eb29cd92d5d9035b22b.zip |
Merge branch 'main' into jarred/esm-conditionsjarred/esm-conditions
Diffstat (limited to 'test/cli/install')
-rw-r--r-- | test/cli/install/bun-add.test.ts | 55 | ||||
-rw-r--r-- | test/cli/install/bun-install.test.ts | 216 | ||||
-rw-r--r-- | test/cli/install/bun-run.test.ts | 172 | ||||
-rw-r--r-- | test/cli/install/bunx.test.ts | 72 |
4 files changed, 433 insertions, 82 deletions
diff --git a/test/cli/install/bun-add.test.ts b/test/cli/install/bun-add.test.ts index 79804f0e0..9dd38c8cd 100644 --- a/test/cli/install/bun-add.test.ts +++ b/test/cli/install/bun-add.test.ts @@ -303,6 +303,61 @@ it("should add dependency with capital letters", async () => { await access(join(package_dir, "bun.lockb")); }); +it("should add exact version", async () => { + const urls: string[] = []; + setHandler(dummyRegistry(urls)); + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ + name: "foo", + version: "0.0.1", + }), + ); + const { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "add", "--exact", "BaR"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + expect(stderr).toBeDefined(); + const err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(stdout).toBeDefined(); + const out = await new Response(stdout).text(); + expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([ + "", + " installed BaR@0.0.2", + "", + "", + " 1 packages installed", + ]); + expect(await exited).toBe(0); + expect(urls.sort()).toEqual([`${root_url}/BaR`, `${root_url}/BaR-0.0.2.tgz`]); + expect(requested).toBe(2); + expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "BaR"]); + expect(await readdirSorted(join(package_dir, "node_modules", "BaR"))).toEqual(["package.json"]); + expect(await file(join(package_dir, "node_modules", "BaR", "package.json")).json()).toEqual({ + name: "bar", + version: "0.0.2", + }); + expect(await file(join(package_dir, "package.json")).text()).toEqual( + JSON.stringify( + { + name: "foo", + version: "0.0.1", + dependencies: { + BaR: "0.0.2", + }, + }, + null, + 2, + ), + ); + await access(join(package_dir, "bun.lockb")); +}); + it("should add dependency with specified semver", async () => { const urls: string[] = []; setHandler( diff --git a/test/cli/install/bun-install.test.ts b/test/cli/install/bun-install.test.ts index f44dc5a7e..f2483c8d2 100644 --- a/test/cli/install/bun-install.test.ts +++ b/test/cli/install/bun-install.test.ts @@ -1,7 +1,7 @@ import { file, listen, Socket, spawn } from "bun"; import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from "bun:test"; import { bunExe, bunEnv as env } from "harness"; -import { access, mkdir, readlink, rm, writeFile } from "fs/promises"; +import { access, mkdir, readlink, realpath, rm, writeFile } from "fs/promises"; import { join } from "path"; import { dummyAfterAll, @@ -292,6 +292,50 @@ it("should handle workspaces", async () => { await access(join(package_dir, "bun.lockb")); }); +it("should handle `workspace:` specifier", async () => { + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ + name: "Foo", + version: "0.0.1", + dependencies: { + Bar: "workspace:path/to/bar", + }, + }), + ); + await mkdir(join(package_dir, "path", "to", "bar"), { recursive: true }); + await writeFile( + join(package_dir, "path", "to", "bar", "package.json"), + JSON.stringify({ + name: "Bar", + version: "0.0.2", + }), + ); + const { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "install"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + expect(stderr).toBeDefined(); + const err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(stdout).toBeDefined(); + const out = await new Response(stdout).text(); + expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([ + " + Bar@workspace:path/to/bar", + "", + " 1 packages installed", + ]); + expect(await exited).toBe(0); + expect(requested).toBe(0); + expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "Bar"]); + expect(await readlink(join(package_dir, "node_modules", "Bar"))).toBe(join("..", "path", "to", "bar")); + await access(join(package_dir, "bun.lockb")); +}); + it("should handle workspaces with packages array", async () => { await writeFile( join(package_dir, "package.json"), @@ -4241,6 +4285,55 @@ it("should handle --cwd", async () => { }); }); +it("should handle --frozen-lockfile", async () => { + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ name: "foo", version: "0.0.1", dependencies: { bar: "0.0.2" } }), + ); + + const { stderr, exited } = spawn({ + cmd: [bunExe(), "install", "--frozen-lockfile"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + expect(stderr).toBeDefined(); + const err = await new Response(stderr).text(); + expect(err).toContain("error: lockfile had changes, but lockfile is frozen"); + expect(await exited).toBe(1); +}); + +it("should handle frozenLockfile in config file", async () => { + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ name: "foo", version: "0.0.1", dependencies: { bar: "0.0.2" } }), + ); + await writeFile( + join(package_dir, "bunfig.toml"), + ` +[install] +frozenLockfile = true +`, + ); + + const { stderr, exited } = spawn({ + cmd: [bunExe(), "install"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + expect(stderr).toBeDefined(); + const err = await new Response(stderr).text(); + expect(err).toContain("error: lockfile had changes, but lockfile is frozen"); + expect(await exited).toBe(1); +}); + it("should perform bin-linking across multiple dependencies", async () => { const foo_package = JSON.stringify({ name: "foo", @@ -4259,7 +4352,11 @@ it("should perform bin-linking across multiple dependencies", async () => { cache = false `, ); - const { stdout, stderr, exited } = spawn({ + const { + stdout: stdout1, + stderr: stderr1, + exited: exited1, + } = spawn({ cmd: [bunExe(), "install"], cwd: package_dir, stdout: null, @@ -4267,13 +4364,13 @@ cache = false stderr: "pipe", env, }); - expect(stderr).toBeDefined(); - const err = await new Response(stderr).text(); - expect(err).toContain("Saved lockfile"); - expect(err).not.toContain("error:"); - expect(stdout).toBeDefined(); - const out = await new Response(stdout).text(); - expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([ + expect(stderr1).toBeDefined(); + const err1 = await new Response(stderr1).text(); + expect(err1).toContain("Saved lockfile"); + expect(err1).not.toContain("error:"); + expect(stdout1).toBeDefined(); + const out1 = await new Response(stdout1).text(); + expect(out1.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([ " + conditional-type-checks@1.0.6", " + prettier@2.8.8", " + tsd@0.22.0", @@ -4281,7 +4378,7 @@ cache = false "", " 119 packages installed", ]); - expect(await exited).toBe(0); + expect(await exited1).toBe(0); expect(await readdirSorted(package_dir)).toEqual(["bun.lockb", "bunfig.toml", "node_modules", "package.json"]); expect(await file(join(package_dir, "package.json")).text()).toEqual(foo_package); expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([ @@ -4394,4 +4491,101 @@ cache = false "tsd", "tsserver", ]); -}, 10000); + // Perform `bun install --production` with lockfile from before + await rm(join(package_dir, "node_modules"), { force: true, recursive: true }); + const { + stdout: stdout2, + stderr: stderr2, + exited: exited2, + } = spawn({ + cmd: [bunExe(), "install", "--production"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + expect(stderr2).toBeDefined(); + const err2 = await new Response(stderr2).text(); + expect(err2).not.toContain("Saved lockfile"); + expect(err2).not.toContain("error:"); + expect(stdout2).toBeDefined(); + const out2 = await new Response(stdout2).text(); + expect(out2.replace(/\[[0-9\.]+m?s\]/, "[]").split(/\r?\n/)).toEqual(["[] done", ""]); + expect(await exited2).toBe(0); + expect(await readdirSorted(package_dir)).toEqual(["bun.lockb", "bunfig.toml", "node_modules", "package.json"]); + expect(await file(join(package_dir, "package.json")).text()).toEqual(foo_package); + expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([]); +}, 20000); + +it("should handle trustedDependencies", async () => { + const scripts = { + preinstall: `${bunExe()} echo.js preinstall`, + install: `${bunExe()} echo.js install`, + postinstall: `${bunExe()} echo.js postinstall`, + preprepare: `${bunExe()} echo.js preprepare`, + prepare: `${bunExe()} echo.js prepare`, + postprepare: `${bunExe()} echo.js postprepare`, + }; + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ + name: "foo", + version: "0.1.0", + dependencies: { + bar: "file:./bar", + moo: "file:./moo", + }, + trustedDependencies: ["moo"], + }), + ); + await mkdir(join(package_dir, "bar")); + const bar_package = JSON.stringify({ + name: "bar", + version: "0.2.0", + scripts, + }); + await writeFile(join(package_dir, "bar", "package.json"), bar_package); + await writeFile(join(package_dir, "bar", "echo.js"), "console.log(`bar|${process.argv[2]}|${import.meta.dir}`);"); + await mkdir(join(package_dir, "moo")); + const moo_package = JSON.stringify({ + name: "moo", + version: "0.3.0", + scripts, + }); + await writeFile(join(package_dir, "moo", "package.json"), moo_package); + await writeFile(join(package_dir, "moo", "echo.js"), "console.log(`moo|${process.argv[2]}|${import.meta.dir}`);"); + const { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "install"], + cwd: package_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + expect(stderr).toBeDefined(); + const err = await new Response(stderr).text(); + expect(err).toContain("Saved lockfile"); + expect(stdout).toBeDefined(); + const out = await new Response(stdout).text(); + const moo_dir = await realpath(join(package_dir, "node_modules", "moo")); + expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([ + `moo|preinstall|${moo_dir}`, + " + bar@bar", + " + moo@moo", + `moo|install|${moo_dir}`, + `moo|postinstall|${moo_dir}`, + `moo|preprepare|${moo_dir}`, + `moo|prepare|${moo_dir}`, + `moo|postprepare|${moo_dir}`, + "", + " 2 packages installed", + ]); + expect(await exited).toBe(0); + expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "bar", "moo"]); + expect(await readdirSorted(join(package_dir, "node_modules", "bar"))).toEqual(["echo.js", "package.json"]); + expect(await file(join(package_dir, "node_modules", "bar", "package.json")).text()).toEqual(bar_package); + expect(await readdirSorted(join(package_dir, "node_modules", "moo"))).toEqual(["echo.js", "package.json"]); + expect(await file(join(package_dir, "node_modules", "moo", "package.json")).text()).toEqual(moo_package); + await access(join(package_dir, "bun.lockb")); +}); diff --git a/test/cli/install/bun-run.test.ts b/test/cli/install/bun-run.test.ts new file mode 100644 index 000000000..95f33ebb8 --- /dev/null +++ b/test/cli/install/bun-run.test.ts @@ -0,0 +1,172 @@ +import { file, spawn } from "bun"; +import { afterEach, beforeEach, expect, it } from "bun:test"; +import { bunExe, bunEnv as env } from "harness"; +import { mkdtemp, realpath, rm, writeFile } from "fs/promises"; +import { tmpdir } from "os"; +import { join } from "path"; +import { readdirSorted } from "./dummy.registry"; + +let run_dir: string; + +beforeEach(async () => { + run_dir = await realpath(await mkdtemp(join(tmpdir(), "bun-run.test"))); +}); +afterEach(async () => { + await rm(run_dir, { force: true, recursive: true }); +}); + +it("should download dependency to run local file", async () => { + await writeFile( + join(run_dir, "test.js"), + ` +const { minify } = require("uglify-js@3.17.4"); + +console.log(minify("print(6 * 7)").code); + `, + ); + const { + stdout: stdout1, + stderr: stderr1, + exited: exited1, + } = spawn({ + cmd: [bunExe(), "run", "test.js"], + cwd: run_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env: { + ...env, + BUN_INSTALL_CACHE_DIR: join(run_dir, ".cache"), + }, + }); + expect(stderr1).toBeDefined(); + const err1 = await new Response(stderr1).text(); + expect(err1).toBe(""); + expect(await readdirSorted(run_dir)).toEqual([".cache", "test.js"]); + expect(await readdirSorted(join(run_dir, ".cache"))).toContain("uglify-js"); + expect(await readdirSorted(join(run_dir, ".cache", "uglify-js"))).toEqual(["3.17.4"]); + expect(stdout1).toBeDefined(); + const out1 = await new Response(stdout1).text(); + expect(out1.split(/\r?\n/)).toEqual(["print(42);", ""]); + expect(await exited1).toBe(0); + // Perform `bun test.js` with cached dependencies + const { + stdout: stdout2, + stderr: stderr2, + exited: exited2, + } = spawn({ + cmd: [bunExe(), "test.js"], + cwd: run_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env: { + ...env, + BUN_INSTALL_CACHE_DIR: join(run_dir, ".cache"), + }, + }); + expect(stderr2).toBeDefined(); + const err2 = await new Response(stderr2).text(); + expect(err2).toBe(""); + expect(await readdirSorted(run_dir)).toEqual([".cache", "test.js"]); + expect(await readdirSorted(join(run_dir, ".cache"))).toContain("uglify-js"); + expect(await readdirSorted(join(run_dir, ".cache", "uglify-js"))).toEqual(["3.17.4"]); + expect(stdout2).toBeDefined(); + const out2 = await new Response(stdout2).text(); + expect(out2.split(/\r?\n/)).toEqual(["print(42);", ""]); + expect(await exited2).toBe(0); +}); + +it("should download dependencies to run local file", async () => { + await writeFile( + join(run_dir, "test.js"), + ` +import { file } from "bun"; +import decompress from "decompress@4.2.1"; + +const buffer = await file("${join(import.meta.dir, "baz-0.0.3.tgz")}").arrayBuffer(); +for (const entry of await decompress(Buffer.from(buffer))) { + console.log(\`\${entry.type}: \${entry.path}\`); +} + `, + ); + const { + stdout: stdout1, + stderr: stderr1, + exited: exited1, + } = spawn({ + cmd: [bunExe(), "test.js"], + cwd: run_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env: { + ...env, + BUN_INSTALL_CACHE_DIR: join(run_dir, ".cache"), + }, + }); + expect(stderr1).toBeDefined(); + const err1 = await new Response(stderr1).text(); + expect(err1).toBe(""); + expect(await readdirSorted(run_dir)).toEqual([".cache", "test.js"]); + expect(await readdirSorted(join(run_dir, ".cache"))).toContain("decompress"); + expect(await readdirSorted(join(run_dir, ".cache", "decompress"))).toEqual(["4.2.1"]); + expect(await readdirSorted(join(run_dir, ".cache", "decompress", "4.2.1"))).toEqual([ + "index.js", + "license", + "package.json", + "readme.md", + ]); + expect(await file(join(run_dir, ".cache", "decompress", "4.2.1", "index.js")).text()).toContain( + "\nmodule.exports = ", + ); + expect(stdout1).toBeDefined(); + const out1 = await new Response(stdout1).text(); + expect(out1.split(/\r?\n/)).toEqual([ + "directory: package/", + "file: package/index.js", + "file: package/package.json", + "", + ]); + expect(await exited1).toBe(0); + // Perform `bun run test.js` with cached dependencies + const { + stdout: stdout2, + stderr: stderr2, + exited: exited2, + } = spawn({ + cmd: [bunExe(), "run", "test.js"], + cwd: run_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env: { + ...env, + BUN_INSTALL_CACHE_DIR: join(run_dir, ".cache"), + }, + }); + expect(stderr2).toBeDefined(); + const err2 = await new Response(stderr2).text(); + expect(err2).toBe(""); + expect(await readdirSorted(run_dir)).toEqual([".cache", "test.js"]); + expect(await readdirSorted(join(run_dir, ".cache"))).toContain("decompress"); + expect(await readdirSorted(join(run_dir, ".cache", "decompress"))).toEqual(["4.2.1"]); + expect(await readdirSorted(join(run_dir, ".cache", "decompress", "4.2.1"))).toEqual([ + "index.js", + "license", + "package.json", + "readme.md", + ]); + expect(await file(join(run_dir, ".cache", "decompress", "4.2.1", "index.js")).text()).toContain( + "\nmodule.exports = ", + ); + expect(stdout2).toBeDefined(); + const out2 = await new Response(stdout2).text(); + expect(out2.split(/\r?\n/)).toEqual([ + "directory: package/", + "file: package/index.js", + "file: package/package.json", + "", + ]); + expect(await exited2).toBe(0); +}); diff --git a/test/cli/install/bunx.test.ts b/test/cli/install/bunx.test.ts index 87ad2f8b4..70d7aac29 100644 --- a/test/cli/install/bunx.test.ts +++ b/test/cli/install/bunx.test.ts @@ -1,7 +1,6 @@ import { spawn } from "bun"; import { afterEach, beforeEach, expect, it } from "bun:test"; import { bunExe, bunEnv as env } from "harness"; -import { realpathSync } from "fs"; import { mkdtemp, realpath, rm, writeFile } from "fs/promises"; import { tmpdir } from "os"; import { join } from "path"; @@ -10,7 +9,7 @@ import { readdirSorted } from "./dummy.registry"; let x_dir: string; beforeEach(async () => { - x_dir = realpathSync(await mkdtemp(join(tmpdir(), "bun-x.test"))); + x_dir = await realpath(await mkdtemp(join(tmpdir(), "bun-x.test"))); }); afterEach(async () => { await rm(x_dir, { force: true, recursive: true }); @@ -110,75 +109,6 @@ it("should work for @scoped packages", async () => { expect(await cached.exited).toBe(0); }); -it("should download dependency to run local file", async () => { - await writeFile( - join(x_dir, "test.js"), - ` -const { minify } = require("uglify-js@3.17.4"); - -console.log(minify("print(6 * 7)").code); -`, - ); - const { stdout, stderr, exited } = spawn({ - cmd: [bunExe(), "test.js"], - cwd: x_dir, - stdout: null, - stdin: "pipe", - stderr: "pipe", - env: { - ...env, - BUN_INSTALL_CACHE_DIR: join(x_dir, ".cache"), - }, - }); - expect(stderr).toBeDefined(); - const err = await new Response(stderr).text(); - expect(err).toBe(""); - expect(stdout).toBeDefined(); - const out = await new Response(stdout).text(); - expect(out.split(/\r?\n/)).toEqual(["print(42);", ""]); - expect(await exited).toBe(0); - expect(await readdirSorted(x_dir)).toEqual([".cache", "test.js"]); -}); - -it("should download dependencies to run local file", async () => { - await writeFile( - join(x_dir, "test.js"), - ` -import { file } from "bun"; -import decompress from "decompress@4.2.1"; - -const buffer = await file("${join(import.meta.dir, "baz-0.0.3.tgz")}").arrayBuffer(); -for (const entry of await decompress(Buffer.from(buffer))) { - console.log(\`\${entry.type}: \${entry.path}\`); -} -`, - ); - const { stdout, stderr, exited } = spawn({ - cmd: [bunExe(), "test.js"], - cwd: x_dir, - stdout: null, - stdin: "pipe", - stderr: "pipe", - env: { - ...env, - BUN_INSTALL_CACHE_DIR: join(x_dir, ".cache"), - }, - }); - expect(stderr).toBeDefined(); - const err = await new Response(stderr).text(); - expect(err).toBe(""); - expect(stdout).toBeDefined(); - const out = await new Response(stdout).text(); - expect(out.split(/\r?\n/)).toEqual([ - "directory: package/", - "file: package/index.js", - "file: package/package.json", - "", - ]); - expect(await exited).toBe(0); - expect(await readdirSorted(x_dir)).toEqual([".cache", "test.js"]); -}); - it("should execute from current working directory", async () => { await writeFile( join(x_dir, "test.js"), |