|
1 | | -import { describe, expect, test } from "bun:test" |
| 1 | +import { describe, expect, spyOn, test } from "bun:test" |
2 | 2 | import fs from "fs/promises" |
3 | 3 | import path from "path" |
| 4 | +import { BunProc } from "../src/bun" |
| 5 | +import { PackageRegistry } from "../src/bun/registry" |
| 6 | +import { Global } from "../src/global" |
| 7 | +import { Process } from "../src/util/process" |
4 | 8 |
|
5 | 9 | describe("BunProc registry configuration", () => { |
6 | 10 | test("should not contain hardcoded registry parameters", async () => { |
@@ -51,3 +55,83 @@ describe("BunProc registry configuration", () => { |
51 | 55 | } |
52 | 56 | }) |
53 | 57 | }) |
| 58 | + |
| 59 | +describe("BunProc install pinning", () => { |
| 60 | + test("uses pinned cache without touching registry", async () => { |
| 61 | + const pkg = `pin-test-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}` |
| 62 | + const ver = "1.2.3" |
| 63 | + const mod = path.join(Global.Path.cache, "node_modules", pkg) |
| 64 | + const data = path.join(Global.Path.cache, "package.json") |
| 65 | + |
| 66 | + await fs.mkdir(mod, { recursive: true }) |
| 67 | + await Bun.write(path.join(mod, "package.json"), JSON.stringify({ name: pkg, version: ver }, null, 2)) |
| 68 | + |
| 69 | + const src = await fs.readFile(data, "utf8").catch(() => "") |
| 70 | + const json = src ? ((JSON.parse(src) as { dependencies?: Record<string, string> }) ?? {}) : {} |
| 71 | + const deps = json.dependencies ?? {} |
| 72 | + deps[pkg] = ver |
| 73 | + await Bun.write(data, JSON.stringify({ ...json, dependencies: deps }, null, 2)) |
| 74 | + |
| 75 | + const stale = spyOn(PackageRegistry, "isOutdated").mockImplementation(async () => { |
| 76 | + throw new Error("unexpected registry check") |
| 77 | + }) |
| 78 | + const run = spyOn(Process, "run").mockImplementation(async () => { |
| 79 | + throw new Error("unexpected process.run") |
| 80 | + }) |
| 81 | + |
| 82 | + try { |
| 83 | + const out = await BunProc.install(pkg, ver) |
| 84 | + expect(out).toBe(mod) |
| 85 | + expect(stale).not.toHaveBeenCalled() |
| 86 | + expect(run).not.toHaveBeenCalled() |
| 87 | + } finally { |
| 88 | + stale.mockRestore() |
| 89 | + run.mockRestore() |
| 90 | + |
| 91 | + await fs.rm(mod, { recursive: true, force: true }) |
| 92 | + const end = await fs |
| 93 | + .readFile(data, "utf8") |
| 94 | + .then((item) => JSON.parse(item) as { dependencies?: Record<string, string> }) |
| 95 | + .catch(() => undefined) |
| 96 | + if (end?.dependencies) { |
| 97 | + delete end.dependencies[pkg] |
| 98 | + await Bun.write(data, JSON.stringify(end, null, 2)) |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + test("passes --ignore-scripts when requested", async () => { |
| 104 | + const pkg = `ignore-test-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}` |
| 105 | + const ver = "4.5.6" |
| 106 | + const mod = path.join(Global.Path.cache, "node_modules", pkg) |
| 107 | + const data = path.join(Global.Path.cache, "package.json") |
| 108 | + |
| 109 | + const run = spyOn(Process, "run").mockImplementation(async () => ({ |
| 110 | + code: 0, |
| 111 | + stdout: Buffer.alloc(0), |
| 112 | + stderr: Buffer.alloc(0), |
| 113 | + })) |
| 114 | + |
| 115 | + try { |
| 116 | + await fs.rm(mod, { recursive: true, force: true }) |
| 117 | + await BunProc.install(pkg, ver, { ignoreScripts: true }) |
| 118 | + |
| 119 | + expect(run).toHaveBeenCalled() |
| 120 | + const call = run.mock.calls[0]?.[0] |
| 121 | + expect(call).toContain("--ignore-scripts") |
| 122 | + expect(call).toContain(`${pkg}@${ver}`) |
| 123 | + } finally { |
| 124 | + run.mockRestore() |
| 125 | + await fs.rm(mod, { recursive: true, force: true }) |
| 126 | + |
| 127 | + const end = await fs |
| 128 | + .readFile(data, "utf8") |
| 129 | + .then((item) => JSON.parse(item) as { dependencies?: Record<string, string> }) |
| 130 | + .catch(() => undefined) |
| 131 | + if (end?.dependencies) { |
| 132 | + delete end.dependencies[pkg] |
| 133 | + await Bun.write(data, JSON.stringify(end, null, 2)) |
| 134 | + } |
| 135 | + } |
| 136 | + }) |
| 137 | +}) |
0 commit comments