mirror of
https://github.com/lensapp/lens.git
synced 2024-09-11 09:25:26 +03:00
feat: Make lens-webpack-build cope long lasting processes, eg. watch
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
0c2cc25b5b
commit
28c9eeec36
@ -1,5 +1,5 @@
|
|||||||
import { getDi } from "./get-di";
|
import { getDi } from "./get-di";
|
||||||
import { Exec, execInjectable } from "./exec.injectable";
|
import { execInjectable } from "./exec.injectable";
|
||||||
import asyncFn, { AsyncFnMock } from "@async-fn/jest";
|
import asyncFn, { AsyncFnMock } from "@async-fn/jest";
|
||||||
import { DoWebpackBuild, doWebpackBuildInjectable } from "./do-webpack-build";
|
import { DoWebpackBuild, doWebpackBuildInjectable } from "./do-webpack-build";
|
||||||
import { getPromiseStatus } from "@ogre-tools/test-utils";
|
import { getPromiseStatus } from "@ogre-tools/test-utils";
|
||||||
@ -7,16 +7,22 @@ import { LogSuccess, logSuccessInjectable } from "./log-success.injectable";
|
|||||||
import { LogWarning, logWarningInjectable } from "./log-warning.injectable";
|
import { LogWarning, logWarningInjectable } from "./log-warning.injectable";
|
||||||
|
|
||||||
describe("do-webpack-build", () => {
|
describe("do-webpack-build", () => {
|
||||||
let execMock: AsyncFnMock<Exec>;
|
let execMock: jest.Mock;
|
||||||
let doWebpackBuild: DoWebpackBuild;
|
let doWebpackBuild: DoWebpackBuild;
|
||||||
let logSuccessMock: AsyncFnMock<LogSuccess>;
|
let logSuccessMock: AsyncFnMock<LogSuccess>;
|
||||||
let logWarningMock: AsyncFnMock<LogWarning>;
|
let logWarningMock: AsyncFnMock<LogWarning>;
|
||||||
|
let execResultStub: { stdout: { on: any }; stderr: { on: any }; on: any };
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const di = getDi();
|
const di = getDi();
|
||||||
|
|
||||||
execMock = asyncFn();
|
execResultStub = {
|
||||||
di.override(execInjectable, () => execMock);
|
stdout: { on: jest.fn() },
|
||||||
|
stderr: { on: jest.fn() },
|
||||||
|
on: jest.fn(),
|
||||||
|
};
|
||||||
|
execMock = jest.fn().mockReturnValue(execResultStub);
|
||||||
|
di.override(execInjectable, () => execMock as any);
|
||||||
logSuccessMock = asyncFn();
|
logSuccessMock = asyncFn();
|
||||||
di.override(logSuccessInjectable, () => logSuccessMock);
|
di.override(logSuccessInjectable, () => logSuccessMock);
|
||||||
logWarningMock = asyncFn();
|
logWarningMock = asyncFn();
|
||||||
@ -36,58 +42,32 @@ describe("do-webpack-build", () => {
|
|||||||
expect(execMock).toHaveBeenCalledWith("webpack");
|
expect(execMock).toHaveBeenCalledWith("webpack");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not resolve yet", async () => {
|
it("data in stdout logs as success", () => {
|
||||||
|
const listeners = execResultStub.stdout.on.mock.calls;
|
||||||
|
|
||||||
|
expect(listeners).toEqual([["data", logSuccessMock]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("data in stderr logs as warning", () => {
|
||||||
|
const listeners = execResultStub.stderr.on.mock.calls;
|
||||||
|
|
||||||
|
expect(listeners).toEqual([["data", logWarningMock]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("script is not done yet", async () => {
|
||||||
const promiseStatus = await getPromiseStatus(actualPromise);
|
const promiseStatus = await getPromiseStatus(actualPromise);
|
||||||
|
|
||||||
expect(promiseStatus.fulfilled).toBe(false);
|
expect(promiseStatus.fulfilled).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when webpack resolves with stdout", () => {
|
it("when execution of webpack exits, script is done", async () => {
|
||||||
beforeEach(async () => {
|
const [[eventName, finishWebpack]] = execResultStub.on.mock.calls;
|
||||||
await execMock.resolve({ stdout: "some-stdout", stderr: "" });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs the stdout", () => {
|
eventName === "exit" && finishWebpack();
|
||||||
expect(logSuccessMock).toHaveBeenCalledWith("some-stdout");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("script is done", async () => {
|
const promiseStatus = await getPromiseStatus(actualPromise);
|
||||||
const promiseStatus = await getPromiseStatus(actualPromise);
|
|
||||||
|
|
||||||
expect(promiseStatus.fulfilled).toBe(true);
|
expect(promiseStatus.fulfilled).toBe(true);
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("when webpack resolves with stderr", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
execMock.resolve({ stdout: "", stderr: "some-stderr" });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not log success", () => {
|
|
||||||
actualPromise.catch(() => {});
|
|
||||||
|
|
||||||
expect(logSuccessMock).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs a warning", () => {
|
|
||||||
expect(logWarningMock).toBeCalledWith("Warning while executing \"webpack\": some-stderr");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("when webpack rejects", () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
execMock.reject(new Error("some-error"));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not log success", () => {
|
|
||||||
actualPromise.catch(() => {});
|
|
||||||
|
|
||||||
expect(logSuccessMock).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("throws", () => {
|
|
||||||
return expect(actualPromise).rejects.toThrow("some-error");
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -13,18 +13,15 @@ export const doWebpackBuildInjectable = getInjectable({
|
|||||||
const logSuccess = di.inject(logSuccessInjectable);
|
const logSuccess = di.inject(logSuccessInjectable);
|
||||||
const logWarning = di.inject(logWarningInjectable);
|
const logWarning = di.inject(logWarningInjectable);
|
||||||
|
|
||||||
const execWithResultHandling = async (command: string) => {
|
|
||||||
const { stdout, stderr } = await exec(command);
|
|
||||||
|
|
||||||
if (stderr) {
|
|
||||||
logWarning(`Warning while executing "${command}": ${stderr}`);
|
|
||||||
} else if (stdout) {
|
|
||||||
logSuccess(stdout);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return async () => {
|
return async () => {
|
||||||
await execWithResultHandling("webpack");
|
const execResult = exec("webpack");
|
||||||
|
|
||||||
|
execResult.stdout?.on("data", logSuccess);
|
||||||
|
execResult.stderr?.on("data", logWarning);
|
||||||
|
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
execResult.on("exit", resolve);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { exec } from "child_process";
|
import { exec } from "child_process";
|
||||||
import { promisify } from "util";
|
|
||||||
|
|
||||||
const promisifiedExec = promisify(exec);
|
export type Exec = typeof exec;
|
||||||
|
|
||||||
export type Exec = typeof promisifiedExec;
|
|
||||||
|
|
||||||
export const execInjectable = getInjectable({
|
export const execInjectable = getInjectable({
|
||||||
id: "exec",
|
id: "exec",
|
||||||
instantiate: () => promisifiedExec,
|
instantiate: () => exec,
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user