Skip to content

Commit

Permalink
replace stdout-stderr with own solution that keeps order between stdo…
Browse files Browse the repository at this point in the history
…ut and stderr
  • Loading branch information
alexkli committed Apr 29, 2022
1 parent 5c4fceb commit 7a4d505
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 47 deletions.
24 changes: 0 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"mocha": "^7.2.0",
"mock-fs": "^4.13.0",
"nyc": "^15.1.0",
"stdout-stderr": "^0.1.13",
"supports-color": "^8.1.1"
},
"dependencies": {
Expand Down
92 changes: 92 additions & 0 deletions test/capture-stdout-stderr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

"use strict";

const supportsColor = require("supports-color");

function stderrColor(str) {
if (supportsColor.stderr && !process.env.TEST_LOG_DISABLE_COLOR) {
return `\u001b[33m${str}\u001b[0m`;
} else {
return str;
}
}

function chunkToString(chunk) {
if (typeof chunk === 'string') {
return chunk;
}
return chunk.toString('utf8');
}

const _global = global;
if (!_global['capture-stdout-stderr']) {
_global['capture-stdout-stderr'] = {
stdout: process.stdout.write,
stderr: process.stderr.write,
};
}

const lines = [];

function overwrite(std) {
process[std].write = (chunk) => {
const str = chunkToString(chunk);
lines.push({
[std]: str
});
if (this.print) {
_global['capture-stdout-stderr'][std].call(process[std], std === "stderr" ? stderrColor(str) : str );
}
return true;
};
}

module.exports = {
print: false,

start() {
lines.length = 0;
overwrite.call(this, "stdout");
overwrite.call(this, "stderr");
},

stop() {
process.stdout.write = _global['capture-stdout-stderr'].stdout;
process.stderr.write = _global['capture-stdout-stderr'].stderr;
},

get output() {
return {
get stdout() {
return lines.map((l) => l.stdout || "").join("");
},
get stderr() {
return lines.map((l) => l.stderr || "").join("");
},
get any() {
return lines.map((l) => l.stdout || l.stderr).join("");
}
};
},

replay() {
for (const line of lines) {
if (line.stdout) {
process.stdout.write(line.stdout);
} else if (line.stderr) {
process.stderr.write(stderrColor(line.stderr));
}
}
}
};
9 changes: 5 additions & 4 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("cli e2e", function() {
it("help", async function() {
await sizewatcher(["-h"]);

assert(this.stderr.output.includes("Usage: sizewatcher [<options>] [<before> [<after>]]"));
assert(this.output.stderr.includes("Usage: sizewatcher [<options>] [<before> [<after>]]"));

assert.strictEqual(lastExitCode, 1);
});
Expand All @@ -60,7 +60,7 @@ describe("cli e2e", function() {
await sizewatcher();

assert.strictEqual(lastExitCode, 1);
assert(this.stderr.output.includes("Error: Not inside a git checkout"));
assert(this.output.stderr.includes("Error: Not inside a git checkout"));
});

it("local branch", async function() {
Expand All @@ -74,7 +74,8 @@ describe("cli e2e", function() {
await sizewatcher();

assert.strictEqual(lastExitCode, undefined);
assert(this.stdout.output.includes("'main' => 'new'"));
assert(this.stdout.output.includes("+ ✅ git: -0.0%"));
assert(this.output.stdout.includes("'main' => 'new'"));
assert(this.output.stdout.includes("+ ✅ git: -0.0%"));
});

});
42 changes: 24 additions & 18 deletions test/mocha-capture-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

'use strict';

const { stdout, stderr } = require("stdout-stderr");
const captureLogs = require("./capture-stdout-stderr");
const { spawn } = require("child_process");
const supportsColor = require("supports-color");

Expand All @@ -38,8 +38,7 @@ function captureAfterEach() {
if (isCaptureConsole(this)) {
if (!process.env.TEST_PRINT_LOGS && this.currentTest.state !== 'passed') {
printLine();
console.log(stdout.output);
console.error(stderr.output);
captureLogs.replay();
printLine();
}
}
Expand All @@ -52,31 +51,28 @@ function wrapTestFn(testFn) {
return;
}

this.stdout = stdout;
this.stderr = stderr;
this.output = captureLogs.output;
this.stdout = captureLogs.stdout;
this.stderr = captureLogs.stderr;

try {
// TODO: write own module that keeps order between stdout and stderr
stdout.start();
stderr.start();
captureLogs.start();


if (process.env.TEST_PRINT_LOGS) {
stdout.print = true;
stderr.print = true;
captureLogs.print = true;
}

await testFn.apply(this);

stdout.stop();
stderr.stop();
captureLogs.stop();

} catch (e) {
stdout.stop();
stderr.stop();
captureLogs.stop();

throw e;
} finally {
stdout.print = false;
stderr.print = false;
captureLogs.print = false;
}
};
}
Expand Down Expand Up @@ -147,9 +143,11 @@ module.exports = {
* Assert on test log output:
*
* it("my test", function() {
* assert(this.stdout.output.includes("something in stdout"));
* assert(this.stderr.output.includes("something in stderr"));
* assert(this.output.stdout.includes("something in stdout"));
* assert(this.output.stderr.includes("something in stderr"));
* assert(this.output.any.includes("something in stdout or stderr"));
* }).captureConsole = true;
*
* -------------------------------------------------
*
* Print all logs during test runs:
Expand All @@ -158,6 +156,14 @@ module.exports = {
*
* TEST_PRINT_LOGS=1 npm test
*
* -------------------------------------------------
*
* Disable color (for stderr)
*
* Set environment variable TEST_LOG_DISABLE_COLOR=1 (any value)
*
* TEST_LOG_DISABLE_COLOR=1 npm test
*
**/
enableMochaCaptureConsole,

Expand Down

0 comments on commit 7a4d505

Please sign in to comment.