Skip to content

Commit

Permalink
Add doc and clean up tests
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
  • Loading branch information
t1m0thyj committed Oct 24, 2024
1 parent 9d1c2ce commit 6a1e1e0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {

Expand Down Expand Up @@ -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 } }
);
}
};
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -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);
});
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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 });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {

Expand Down Expand Up @@ -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);
});

/**
Expand Down Expand Up @@ -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 } }
);
};

Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<INpmInstallArgs>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand All @@ -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;
Expand Down

0 comments on commit 6a1e1e0

Please sign in to comment.