Skip to content

Commit

Permalink
[nodejs/auto] Fix incremental behavior of onStdout (#10678)
Browse files Browse the repository at this point in the history
* Fix incrementality of onStdout

* Test that onOutput is called more than once

* Add a CHANGELOG entry
  • Loading branch information
t0yv0 committed Sep 12, 2022
1 parent fd099ca commit a13426d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@

- Fix invalid resource type on `pulumi convert` to Go
[#10670](https://github.com/pulumi/pulumi/pull/10670)

- [auto/nodejs] `onOutput` is now called incrementally as the
underyling Pulumi process produces data, instead of being called
once at the end of the process execution. This restores behavior
that regressed since 3.39.0.
[#10678](https://github.com/pulumi/pulumi/pull/10678)
17 changes: 13 additions & 4 deletions sdk/nodejs/automation/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,23 @@ export async function runPulumiCmd(
const env = { ...process.env, ...additionalEnv };

try {
const { stdout, stderr, exitCode } = await execa("pulumi", args, { env, cwd });
const proc = execa("pulumi", args, { env, cwd });

if (onOutput && proc.stdout) {
proc.stdout!.on("data", (data: any) => {
if (data && data.toString) {
data = data.toString();
}
onOutput(data);
});
}

const { stdout, stderr, exitCode } = await proc;
const commandResult = new CommandResult(stdout, stderr, exitCode);
if (exitCode !== 0) {
throw createCommandError(commandResult);
}
if (onOutput) {
onOutput(stdout);
}

return commandResult;
} catch (err) {
const error = err as Error;
Expand Down
18 changes: 10 additions & 8 deletions sdk/nodejs/tests/automation/cmd.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2021, Pulumi Corporation.
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -13,17 +13,19 @@
// limitations under the License.

import * as assert from "assert";
import * as sinon from "sinon";
import { runPulumiCmd } from "../../automation";
import { asyncTest } from "../util";

describe("automation/cmd", () => {
it("calls onOutput when provided to runPulumiCmd", asyncTest(async () => {
const spy = sinon.spy();
await runPulumiCmd(["version"], ".", {}, spy);

assert.ok(spy.calledOnce);
assert.strictEqual(spy.firstCall.firstArg, spy.lastCall.lastArg);
let output = "";
let numCalls = 0;
await runPulumiCmd(["--help"], ".", {}, (data: string) => {
output += data;
numCalls += 1;
});
assert.ok(numCalls > 1, `expected numCalls > 1, got ${numCalls}`);
assert.match(output, new RegExp("Usage[:]"));
assert.match(output, new RegExp("[-][-]verbose"));
}));
});

0 comments on commit a13426d

Please sign in to comment.