Skip to content

Commit

Permalink
feat: add test for plaintext lockfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jbergstroem committed Dec 29, 2024
1 parent 4401c23 commit af9f6d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 45 additions & 0 deletions test/cli/install/bun-lock.test.ts
Original file line number Diff line number Diff line change
@@ -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`,
);
});
4 changes: 2 additions & 2 deletions test/cli/install/bun-lockb.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit af9f6d2

Please sign in to comment.