From af9f6d24ab8b173695b6b3ca9975ff6854cbe521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Bergstro=CC=88m?= Date: Sun, 29 Dec 2024 10:11:56 +0000 Subject: [PATCH] feat: add test for plaintext lockfiles --- test/cli/install/bun-lock.test.ts | 45 ++++++++++++++++++++++++++++++ test/cli/install/bun-lockb.test.ts | 4 +-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/cli/install/bun-lock.test.ts diff --git a/test/cli/install/bun-lock.test.ts b/test/cli/install/bun-lock.test.ts new file mode 100644 index 00000000000000..b83c0285bc107d --- /dev/null +++ b/test/cli/install/bun-lock.test.ts @@ -0,0 +1,45 @@ +import { spawn } from "bun"; +import { expect, it } from "bun:test"; +import { access, copyFile, open, writeFile } from "fs/promises"; +import { bunExe, bunEnv as env, tmpdirSync } from "harness"; +import { join } from "path"; + +it("should write plaintext lockfiles", async () => { + const package_dir = tmpdirSync(); + + // copy bar-0.0.2.tgz to package_dir + await copyFile(join(__dirname, "bar-0.0.2.tgz"), join(package_dir, "bar-0.0.2.tgz")); + + // Create a simple package.json + await writeFile( + join(package_dir, "package.json"), + JSON.stringify({ + name: "test-package", + version: "1.0.0", + dependencies: { + "dummy-package": "file:./bar-0.0.2.tgz", + }, + }), + ); + + // Run 'bun install' to generate the lockfile + const installResult = spawn({ + cmd: [bunExe(), "install", "--save-text-lockfile"], + cwd: package_dir, + env, + }); + await installResult.exited; + + // Ensure the lockfile was created + await access(join(package_dir, "bun.lock")); + + // Assert that the lockfile has the correct permissions + const file = await open(join(package_dir, "bun.lock"), "r"); + const stat = await file.stat(); + // 0o644 == 33188 + expect(stat.mode).toBe(33188); + + expect(await file.readFile({ encoding: "utf8" })).toEqual( + `{\n \"lockfileVersion\": 0,\n \"workspaces\": {\n \"\": {\n \"dependencies\": {\n \"dummy-package\": \"file:./bar-0.0.2.tgz\",\n },\n },\n },\n \"packages\": {\n \"dummy-package\": [\"bar@./bar-0.0.2.tgz\", {}],\n }\n}\n`, + ); +}); diff --git a/test/cli/install/bun-lockb.test.ts b/test/cli/install/bun-lockb.test.ts index edec4aad6e7cfc..fe593bc8e68b3c 100644 --- a/test/cli/install/bun-lockb.test.ts +++ b/test/cli/install/bun-lockb.test.ts @@ -1,6 +1,6 @@ import { spawn } from "bun"; import { expect, it } from "bun:test"; -import { access, constants, copyFile, open, writeFile } from "fs/promises"; +import { access, copyFile, open, writeFile } from "fs/promises"; import { bunExe, bunEnv as env, tmpdirSync } from "harness"; import { join } from "path"; @@ -31,7 +31,7 @@ it("should not print anything to stderr when running bun.lockb", async () => { await installResult.exited; // Ensure the lockfile was created - await access(join(package_dir, "bun.lockb"), constants.F_OK); + await access(join(package_dir, "bun.lockb")); // Assert that the lockfile has the correct permissions const file = await open(join(package_dir, "bun.lockb"), "r");