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 { Exec, execInjectable } from "./exec.injectable";
|
||||
import { execInjectable } from "./exec.injectable";
|
||||
import asyncFn, { AsyncFnMock } from "@async-fn/jest";
|
||||
import { DoWebpackBuild, doWebpackBuildInjectable } from "./do-webpack-build";
|
||||
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";
|
||||
|
||||
describe("do-webpack-build", () => {
|
||||
let execMock: AsyncFnMock<Exec>;
|
||||
let execMock: jest.Mock;
|
||||
let doWebpackBuild: DoWebpackBuild;
|
||||
let logSuccessMock: AsyncFnMock<LogSuccess>;
|
||||
let logWarningMock: AsyncFnMock<LogWarning>;
|
||||
let execResultStub: { stdout: { on: any }; stderr: { on: any }; on: any };
|
||||
|
||||
beforeEach(() => {
|
||||
const di = getDi();
|
||||
|
||||
execMock = asyncFn();
|
||||
di.override(execInjectable, () => execMock);
|
||||
execResultStub = {
|
||||
stdout: { on: jest.fn() },
|
||||
stderr: { on: jest.fn() },
|
||||
on: jest.fn(),
|
||||
};
|
||||
execMock = jest.fn().mockReturnValue(execResultStub);
|
||||
di.override(execInjectable, () => execMock as any);
|
||||
logSuccessMock = asyncFn();
|
||||
di.override(logSuccessInjectable, () => logSuccessMock);
|
||||
logWarningMock = asyncFn();
|
||||
@ -36,58 +42,32 @@ describe("do-webpack-build", () => {
|
||||
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);
|
||||
|
||||
expect(promiseStatus.fulfilled).toBe(false);
|
||||
});
|
||||
|
||||
describe("when webpack resolves with stdout", () => {
|
||||
beforeEach(async () => {
|
||||
await execMock.resolve({ stdout: "some-stdout", stderr: "" });
|
||||
});
|
||||
it("when execution of webpack exits, script is done", async () => {
|
||||
const [[eventName, finishWebpack]] = execResultStub.on.mock.calls;
|
||||
|
||||
it("logs the stdout", () => {
|
||||
expect(logSuccessMock).toHaveBeenCalledWith("some-stdout");
|
||||
});
|
||||
eventName === "exit" && finishWebpack();
|
||||
|
||||
it("script is done", async () => {
|
||||
const promiseStatus = await getPromiseStatus(actualPromise);
|
||||
|
||||
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 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 () => {
|
||||
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 { exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
|
||||
const promisifiedExec = promisify(exec);
|
||||
|
||||
export type Exec = typeof promisifiedExec;
|
||||
export type Exec = typeof exec;
|
||||
|
||||
export const execInjectable = getInjectable({
|
||||
id: "exec",
|
||||
instantiate: () => promisifiedExec,
|
||||
instantiate: () => exec,
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user