/** * The built binary refuses an ORM command in a project holding a second * copy of a Prisma package, and runs it where the project's copy is the * CLI's own. No credentials and no management API: the check runs * before anything is read from the project or the platform. */ import { execFile } from "node:child_process"; import { existsSync } from "node:fs"; import { mkdir, mkdtemp, realpath, rm, symlink, writeFile, } from "node:fs/promises"; import { createRequire } from "node:module"; import os from "node:os"; import path from "node:path"; import { promisify } from "node:util"; import { afterEach, describe, expect, it } from "vitest"; import { CLI_BINARY } from "./harness"; const execFileAsync = promisify(execFile); const TOOLCHAIN = "@prisma/orm-toolchain"; const FRAMEWORK = "@prisma/orm-framework"; const requireFromCli = createRequire( path.resolve(import.meta.dirname, "../package.json"), ); const projects: string[] = []; async function makeProject(): Promise<{ dir: string; marker: string }> { const root = await mkdtemp(path.join(os.tmpdir(), "prisma-e2e-second-copy-")); projects.push(root); const dir = path.join(await realpath(root), "app"); await mkdir(path.join(dir, ".git"), { recursive: true }); await mkdir(path.join(dir, "node_modules", "@prisma"), { recursive: true }); const marker = path.join(dir, "config-was-evaluated"); await writeFile( path.join(dir, "prisma.config.ts"), `import { writeFileSync } from "node:fs";\nwriteFileSync(${JSON.stringify(marker)}, "");\nexport default {};\n`, "utf8", ); return { dir, marker }; } async function runMigrationList(cwd: string): Promise<{ readonly exitCode: number; readonly errorCode: string | undefined; }> { const result = await execFileAsync( process.execPath, [CLI_BINARY, "migration", "list", "--json"], { cwd, env: { PATH: process.env.PATH, TMPDIR: process.env.TMPDIR, HOME: cwd, USERPROFILE: cwd, PRISMA_DISABLE_TELEMETRY: "1", PRISMA_SKILLS_CHECK: "0", NO_UPDATE_NOTIFIER: "1", }, timeout: 60_000, }, ).then( ({ stdout }) => ({ exitCode: 0, stdout }), (failure: { code: number; stdout: string }) => ({ exitCode: failure.code, stdout: failure.stdout, }), ); const last = result.stdout .split("\n") .filter((line) => line.trim().startsWith("{")) .map( (line) => JSON.parse(line) as { kind?: string; envelope?: { error?: { code?: string } }; }, ) .reverse() .find((frame) => frame.kind === "result"); return { exitCode: result.exitCode, errorCode: last?.envelope?.error?.code }; } afterEach(async () => { await Promise.all( projects .splice(0) .map((root) => rm(root, { recursive: true, force: true })), ); }); describe("a second copy of a Prisma package", () => { it("runs the command when the project's copies are the CLI's own", async () => { const { dir, marker } = await makeProject(); await Promise.all( [TOOLCHAIN, FRAMEWORK].map(async (name) => symlink( await realpath( path.dirname(requireFromCli.resolve(`${name}/package.json`)), ), path.join(dir, "node_modules", name), "dir", ), ), ); const run = await runMigrationList(dir); expect(run.errorCode).not.toBe("CLI.SECOND_PACKAGE_COPY"); expect(existsSync(marker)).toBe(true); }); it("refuses the command, before reading the config, when the project has its own copy", async () => { const { dir, marker } = await makeProject(); await mkdir(path.join(dir, "node_modules", TOOLCHAIN), { recursive: true }); await writeFile( path.join(dir, "node_modules", TOOLCHAIN, "package.json"), `${JSON.stringify({ name: TOOLCHAIN, version: "8.0.0-rc.24" })}\n`, "utf8", ); const run = await runMigrationList(dir); expect(run.exitCode).toBe(2); expect(run.errorCode).toBe("CLI.SECOND_PACKAGE_COPY"); expect(existsSync(marker)).toBe(false); }); });