Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/cmd/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,50 @@ describe.sequential("install all", () => {
"f-0.0.1/package.oo.yaml",
]));
});

it("should fail with cancel signal", async (ctx) => {
const p = fixture("install_all");

// publish `remote_storage` to registry
{
const remoteStorage = path.join(p, "remote_storage");
await Promise.all([
publish(path.join(remoteStorage, "a-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "b-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "c-0.0.2"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "d-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "e-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "e-0.0.2"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "f-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "e-0.0.3"), ctx.registry.endpoint, "fake-token"),
]);
}

// Copy `local_storage` to distDir
const distDir = await tempDir();
{
const localStorage = path.join(p, "local_storage");
await Promise.all([
copyDir(path.join(localStorage, "c-0.0.1"), path.join(distDir, "c-0.0.1")),
copyDir(path.join(localStorage, "e-0.0.1"), path.join(distDir, "e-0.0.1")),
]);
}

// Copy `entry` to workdir
await copyDir(path.join(p, "entry"), ctx.workdir);

const controller = new AbortController();
controller.abort();

await expect(install({
all: true,
token: "fake-token",
workdir: ctx.workdir,
distDir,
registry: ctx.registry.endpoint,
cancelSignal: controller.signal,
})).rejects.toThrow("This operation was aborted");
});
});

describe.sequential("install deps", () => {
Expand Down Expand Up @@ -559,6 +603,49 @@ describe.sequential("install deps", () => {
},
});
});

it("should fail with cancel signal", async (ctx) => {
const p = fixture("install_deps");

// publish `remote_storage` to registry
{
const remoteStorage = path.join(p, "remote_storage");
await Promise.all([
publish(path.join(remoteStorage, "a-0.0.2"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "b-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "c-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "d-0.0.1"), ctx.registry.endpoint, "fake-token"),
]);
await publish(path.join(remoteStorage, "b-0.0.2"), ctx.registry.endpoint, "fake-token");
}

// Copy `local_storage` to distDir
const distDir = await tempDir();
{
const localStorage = path.join(p, "local_storage");
await copyDir(path.join(localStorage, "a-0.0.1"), path.join(distDir, "a-0.0.1"));
}

// Copy `entry` to workdir
await copyDir(path.join(p, "entry"), ctx.workdir);

const controller = new AbortController();
controller.abort();

await expect(install({
deps: [
{ name: "a" },
{ name: "b" },
{ name: "c", version: "0.0.1" },
],
save: true,
token: "fake-token",
workdir: ctx.workdir,
distDir,
registry: ctx.registry.endpoint,
cancelSignal: controller.signal,
})).rejects.toThrow("This operation was aborted");
});
});

describe.sequential("unknown type", () => {
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {

export interface InstallBasicOptions {
distDir: string;

cancelSignal?: AbortSignal;
}

export interface InstallAllOptions extends InstallBasicOptions {
Expand Down Expand Up @@ -170,6 +172,8 @@ export async function installPackage(options: InstallPackageOptions): Promise<In
distDir: options.distDir,
token: options.token,
registry: options.registry,

cancelSignal: options.cancelSignal,
});

return {
Expand Down Expand Up @@ -215,6 +219,8 @@ export async function installAll(options: InstallAllOptions): Promise<InstallAll
distDir: options.distDir,
token: options.token,
registry: options.registry,

cancelSignal: options.cancelSignal,
});

return {
Expand All @@ -231,6 +237,8 @@ interface _InstallOptions {
alreadyInstalled: Deps;
needInstall: Deps;
registry: string;

cancelSignal?: AbortSignal;
}

async function _install(options: _InstallOptions): Promise<InstallPackageResult["deps"]> {
Expand All @@ -242,6 +250,8 @@ async function _install(options: _InstallOptions): Promise<InstallPackageResult[
env: env(options.registry),
stdout: "inherit",
stderr: "inherit",
cancelSignal: options.cancelSignal,
forceKillAfterDelay: 1000,
})`npm install`;

const info = await transformNodeModules(temp);
Expand Down