1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-06-22 15:34:47 -04:00
parent 3428e72b9a
commit 2dc7700941
7 changed files with 33 additions and 71 deletions

View File

@ -14,6 +14,7 @@ import callForPublicHelmRepositoriesInjectable from "../../renderer/components/+
import showSuccessNotificationInjectable from "../../renderer/components/notifications/show-success-notification.injectable"; import showSuccessNotificationInjectable from "../../renderer/components/notifications/show-success-notification.injectable";
import showErrorNotificationInjectable from "../../renderer/components/notifications/show-error-notification.injectable"; import showErrorNotificationInjectable from "../../renderer/components/notifications/show-error-notification.injectable";
import type { AsyncResult } from "../../common/utils/async-result"; import type { AsyncResult } from "../../common/utils/async-result";
import type { ExecFile } from "../../main/child-process/exec-file.injectable";
import execFileInjectable from "../../main/child-process/exec-file.injectable"; import execFileInjectable from "../../main/child-process/exec-file.injectable";
// TODO: Make tooltips free of side effects by making it deterministic // TODO: Make tooltips free of side effects by making it deterministic
@ -26,10 +27,8 @@ describe("add helm repository from list in preferences", () => {
let showSuccessNotificationMock: jest.Mock; let showSuccessNotificationMock: jest.Mock;
let showErrorNotificationMock: jest.Mock; let showErrorNotificationMock: jest.Mock;
let rendered: RenderResult; let rendered: RenderResult;
let execFileMock: AsyncFnMock< let execFileMock: AsyncFnMock<ExecFile>;
ReturnType<typeof execFileInjectable["instantiate"]> let getActiveHelmRepositoriesMock: AsyncFnMock<() => Promise<AsyncResult<HelmRepo[]>>>;
>;
let getActiveHelmRepositoriesMock: AsyncFnMock<() => AsyncResult<HelmRepo[]>>;
let callForPublicHelmRepositoriesMock: AsyncFnMock<() => Promise<HelmRepo[]>>; let callForPublicHelmRepositoriesMock: AsyncFnMock<() => Promise<HelmRepo[]>>;
beforeEach(async () => { beforeEach(async () => {

View File

@ -15,6 +15,7 @@ import loggerInjectable from "../../common/logger.injectable";
import type { Logger } from "../../common/logger"; import type { Logger } from "../../common/logger";
import callForPublicHelmRepositoriesInjectable from "../../renderer/components/+preferences/kubernetes/helm-charts/adding-of-public-helm-repository/public-helm-repositories/call-for-public-helm-repositories.injectable"; import callForPublicHelmRepositoriesInjectable from "../../renderer/components/+preferences/kubernetes/helm-charts/adding-of-public-helm-repository/public-helm-repositories/call-for-public-helm-repositories.injectable";
import showErrorNotificationInjectable from "../../renderer/components/notifications/show-error-notification.injectable"; import showErrorNotificationInjectable from "../../renderer/components/notifications/show-error-notification.injectable";
import type { ExecFile } from "../../main/child-process/exec-file.injectable";
import execFileInjectable from "../../main/child-process/exec-file.injectable"; import execFileInjectable from "../../main/child-process/exec-file.injectable";
// TODO: Make tooltips free of side effects by making it deterministic // TODO: Make tooltips free of side effects by making it deterministic
@ -26,7 +27,7 @@ describe("listing active helm repositories in preferences", () => {
let applicationBuilder: ApplicationBuilder; let applicationBuilder: ApplicationBuilder;
let rendered: RenderResult; let rendered: RenderResult;
let readYamlFileMock: AsyncFnMock<ReadYamlFile>; let readYamlFileMock: AsyncFnMock<ReadYamlFile>;
let execFileMock: AsyncFnMock<ReturnType<typeof execFileInjectable["instantiate"]>>; let execFileMock: AsyncFnMock<ExecFile>;
let loggerStub: Logger; let loggerStub: Logger;
let showErrorNotificationMock: jest.Mock; let showErrorNotificationMock: jest.Mock;

View File

@ -13,6 +13,7 @@ import getActiveHelmRepositoriesInjectable from "../../main/helm/repositories/ge
import type { HelmRepo } from "../../common/helm/helm-repo"; import type { HelmRepo } from "../../common/helm/helm-repo";
import callForPublicHelmRepositoriesInjectable from "../../renderer/components/+preferences/kubernetes/helm-charts/adding-of-public-helm-repository/public-helm-repositories/call-for-public-helm-repositories.injectable"; import callForPublicHelmRepositoriesInjectable from "../../renderer/components/+preferences/kubernetes/helm-charts/adding-of-public-helm-repository/public-helm-repositories/call-for-public-helm-repositories.injectable";
import type { AsyncResult } from "../../common/utils/async-result"; import type { AsyncResult } from "../../common/utils/async-result";
import type { ExecFile } from "../../main/child-process/exec-file.injectable";
import execFileInjectable from "../../main/child-process/exec-file.injectable"; import execFileInjectable from "../../main/child-process/exec-file.injectable";
// TODO: Make tooltips free of side effects by making it deterministic // TODO: Make tooltips free of side effects by making it deterministic
@ -23,10 +24,8 @@ jest.mock("../../renderer/components/tooltip/withTooltip", () => ({
describe("remove helm repository from list of active repositories in preferences", () => { describe("remove helm repository from list of active repositories in preferences", () => {
let applicationBuilder: ApplicationBuilder; let applicationBuilder: ApplicationBuilder;
let rendered: RenderResult; let rendered: RenderResult;
let getActiveHelmRepositoriesMock: AsyncFnMock<() => AsyncResult<HelmRepo[]>>; let getActiveHelmRepositoriesMock: AsyncFnMock<() => Promise<AsyncResult<HelmRepo[]>>>;
let execFileMock: AsyncFnMock< let execFileMock: AsyncFnMock<ExecFile>;
ReturnType<typeof execFileInjectable["instantiate"]>
>;
beforeEach(async () => { beforeEach(async () => {
applicationBuilder = getApplicationBuilder(); applicationBuilder = getApplicationBuilder();

View File

@ -3,14 +3,25 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { ExecFileOptions, ExecFileOptionsWithStringEncoding } from "child_process";
import { execFile } from "child_process"; import { execFile } from "child_process";
import { promisify } from "util"; import { promisify } from "util";
export type ExecFile = typeof execFile["__promisify__"]; export interface ExecFile {
(file: string, args?: readonly string[], opts?: ExecFileOptionsWithStringEncoding | ExecFileOptions): Promise<string>;
}
const execFileInjectable = getInjectable({ const execFileInjectable = getInjectable({
id: "exec-file", id: "exec-file",
instantiate: (): ExecFile => promisify(execFile), instantiate: (): ExecFile => {
const asyncExecFile = promisify(execFile);
return async (file) => {
const { stdout } = await asyncExecFile(file);
return stdout;
};
},
}); });
export default execFileInjectable; export default execFileInjectable;

View File

@ -17,9 +17,9 @@ const execHelmInjectable = getInjectable({
return async (...args: string[]): Promise<AsyncResult<string>> => { return async (...args: string[]): Promise<AsyncResult<string>> => {
try { try {
const { stdout } = await execFile(helmBinaryPath, args); const response = await execFile(helmBinaryPath, args);
return { callWasSuccessful: true, response: stdout }; return { callWasSuccessful: true, response };
} catch (error) { } catch (error) {
return { callWasSuccessful: false, error: getErrorMessage(error) }; return { callWasSuccessful: false, error: getErrorMessage(error) };
} }

View File

@ -4,7 +4,6 @@
*/ */
import type { DiContainer } from "@ogre-tools/injectable"; import type { DiContainer } from "@ogre-tools/injectable";
import type { ChildProcess } from "child_process";
import path from "path"; import path from "path";
import directoryForKubeConfigsInjectable from "../../../../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable"; import directoryForKubeConfigsInjectable from "../../../../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
import createAuthorizationReviewInjectable from "../../../../common/cluster/authorization-review.injectable"; import createAuthorizationReviewInjectable from "../../../../common/cluster/authorization-review.injectable";
@ -91,36 +90,20 @@ describe("ResourceApplier", () => {
}); });
it("should call unlink, if everything passes", async () => { it("should call unlink, if everything passes", async () => {
execFile.mockImplementation(() => Object.assign( execFile.mockImplementation(async () => "I am some output");
Promise.resolve({
stdout: "I am some output",
stderr: "",
}),
{
child: {} as ChildProcess,
},
));
await expect(resourceApplier.apply({})).resolves.toBeTruthy(); await expect(resourceApplier.apply({})).resolves.toBeTruthy();
expect(unlink).toBeCalledWith("some/temp/file"); expect(unlink).toBeCalledWith("some/temp/file");
}); });
it("should return the stdout of execFile", async () => { it("should return the stdout of execFile", async () => {
execFile.mockImplementation(() => Object.assign( execFile.mockImplementation(async () => "I am some output");
Promise.resolve({
stdout: "I am some output",
stderr: "",
}),
{
child: {} as ChildProcess,
},
));
expect(await resourceApplier.apply({})).toBe("I am some output"); expect(await resourceApplier.apply({})).toBe("I am some output");
}); });
it("should build up a correct set of arguments", async () => { it("should build up a correct set of arguments", async () => {
execFile.mockImplementation((path, args) => { execFile.mockImplementation(async (path, args) => {
expect(args).toEqual([ expect(args).toEqual([
"apply", "apply",
"--kubeconfig", "--kubeconfig",
@ -131,15 +114,7 @@ describe("ResourceApplier", () => {
"some/temp/file", "some/temp/file",
]); ]);
return Object.assign( return "I am some output";
Promise.resolve({
stdout: "I am some output",
stderr: "",
}),
{
child: {} as ChildProcess,
},
);
}); });
await resourceApplier.apply({}); await resourceApplier.apply({});
@ -184,17 +159,7 @@ describe("ResourceApplier", () => {
onlyOnce(contents); onlyOnce(contents);
}); });
execFile.mockImplementation(() => { execFile.mockImplementation(async () => "I am some output");
return Object.assign(
Promise.resolve({
stdout: "I am some output",
stderr: "",
}),
{
child: {} as ChildProcess,
},
);
});
await expect(resourceApplier.kubectlApplyAll([...resources])).resolves.toBeTruthy(); await expect(resourceApplier.kubectlApplyAll([...resources])).resolves.toBeTruthy();
expect(removeDir).toBeCalledWith("some/temp/dir"); expect(removeDir).toBeCalledWith("some/temp/dir");
@ -202,7 +167,7 @@ describe("ResourceApplier", () => {
}); });
it("should use resonable arguments", async () => { it("should use resonable arguments", async () => {
execFile.mockImplementation((path, args) => { execFile.mockImplementation(async (path, args) => {
expect(args).toEqual([ expect(args).toEqual([
"apply", "apply",
"--kubeconfig", "--kubeconfig",
@ -213,15 +178,7 @@ describe("ResourceApplier", () => {
"some/temp/dir", "some/temp/dir",
]); ]);
return Object.assign( return "I am some output";
Promise.resolve({
stdout: "I am some output",
stderr: "",
}),
{
child: {} as ChildProcess,
},
);
}); });
await expect(resourceApplier.kubectlApplyAll(["foo", "bar"])).resolves.toBeTruthy(); await expect(resourceApplier.kubectlApplyAll(["foo", "bar"])).resolves.toBeTruthy();

View File

@ -75,9 +75,7 @@ export class ResourceApplier implements K8sResourceApplier {
); );
try { try {
const { stdout } = await this.dependencies.execFile(kubectlPath, args); return await this.dependencies.execFile(kubectlPath, args);
return stdout;
} catch (error) { } catch (error) {
if (isObject(error) && hasTypedProperty(error, "stderr", isString)) { if (isObject(error) && hasTypedProperty(error, "stderr", isString)) {
throw error.stderr; throw error.stderr;
@ -114,9 +112,8 @@ export class ResourceApplier implements K8sResourceApplier {
try { try {
await this.dependencies.writeFile(fileName, content); await this.dependencies.writeFile(fileName, content);
const { stdout } = await this.dependencies.execFile(kubectlPath, args, { env: execEnv });
return stdout; return await this.dependencies.execFile(kubectlPath, args, { env: execEnv });
} catch (error) { } catch (error) {
if (isObject(error) && hasTypedProperty(error, "stderr", isString)) { if (isObject(error) && hasTypedProperty(error, "stderr", isString)) {
throw error.stderr; throw error.stderr;
@ -155,9 +152,7 @@ export class ResourceApplier implements K8sResourceApplier {
this.dependencies.logger.info(`[RESOURCE-APPLIER] Executing ${kubectlPath}`, { args }); this.dependencies.logger.info(`[RESOURCE-APPLIER] Executing ${kubectlPath}`, { args });
const { stdout } = await this.dependencies.execFile(kubectlPath, args); return await this.dependencies.execFile(kubectlPath, args);
return stdout;
} catch (error) { } catch (error) {
this.dependencies.logger.error(`[RESOURCE-APPLIER] cmd errored: ${error}`); this.dependencies.logger.error(`[RESOURCE-APPLIER] cmd errored: ${error}`);