Fix npm-spec plugin installs when npm pack output is empty (#21039)

* fix(plugins): recover npm pack archive when stdout is empty

* test(plugins): create npm pack archive in metadata mock

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
This commit is contained in:
graysurf
2026-02-27 11:00:24 +08:00
committed by GitHub
parent 9d52dcf1f4
commit 7aa233790b
3 changed files with 133 additions and 13 deletions

View File

@@ -1,5 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import { expect } from "vitest";
import type { SpawnResult } from "../process/exec.js";
import type { CommandOptions, SpawnResult } from "../process/exec.js";
import { expectSingleNpmInstallIgnoreScriptsCall } from "./exec-assertions.js";
export type InstallResultLike = {
@@ -40,10 +42,31 @@ export async function expectUnsupportedNpmSpec(
}
export function mockNpmPackMetadataResult(
run: { mockResolvedValue: (value: SpawnResult) => unknown },
run: {
mockImplementation: (
implementation: (
argv: string[],
optionsOrTimeout: number | CommandOptions,
) => Promise<SpawnResult>,
) => unknown;
},
metadata: NpmPackMetadata,
) {
run.mockResolvedValue(createSuccessfulSpawnResult(JSON.stringify([metadata])));
run.mockImplementation(async (argv, optionsOrTimeout) => {
if (argv[0] !== "npm" || argv[1] !== "pack") {
throw new Error(`unexpected command: ${argv.join(" ")}`);
}
const cwd =
typeof optionsOrTimeout === "object" && optionsOrTimeout !== null
? optionsOrTimeout.cwd
: undefined;
if (cwd) {
fs.writeFileSync(path.join(cwd, metadata.filename), "");
}
return createSuccessfulSpawnResult(JSON.stringify([metadata]));
});
}
export function expectIntegrityDriftRejected(params: {