From 6a1e1e0c716c9d08d6a6bacf37fb63fb457cd684 Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Thu, 24 Oct 2024 14:18:33 -0400 Subject: [PATCH] Add doc and clean up tests Signed-off-by: Timothy Johnson --- .../cmd/install/install.handler.unit.test.ts | 80 +++++-------------- .../cmd/update/update.handler.unit.test.ts | 18 ++--- .../src/plugins/doc/INpmRegistryInfo.ts | 7 ++ .../src/plugins/utilities/NpmFunctions.ts | 11 +++ 4 files changed, 43 insertions(+), 73 deletions(-) diff --git a/packages/imperative/src/imperative/__tests__/plugins/cmd/install/install.handler.unit.test.ts b/packages/imperative/src/imperative/__tests__/plugins/cmd/install/install.handler.unit.test.ts index 2e4bcdad4..dbd84c1ba 100644 --- a/packages/imperative/src/imperative/__tests__/plugins/cmd/install/install.handler.unit.test.ts +++ b/packages/imperative/src/imperative/__tests__/plugins/cmd/install/install.handler.unit.test.ts @@ -44,7 +44,6 @@ import { PMFConstants } from "../../../../src/plugins/utilities/PMFConstants"; import { TextUtils } from "../../../../../utilities"; import { NpmRegistryUtils } from "../../../../src/plugins/utilities/NpmFunctions"; import * as spawn from "cross-spawn"; -import { INpmRegistryInfo } from "../../../../src/plugins/doc/INpmRegistryInfo"; describe("Plugin Management Facility install handler", () => { @@ -132,16 +131,17 @@ describe("Plugin Management Facility install handler", () => { */ const wasInstallCallValid = ( packageLocation: string, - registry: INpmRegistryInfo, - installFromFile = false + registry: string, + installFromFile = false, + extraNpmArgs = {} ) => { if (installFromFile) { expect(mocks.install).toHaveBeenCalledWith( - packageLocation, registry, true + packageLocation, { location: registry, npmArgs: { registry, ...extraNpmArgs } }, true ); } else { expect(mocks.install).toHaveBeenCalledWith( - packageLocation, registry + packageLocation, { location: registry, npmArgs: { registry, ...extraNpmArgs } } ); } }; @@ -207,14 +207,8 @@ describe("Plugin Management Facility install handler", () => { wasGetRegistryCalled(); expect(mocks.install).toHaveBeenCalledTimes(2); - wasInstallCallValid(`${fileJson.a.package}@${fileJson.a.version}`, { - location: packageRegistry, - npmArgs: { registry: packageRegistry } - }, true); - wasInstallCallValid(fileJson.plugin2.package, { - location: packageRegistry2, - npmArgs: { registry: packageRegistry2 } - }, true); + wasInstallCallValid(`${fileJson.a.package}@${fileJson.a.version}`, packageRegistry, true); + wasInstallCallValid(fileJson.plugin2.package, packageRegistry2, true); expect(mocks.runValidatePlugin).toHaveBeenCalledTimes(2); expect(mocks.runValidatePlugin).toHaveBeenCalledWith("a"); @@ -261,10 +255,7 @@ describe("Plugin Management Facility install handler", () => { wasGetRegistryCalled(); // Check that install worked as expected - wasInstallCallValid(params.arguments.plugin[0], { - location: packageRegistry, - npmArgs: { registry: packageRegistry } - }); + wasInstallCallValid(params.arguments.plugin[0], packageRegistry); // Check that success is output wasInstallSuccessful(params); @@ -280,10 +271,7 @@ describe("Plugin Management Facility install handler", () => { await handler.process(params as IHandlerParameters); // Validate the call to install with specified plugin and registry - wasInstallCallValid(params.arguments.plugin[0], { - location: params.arguments.registry, - npmArgs: { registry: params.arguments.registry } - }); + wasInstallCallValid(params.arguments.plugin[0], params.arguments.registry); wasInstallSuccessful(params); }); @@ -303,10 +291,7 @@ describe("Plugin Management Facility install handler", () => { wasNpmLoginCallValid(packageRegistry); // Check that install worked as expected - wasInstallCallValid(params.arguments.plugin[0], { - location: params.arguments.registry, - npmArgs: { registry: params.arguments.registry } - }); + wasInstallCallValid(params.arguments.plugin[0], params.arguments.registry); // Check that success is output wasInstallSuccessful(params); @@ -325,13 +310,9 @@ describe("Plugin Management Facility install handler", () => { // Validate that install was called with each of these values expect(mocks.install).toHaveBeenCalledTimes(params.arguments.plugin.length); - const registryInfo: INpmRegistryInfo = { - location: packageRegistry, - npmArgs: { registry: packageRegistry } - }; - wasInstallCallValid(params.arguments.plugin[0], registryInfo); - wasInstallCallValid(params.arguments.plugin[1], registryInfo); - wasInstallCallValid(params.arguments.plugin[2], registryInfo); + wasInstallCallValid(params.arguments.plugin[0], packageRegistry); + wasInstallCallValid(params.arguments.plugin[1], packageRegistry); + wasInstallCallValid(params.arguments.plugin[2], packageRegistry); wasInstallSuccessful(params); }); @@ -417,10 +398,7 @@ describe("Plugin Management Facility install handler", () => { expect(e).toBeUndefined(); } - wasInstallCallValid("@public/sample1", { - location: packageRegistry, - npmArgs: { registry: packageRegistry, "@public:registry": "publicRegistryUrl" } - }); + wasInstallCallValid("@public/sample1", packageRegistry, false, { "@public:registry": "publicRegistryUrl" }); }); it("should handle installed plugins via project/directory", async () => { const handler = new InstallHandler(); @@ -435,10 +413,7 @@ describe("Plugin Management Facility install handler", () => { expect(e).toBeUndefined(); } - wasInstallCallValid("path/to/dir", { - location: "path/to/dir", - npmArgs: { registry: packageRegistry } - }); + wasInstallCallValid("path/to/dir", "path/to/dir", false, { registry: packageRegistry }); }); it("should handle installed plugins via tarball file", async () => { const handler = new InstallHandler(); @@ -453,10 +428,7 @@ describe("Plugin Management Facility install handler", () => { expect(e).toBeUndefined(); } - wasInstallCallValid("path/to/dir/file.tgz", { - location: "path/to/dir/file.tgz", - npmArgs: { registry: packageRegistry } - }); + wasInstallCallValid("path/to/dir/file.tgz", "path/to/dir/file.tgz", false, { registry: packageRegistry }); }); it("should handle multiple installed plugins via tarball, directory, and registry", async () => { const handler = new InstallHandler(); @@ -474,21 +446,9 @@ describe("Plugin Management Facility install handler", () => { } expect(mocks.install).toHaveBeenCalledTimes(params.arguments.plugin.length); - wasInstallCallValid("@public/sample1", { - location: packageRegistry, - npmArgs: { registry: packageRegistry, "@public:registry": "publicRegistryUrl" } - }); - wasInstallCallValid("@private/sample1", { - location: packageRegistry, - npmArgs: { registry: packageRegistry, "@private:registry": "privateRegistryUrl" } - }); - wasInstallCallValid("path/to/dir", { - location: "path/to/dir", - npmArgs: { registry: packageRegistry } - }); - wasInstallCallValid("path/to/dir/file.tgz", { - location: "path/to/dir/file.tgz", - npmArgs: { registry: packageRegistry } - }); + wasInstallCallValid("@public/sample1", packageRegistry, false, { "@public:registry": "publicRegistryUrl" }); + wasInstallCallValid("@private/sample1", packageRegistry, false, { "@private:registry": "privateRegistryUrl" }); + wasInstallCallValid("path/to/dir", "path/to/dir", false, { registry: packageRegistry }); + wasInstallCallValid("path/to/dir/file.tgz", "path/to/dir/file.tgz", false, { registry: packageRegistry }); }); }); diff --git a/packages/imperative/src/imperative/__tests__/plugins/cmd/update/update.handler.unit.test.ts b/packages/imperative/src/imperative/__tests__/plugins/cmd/update/update.handler.unit.test.ts index e3c7922ff..cd13230b3 100644 --- a/packages/imperative/src/imperative/__tests__/plugins/cmd/update/update.handler.unit.test.ts +++ b/packages/imperative/src/imperative/__tests__/plugins/cmd/update/update.handler.unit.test.ts @@ -25,7 +25,6 @@ import { NpmRegistryUtils } from "../../../../src/plugins/utilities/NpmFunctions import * as childProcess from "child_process"; import * as jsonfile from "jsonfile"; import * as npmInterface from "../../../../src/plugins/utilities/npm-interface"; -import { INpmRegistryInfo } from "../../../../src/plugins/doc/INpmRegistryInfo"; describe("Plugin Management Facility update handler", () => { @@ -58,7 +57,6 @@ describe("Plugin Management Facility update handler", () => { jest.spyOn(Logger, "getImperativeLogger").mockReturnValue(new Logger(new Console())); mocks.execSyncSpy.mockReturnValue(packageRegistry); mocks.readFileSyncSpy.mockReturnValue({}); - NpmRegistryUtils.npmLogin(packageRegistry); }); /** @@ -88,10 +86,10 @@ describe("Plugin Management Facility update handler", () => { */ const wasUpdateCallValid = ( packageNameParm: string, - registry: INpmRegistryInfo + registry: string ) => { expect(mocks.updateSpy).toHaveBeenCalledWith( - packageNameParm, registry + packageNameParm, { location: registry, npmArgs: { registry } } ); }; @@ -151,16 +149,14 @@ describe("Plugin Management Facility update handler", () => { const params = getIHandlerParametersObject(); params.arguments.plugin = pluginName; params.arguments.registry = packageRegistry; + params.arguments.login = true; await handler.process(params as IHandlerParameters); // Validate the call to login wasNpmLoginCallValid(packageRegistry); wasWriteFileSyncValid(PMFConstants.instance.PLUGIN_JSON, fileJson); - wasUpdateCallValid(packageName, { - location: packageRegistry, - npmArgs: { registry: packageRegistry } - }); + wasUpdateCallValid(packageName, packageRegistry); wasUpdateSuccessful(params); }); @@ -186,12 +182,8 @@ describe("Plugin Management Facility update handler", () => { await handler.process(params as IHandlerParameters); // Validate the call to login - wasNpmLoginCallValid(packageRegistry); wasWriteFileSyncValid(PMFConstants.instance.PLUGIN_JSON, fileJson); - wasUpdateCallValid(resolveVal, { - location: packageRegistry, - npmArgs: { registry: packageRegistry } - }); + wasUpdateCallValid(resolveVal, packageRegistry); wasUpdateSuccessful(params); }); }); diff --git a/packages/imperative/src/imperative/src/plugins/doc/INpmRegistryInfo.ts b/packages/imperative/src/imperative/src/plugins/doc/INpmRegistryInfo.ts index 0e0ff9c3e..220f675a5 100644 --- a/packages/imperative/src/imperative/src/plugins/doc/INpmRegistryInfo.ts +++ b/packages/imperative/src/imperative/src/plugins/doc/INpmRegistryInfo.ts @@ -15,6 +15,13 @@ import { INpmInstallArgs } from "./INpmInstallArgs"; * Location info for an npm package. */ export interface INpmRegistryInfo { + /** + * The origin of npm package (registry URL or absolute path) + */ location: string; + + /** + * Defines npm config values to pass to `npm install` command + */ npmArgs: Partial; } diff --git a/packages/imperative/src/imperative/src/plugins/utilities/NpmFunctions.ts b/packages/imperative/src/imperative/src/plugins/utilities/NpmFunctions.ts index 840cdfbc6..849cd4a6f 100644 --- a/packages/imperative/src/imperative/src/plugins/utilities/NpmFunctions.ts +++ b/packages/imperative/src/imperative/src/plugins/utilities/NpmFunctions.ts @@ -74,6 +74,11 @@ export async function getPackageInfo(pkgSpec: string): Promise<{ name: string, v } export class NpmRegistryUtils { + /** + * Get the registry to install to. + * @param userRegistry Registry override specified on the command line + * @return {string} + */ public static getRegistry(userRegistry?: string): string { if (userRegistry != null) return userRegistry; const execOutput = ExecUtils.spawnAndGetOutput(npmCmd, ["config", "get", "registry"]); @@ -97,6 +102,12 @@ export class NpmRegistryUtils { ); } + /** + * Get package location and npm registry args for installing it. + * @param packageInfo Plugin name or object from plugins.json + * @param userRegistry Registry override specified on the command line + * @returns Location info for npm package to be installed + */ public static buildRegistryInfo(packageInfo: string | IPluginJsonObject, userRegistry?: string): INpmRegistryInfo { const packageName = typeof packageInfo === "string" ? packageInfo : packageInfo.package; const packageScope = packageName.startsWith("@") ? packageName.split("/")[0] : undefined;