aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/install/bun-add.test.ts
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-01-31 23:05:41 +0200
committerGravatar GitHub <noreply@github.com> 2023-01-31 13:05:41 -0800
commit9598868b57e6779c3b51625a851ff8cdfbd25b97 (patch)
tree967bfb4b3cfd105b6a01f059dd2f515e42f32298 /test/bun.js/install/bun-add.test.ts
parent79b32f80fa9be5a6c838088068cc5281859f2671 (diff)
downloadbun-9598868b57e6779c3b51625a851ff8cdfbd25b97.tar.gz
bun-9598868b57e6779c3b51625a851ff8cdfbd25b97.tar.zst
bun-9598868b57e6779c3b51625a851ff8cdfbd25b97.zip
[bun add] various fixes (#1953)
Diffstat (limited to 'test/bun.js/install/bun-add.test.ts')
-rw-r--r--test/bun.js/install/bun-add.test.ts365
1 files changed, 194 insertions, 171 deletions
diff --git a/test/bun.js/install/bun-add.test.ts b/test/bun.js/install/bun-add.test.ts
index 5094e541d..5be122bd0 100644
--- a/test/bun.js/install/bun-add.test.ts
+++ b/test/bun.js/install/bun-add.test.ts
@@ -1,22 +1,8 @@
import { file, spawn } from "bun";
-import {
- afterAll,
- afterEach,
- beforeAll,
- beforeEach,
- expect,
- it,
-} from "bun:test";
+import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from "bun:test";
import { bunExe } from "bunExe";
import { bunEnv as env } from "bunEnv";
-import {
- access,
- mkdir,
- mkdtemp,
- readlink,
- rm,
- writeFile,
-} from "fs/promises";
+import { access, mkdir, mkdtemp, readlink, rm, writeFile } from "fs/promises";
import { join, relative } from "path";
import { tmpdir } from "os";
import {
@@ -47,14 +33,20 @@ afterEach(async () => {
});
it("should add existing package", async () => {
- await writeFile(join(add_dir, "package.json"), JSON.stringify({
- name: "foo",
- version: "0.0.1",
- }));
- await writeFile(join(package_dir, "package.json"), JSON.stringify({
- name: "bar",
- version: "0.0.2",
- }));
+ await writeFile(
+ join(add_dir, "package.json"),
+ JSON.stringify({
+ name: "foo",
+ version: "0.0.1",
+ }),
+ );
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "bar",
+ version: "0.0.2",
+ }),
+ );
const add_path = relative(package_dir, add_dir);
const { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "add", `file:${add_path}`],
@@ -66,11 +58,7 @@ it("should add existing package", async () => {
});
expect(stderr).toBeDefined();
const err = await new Response(stderr).text();
- expect(err.replace(/^(.*?) v[^\n]+/, "$1").split(/\r?\n/)).toEqual([
- "bun add",
- " Saved lockfile",
- "",
- ]);
+ expect(err.replace(/^(.*?) v[^\n]+/, "$1").split(/\r?\n/)).toEqual(["bun add", " 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([
@@ -91,10 +79,13 @@ it("should add existing package", async () => {
});
it("should reject missing package", async () => {
- await writeFile(join(package_dir, "package.json"), JSON.stringify({
- name: "bar",
- version: "0.0.2",
- }));
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "bar",
+ version: "0.0.2",
+ }),
+ );
const add_path = relative(package_dir, add_dir);
const { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "add", `file:${add_path}`],
@@ -122,14 +113,20 @@ it("should reject missing package", async () => {
});
it("should reject invalid path without segfault", async () => {
- await writeFile(join(add_dir, "package.json"), JSON.stringify({
- name: "foo",
- version: "0.0.1",
- }));
- await writeFile(join(package_dir, "package.json"), JSON.stringify({
- name: "bar",
- version: "0.0.2",
- }));
+ await writeFile(
+ join(add_dir, "package.json"),
+ JSON.stringify({
+ name: "foo",
+ version: "0.0.1",
+ }),
+ );
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "bar",
+ version: "0.0.2",
+ }),
+ );
const add_path = relative(package_dir, add_dir);
const { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "add", `file://${add_path}`],
@@ -156,9 +153,9 @@ it("should reject invalid path without segfault", async () => {
});
});
-it("should handle semver-like names", async() => {
+it("should handle semver-like names", async () => {
const urls: string[] = [];
- setHandler(async (request) => {
+ setHandler(async request => {
expect(request.method).toBe("GET");
expect(request.headers.get("accept")).toBe(
"application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*",
@@ -168,18 +165,15 @@ it("should handle semver-like names", async() => {
urls.push(request.url);
return new Response("not to be found", { status: 404 });
});
- await writeFile(join(package_dir, "package.json"), JSON.stringify({
- name: "foo",
- version: "0.0.1",
- }));
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "foo",
+ version: "0.0.1",
+ }),
+ );
const { stdout, stderr, exited } = spawn({
- cmd: [
- bunExe(),
- "add",
- "1.2.3",
- "--config",
- import.meta.dir + "/basic.toml",
- ],
+ cmd: [bunExe(), "add", "1.2.3", "--config", import.meta.dir + "/basic.toml"],
cwd: package_dir,
stdout: null,
stdin: "pipe",
@@ -188,9 +182,7 @@ it("should handle semver-like names", async() => {
});
expect(stderr).toBeDefined();
const err = await new Response(stderr).text();
- expect(err.split(/\r?\n/)).toContain(
- 'error: package "1.2.3" not found localhost/1.2.3 404',
- );
+ expect(err.split(/\r?\n/)).toContain('error: package "1.2.3" not found localhost/1.2.3 404');
expect(stdout).toBeDefined();
expect(await new Response(stdout).text()).toBe("");
expect(await exited).toBe(1);
@@ -204,9 +196,9 @@ it("should handle semver-like names", async() => {
}
});
-it("should handle @scoped names", async() => {
+it("should handle @scoped names", async () => {
const urls: string[] = [];
- setHandler(async (request) => {
+ setHandler(async request => {
expect(request.method).toBe("GET");
expect(request.headers.get("accept")).toBe(
"application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*",
@@ -216,18 +208,15 @@ it("should handle @scoped names", async() => {
urls.push(request.url);
return new Response("not to be found", { status: 404 });
});
- await writeFile(join(package_dir, "package.json"), JSON.stringify({
- name: "foo",
- version: "0.0.1",
- }));
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "foo",
+ version: "0.0.1",
+ }),
+ );
const { stdout, stderr, exited } = spawn({
- cmd: [
- bunExe(),
- "add",
- "@bar/baz",
- "--config",
- import.meta.dir + "/basic.toml",
- ],
+ cmd: [bunExe(), "add", "@bar/baz", "--config", import.meta.dir + "/basic.toml"],
cwd: package_dir,
stdout: null,
stdin: "pipe",
@@ -236,9 +225,7 @@ it("should handle @scoped names", async() => {
});
expect(stderr).toBeDefined();
const err = await new Response(stderr).text();
- expect(err.split(/\r?\n/)).toContain(
- 'error: package "@bar/baz" not found localhost/@bar/baz 404',
- );
+ expect(err.split(/\r?\n/)).toContain('error: package "@bar/baz" not found localhost/@bar/baz 404');
expect(stdout).toBeDefined();
expect(await new Response(stdout).text()).toBe("");
expect(await exited).toBe(1);
@@ -254,11 +241,13 @@ it("should handle @scoped names", async() => {
it("should add dependency with specified semver", async () => {
const urls: string[] = [];
- setHandler(dummyRegistry(urls, "0.0.3", {
- bin: {
- "baz-run": "index.js",
- },
- }));
+ setHandler(
+ dummyRegistry(urls, "0.0.3", {
+ bin: {
+ "baz-run": "index.js",
+ },
+ }),
+ );
await writeFile(
join(package_dir, "package.json"),
JSON.stringify({
@@ -288,26 +277,12 @@ it("should add dependency with specified semver", async () => {
" 1 packages installed",
]);
expect(await exited).toBe(0);
- expect(urls).toEqual([
- `${root_url}/baz`,
- `${root_url}/baz.tgz`,
- ]);
+ expect(urls).toEqual([`${root_url}/baz`, `${root_url}/baz.tgz`]);
expect(requested).toBe(2);
- expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([
- ".bin",
- ".cache",
- "baz",
- ]);
- expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual([
- "baz-run",
- ]);
- expect(await readlink(join(package_dir, "node_modules", ".bin", "baz-run"))).toBe(
- join("..", "baz", "index.js"),
- );
- expect(await readdirSorted(join(package_dir, "node_modules", "baz"))).toEqual([
- "index.js",
- "package.json",
- ]);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".bin", ".cache", "baz"]);
+ expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual(["baz-run"]);
+ expect(await readlink(join(package_dir, "node_modules", ".bin", "baz-run"))).toBe(join("..", "baz", "index.js"));
+ expect(await readdirSorted(join(package_dir, "node_modules", "baz"))).toEqual(["index.js", "package.json"]);
expect(await file(join(package_dir, "node_modules", "baz", "package.json")).json()).toEqual({
name: "baz",
version: "0.0.3",
@@ -327,11 +302,13 @@ it("should add dependency with specified semver", async () => {
it("should add dependency alongside workspaces", async () => {
const urls: string[] = [];
- setHandler(dummyRegistry(urls, "0.0.3", {
- bin: {
- "baz-run": "index.js",
- },
- }));
+ setHandler(
+ dummyRegistry(urls, "0.0.3", {
+ bin: {
+ "baz-run": "index.js",
+ },
+ }),
+ );
await writeFile(
join(package_dir, "package.json"),
JSON.stringify({
@@ -371,30 +348,13 @@ it("should add dependency alongside workspaces", async () => {
" 2 packages installed",
]);
expect(await exited).toBe(0);
- expect(urls).toEqual([
- `${root_url}/baz`,
- `${root_url}/baz.tgz`,
- ]);
+ expect(urls).toEqual([`${root_url}/baz`, `${root_url}/baz.tgz`]);
expect(requested).toBe(2);
- expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([
- ".bin",
- ".cache",
- "bar",
- "baz",
- ]);
- expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual([
- "baz-run",
- ]);
- expect(await readlink(join(package_dir, "node_modules", ".bin", "baz-run"))).toBe(
- join("..", "baz", "index.js"),
- );
- expect(await readlink(join(package_dir, "node_modules", "bar"))).toBe(
- join("..", "packages", "bar"),
- );
- expect(await readdirSorted(join(package_dir, "node_modules", "baz"))).toEqual([
- "index.js",
- "package.json",
- ]);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".bin", ".cache", "bar", "baz"]);
+ expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual(["baz-run"]);
+ expect(await readlink(join(package_dir, "node_modules", ".bin", "baz-run"))).toBe(join("..", "baz", "index.js"));
+ expect(await readlink(join(package_dir, "node_modules", "bar"))).toBe(join("..", "packages", "bar"));
+ expect(await readdirSorted(join(package_dir, "node_modules", "baz"))).toEqual(["index.js", "package.json"]);
expect(await file(join(package_dir, "node_modules", "baz", "package.json")).json()).toEqual({
name: "baz",
version: "0.0.3",
@@ -405,7 +365,7 @@ it("should add dependency alongside workspaces", async () => {
expect(await file(join(package_dir, "package.json")).json()).toEqual({
name: "foo",
version: "0.0.1",
- workspaces: [ "packages/bar" ],
+ workspaces: ["packages/bar"],
dependencies: {
baz: "^0.0.3",
},
@@ -415,11 +375,13 @@ it("should add dependency alongside workspaces", async () => {
it("should add aliased dependency (npm)", async () => {
const urls: string[] = [];
- setHandler(dummyRegistry(urls, "0.0.3", {
- bin: {
- "baz-run": "index.js",
- },
- }));
+ setHandler(
+ dummyRegistry(urls, "0.0.3", {
+ bin: {
+ "baz-run": "index.js",
+ },
+ }),
+ );
await writeFile(
join(package_dir, "package.json"),
JSON.stringify({
@@ -448,26 +410,12 @@ it("should add aliased dependency (npm)", async () => {
" 1 packages installed",
]);
expect(await exited).toBe(0);
- expect(urls).toEqual([
- `${root_url}/baz`,
- `${root_url}/baz.tgz`,
- ]);
+ expect(urls).toEqual([`${root_url}/baz`, `${root_url}/baz.tgz`]);
expect(requested).toBe(2);
- expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([
- ".bin",
- ".cache",
- "bar",
- ]);
- expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual([
- "baz-run",
- ]);
- expect(await readlink(join(package_dir, "node_modules", ".bin", "baz-run"))).toBe(
- join("..", "bar", "index.js"),
- );
- expect(await readdirSorted(join(package_dir, "node_modules", "bar"))).toEqual([
- "index.js",
- "package.json",
- ]);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".bin", ".cache", "bar"]);
+ expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual(["baz-run"]);
+ expect(await readlink(join(package_dir, "node_modules", ".bin", "baz-run"))).toBe(join("..", "bar", "index.js"));
+ expect(await readdirSorted(join(package_dir, "node_modules", "bar"))).toEqual(["index.js", "package.json"]);
expect(await file(join(package_dir, "node_modules", "bar", "package.json")).json()).toEqual({
name: "baz",
version: "0.0.3",
@@ -518,14 +466,8 @@ it("should add aliased dependency (GitHub)", async () => {
expect(await exited).toBe(0);
expect(urls).toEqual([]);
expect(requested).toBe(0);
- expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([
- ".bin",
- ".cache",
- "uglify",
- ]);
- expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual([
- "uglifyjs",
- ]);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".bin", ".cache", "uglify"]);
+ expect(await readdirSorted(join(package_dir, "node_modules", ".bin"))).toEqual(["uglifyjs"]);
expect(await readdirSorted(join(package_dir, "node_modules", ".cache"))).toEqual([
"@GH@mishoo-UglifyJS-e219a9a",
"uglify",
@@ -533,13 +475,7 @@ it("should add aliased dependency (GitHub)", async () => {
expect(await readdirSorted(join(package_dir, "node_modules", ".cache", "uglify"))).toEqual([
"mishoo-UglifyJS-e219a9a",
]);
- expect(await readlink(join(
- package_dir,
- "node_modules",
- ".cache",
- "uglify",
- "mishoo-UglifyJS-e219a9a",
- ))).toBe(
+ expect(await readlink(join(package_dir, "node_modules", ".cache", "uglify", "mishoo-UglifyJS-e219a9a"))).toBe(
join(package_dir, "node_modules", ".cache", "@GH@mishoo-UglifyJS-e219a9a"),
);
expect(await readdirSorted(join(package_dir, "node_modules", "uglify"))).toEqual([
@@ -556,12 +492,7 @@ it("should add aliased dependency (GitHub)", async () => {
"test",
"tools",
]);
- const package_json = await file(join(
- package_dir,
- "node_modules",
- "uglify",
- "package.json",
- )).json();
+ const package_json = await file(join(package_dir, "node_modules", "uglify", "package.json")).json();
expect(package_json.name).toBe("uglify-js");
expect(package_json.version).toBe("3.14.1");
expect(await file(join(package_dir, "package.json")).json()).toEqual({
@@ -573,3 +504,95 @@ it("should add aliased dependency (GitHub)", async () => {
});
await access(join(package_dir, "bun.lockb"));
});
+
+it("should let you add the same package twice", async () => {
+ const urls: string[] = [];
+ setHandler(dummyRegistry(urls, "0.0.3", {}));
+ await writeFile(
+ join(package_dir, "package.json"),
+ JSON.stringify({
+ name: "Foo",
+ version: "0.0.1",
+ dependencies: {},
+ }),
+ );
+ // add as non-dev
+ const {
+ stdout: stdout1,
+ stderr: stderr1,
+ exited: exited1,
+ } = spawn({
+ cmd: [bunExe(), "add", "baz@0.0.3", "--config", import.meta.dir + "/basic.toml"],
+ cwd: package_dir,
+ stdout: null,
+ stdin: "pipe",
+ stderr: "pipe",
+ env,
+ });
+ expect(stderr1).toBeDefined();
+ const err1 = await new Response(stderr1).text();
+ expect(err1).toContain("Saved lockfile");
+ expect(stdout1).toBeDefined();
+ const out1 = await new Response(stdout1).text();
+ expect(out1).toContain("installed baz@0.0.3");
+ expect(out1).toContain("1 packages installed");
+ expect(await exited1).toBe(0);
+ expect(urls).toEqual([`${root_url}/baz`, `${root_url}/baz.tgz`]);
+ expect(requested).toBe(2);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "baz"]);
+ expect(await file(join(package_dir, "node_modules", "baz", "package.json")).json()).toEqual({
+ name: "baz",
+ version: "0.0.3",
+ bin: {
+ "baz-run": "index.js",
+ },
+ });
+ expect(await file(join(package_dir, "package.json")).json()).toEqual({
+ name: "Foo",
+ version: "0.0.1",
+ dependencies: {
+ baz: "0.0.3",
+ },
+ });
+ await access(join(package_dir, "bun.lockb"));
+ // re-add as dev
+ urls.length = 0;
+ const {
+ stdout: stdout2,
+ stderr: stderr2,
+ exited: exited2,
+ } = spawn({
+ cmd: [bunExe(), "add", "baz", "-d", "--config", import.meta.dir + "/basic.toml"],
+ cwd: package_dir,
+ stdout: null,
+ stdin: "pipe",
+ stderr: "pipe",
+ env,
+ });
+ expect(stderr2).toBeDefined();
+ const err2 = await new Response(stderr2).text();
+ expect(err2).toContain("Saved lockfile");
+ expect(stdout2).toBeDefined();
+ const out2 = await new Response(stdout2).text();
+ expect(out2).toContain("installed baz@0.0.3");
+ expect(out2).not.toContain("1 packages installed");
+ expect(await exited2).toBe(0);
+ expect(urls).toEqual([`${root_url}/baz`]);
+ expect(requested).toBe(3);
+ expect(await readdirSorted(join(package_dir, "node_modules"))).toEqual([".cache", "baz"]);
+ expect(await file(join(package_dir, "node_modules", "baz", "package.json")).json()).toEqual({
+ name: "baz",
+ version: "0.0.3",
+ bin: {
+ "baz-run": "index.js",
+ },
+ });
+ expect(await file(join(package_dir, "package.json")).json()).toEqual({
+ name: "Foo",
+ version: "0.0.1",
+ dependencies: {
+ baz: "^0.0.3",
+ },
+ });
+ await access(join(package_dir, "bun.lockb"));
+});