Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support match stderr in cli preview #10728

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions packages/cli/src/cmds/preview/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"use strict";

import { ChildProcess, spawn } from "child_process";
import cp from "child_process";
import { err, FxError, LogLevel, ok, Result } from "@microsoft/teamsfx-api";
import treeKill from "tree-kill";
import { ServiceLogWriter } from "./serviceLogWriter";
Expand Down Expand Up @@ -33,7 +33,7 @@ export class Task {
private options?: TaskOptions;

private resolved = false;
private task: ChildProcess | undefined;
private task: cp.ChildProcess | undefined;

constructor(
taskTitle: string,
Expand Down Expand Up @@ -61,26 +61,25 @@ export class Task {
) => Promise<FxError | null>
): Promise<Result<TaskResult, FxError>> {
await startCallback(this.taskTitle, this.background);
this.task = spawn(this.command, this.args, this.options);
this.task = cp.spawn(this.command, this.args, this.options);
const stdout: string[] = [];
const stderr: string[] = [];
return new Promise((resolve) => {
this.task?.stdout?.on("data", (data) => {
// TODO: log
stdout.push(data.toString());
});
this.task?.stderr?.on("data", (data) => {
// TODO: log
stderr.push(data.toString());
});
this.task?.on("exit", async () => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.task?.on("exit", async (code) => {
const result: TaskResult = {
command: this.command,
options: this.options,
success: this.task?.exitCode === 0,
success: code === 0,
stdout: stdout,
stderr: stderr,
exitCode: this.task?.exitCode === undefined ? null : this.task?.exitCode,
exitCode: code,
};
const error = await stopCallback(this.taskTitle, this.background, result);
if (error) {
Expand All @@ -93,7 +92,7 @@ export class Task {
}

/**
* wait until stdout of the task matches the pattern or the task ends
* wait until stdout/stderr of the task matches the pattern or the task ends
*/
public async waitFor(
pattern: RegExp,
Expand All @@ -116,18 +115,19 @@ export class Task {
`${this.command} ${this.args ? this.args?.join(" ") : ""}\n`
);
await startCallback(this.taskTitle, this.background, serviceLogWriter);
this.task = spawn(this.command, this.args, this.options);
this.task = cp.spawn(this.command, this.args, this.options);
const stdout: string[] = [];
const stderr: string[] = [];
return new Promise((resolve) => {
if (timeout !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
setTimeout(async () => {
if (!this.resolved) {
this.resolved = true;
const result: TaskResult = {
command: this.command,
options: this.options,
success: true,
success: false,
stdout: stdout,
stderr: stderr,
exitCode: null,
Expand All @@ -142,6 +142,7 @@ export class Task {
}, timeout);
}

// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.task?.stdout?.on("data", async (data) => {
const dataStr = data.toString();
await serviceLogWriter?.write(this.taskTitle, dataStr);
Expand Down Expand Up @@ -170,16 +171,38 @@ export class Task {
}
}
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.task?.stderr?.on("data", async (data) => {
const dataStr = data.toString();
await serviceLogWriter?.write(this.taskTitle, dataStr);
if (logProvider) {
logProvider.necessaryLog(LogLevel.Info, dataStr.trim(), true);
}
stderr.push(dataStr);
if (!this.resolved) {
const match = pattern.test(dataStr);
if (match) {
this.resolved = true;
const result: TaskResult = {
command: this.command,
options: this.options,
success: false,
stdout: stdout,
stderr: stderr,
exitCode: null,
};
const error = await stopCallback(this.taskTitle, this.background, result);
if (error) {
resolve(err(error));
} else {
resolve(ok(result));
}
}
}
});

this.task?.on("exit", async () => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.task?.on("exit", async (code) => {
if (!this.resolved) {
this.resolved = true;
const result: TaskResult = {
Expand All @@ -188,7 +211,7 @@ export class Task {
success: false,
stdout: stdout,
stderr: stderr,
exitCode: this.task?.exitCode === undefined ? null : this.task?.exitCode,
exitCode: code,
};
const error = await stopCallback(this.taskTitle, this.background, result);
if (error) {
Expand Down
158 changes: 158 additions & 0 deletions packages/cli/tests/unit/cmds/preview/task.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as sinon from "sinon";
import { EventEmitter } from "events";
import cp from "child_process";
import { Readable } from "stream";
import { Task } from "../../../../src/cmds/preview/task";
import { expect } from "../../utils";

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

describe("Task", () => {
describe("wait", () => {
let sandbox: sinon.SinonSandbox;

afterEach(() => {
sandbox.restore();
});

it("happy path", async () => {
sandbox = sinon.createSandbox();
const startCallback = sinon.stub().resolves();
const stopCallback = sinon.stub().resolves(null);
const spawnEvent = <cp.ChildProcess>new EventEmitter();
spawnEvent.stdout = <Readable>new EventEmitter();
spawnEvent.stderr = <Readable>new EventEmitter();
sandbox.stub(cp, "spawn").callsFake(() => {
return spawnEvent;
});

const task = new Task("taskTitle", false, "command");
const promise = task.wait(startCallback, stopCallback);

await delay(10);

spawnEvent.stdout.emit("data", "stdout1");
spawnEvent.stdout.emit("data", "stdout2");
spawnEvent.stderr.emit("data", "stderr1");
spawnEvent.stderr.emit("data", "stderr2");
spawnEvent.emit("exit", 0);

const resultRes = await promise;

expect(resultRes.isOk()).to.be.true;
const result = (resultRes as any).value;
expect(result.success).to.be.true;
expect(result.stdout).to.deep.equals(["stdout1", "stdout2"]);
expect(result.stderr).to.deep.equals(["stderr1", "stderr2"]);
expect(result.exitCode).to.equals(0);
});
});

describe("waitFor", () => {
let sandbox: sinon.SinonSandbox;

afterEach(() => {
sandbox.restore();
});

it("happy path: match stdout", async () => {
sandbox = sinon.createSandbox();
const startCallback = sinon.stub().resolves();
const stopCallback = sinon.stub().resolves(null);
const spawnEvent = <cp.ChildProcess>new EventEmitter();
spawnEvent.stdout = <Readable>new EventEmitter();
spawnEvent.stderr = <Readable>new EventEmitter();
sandbox.stub(cp, "spawn").callsFake(() => {
return spawnEvent;
});

const task = new Task("taskTitle", true, "command");
const promise = task.waitFor(new RegExp("started", "i"), startCallback, stopCallback);

await delay(5);

spawnEvent.stdout.emit("data", "stdout1");
spawnEvent.stdout.emit("data", "stdout2");
spawnEvent.stdout.emit("data", "xxx started xxx");
spawnEvent.stderr.emit("data", "stderr1");
spawnEvent.stderr.emit("data", "stderr2");

const resultRes = await promise;

expect(resultRes.isOk()).to.be.true;
const result = (resultRes as any).value;
expect(result.success).to.be.true;
expect(result.stdout).to.deep.equals(["stdout1", "stdout2", "xxx started xxx"]);
expect(result.stderr).to.deep.equals(["stderr1", "stderr2"]);
expect(result.exitCode).to.equals(null);
});

it("happy path: match stderr", async () => {
sandbox = sinon.createSandbox();
const startCallback = sinon.stub().resolves();
const stopCallback = sinon.stub().resolves(null);
const spawnEvent = <cp.ChildProcess>new EventEmitter();
spawnEvent.stdout = <Readable>new EventEmitter();
spawnEvent.stderr = <Readable>new EventEmitter();
sandbox.stub(cp, "spawn").callsFake(() => {
return spawnEvent;
});

const task = new Task("taskTitle", true, "command");
const promise = task.waitFor(new RegExp("started", "i"), startCallback, stopCallback);

await delay(5);

spawnEvent.stdout.emit("data", "stdout1");
spawnEvent.stdout.emit("data", "stdout2");
spawnEvent.stderr.emit("data", "stderr1");
spawnEvent.stderr.emit("data", "stderr2");
spawnEvent.stderr.emit("data", "xxx started xxx");

const resultRes = await promise;

expect(resultRes.isOk()).to.be.true;
const result = (resultRes as any).value;
expect(result.success).to.be.false;
expect(result.stdout).to.deep.equals(["stdout1", "stdout2"]);
expect(result.stderr).to.deep.equals(["stderr1", "stderr2", "xxx started xxx"]);
expect(result.exitCode).to.equals(null);
});

it("timeout", async () => {
sandbox = sinon.createSandbox();
const startCallback = sinon.stub().resolves();
const stopCallback = sinon.stub().resolves(null);
const spawnEvent = <cp.ChildProcess>new EventEmitter();
spawnEvent.stdout = <Readable>new EventEmitter();
spawnEvent.stderr = <Readable>new EventEmitter();
sandbox.stub(cp, "spawn").callsFake(() => {
return spawnEvent;
});

const task = new Task("taskTitle", true, "command");
const promise = task.waitFor(new RegExp("started", "i"), startCallback, stopCallback, 30);

await delay(5);

spawnEvent.stdout.emit("data", "stdout1");
spawnEvent.stdout.emit("data", "stdout2");
spawnEvent.stderr.emit("data", "stderr1");
spawnEvent.stderr.emit("data", "stderr2");

const resultRes = await promise;

expect(resultRes.isOk()).to.be.true;
const result = (resultRes as any).value;
expect(result.success).to.be.false;
expect(result.stdout).to.deep.equals(["stdout1", "stdout2"]);
expect(result.stderr).to.deep.equals(["stderr1", "stderr2"]);
expect(result.exitCode).to.equals(null);
});
});
});