fix Progress and Stats Tests for colors and output

This commit is contained in:
Tobias Koppers 2019-08-01 08:18:45 +02:00
parent b89853ea12
commit 0b0e9e9608
5 changed files with 47 additions and 15 deletions

View File

@ -23,7 +23,7 @@ const truncateArgs = (args, maxLength) => {
}
// Check if there is space for at least 4 chars per arg
if (availableLength < lengths.reduce((s, i) => s + Math.min(i, 4), 0)) {
if (availableLength < lengths.reduce((s, i) => s + Math.min(i, 6), 0)) {
// remove args
if (args.length > 1)
return truncateArgs(args.slice(0, args.length - 1), maxLength);
@ -61,7 +61,7 @@ const truncateArgs = (args, maxLength) => {
const length = lengths[i];
if (str.length === length) {
return str;
} else if (length > 6) {
} else if (length > 5) {
return "..." + str.slice(-length + 3);
} else if (length > 0) {
return str.slice(-length);

View File

@ -111,6 +111,7 @@ module.exports = {
}),
status: tty
? (name, ...args) => {
args = args.filter(Boolean);
if (name === undefined && args.length === 0) {
clearStatusMessage();
currentStatusMessage = undefined;

View File

@ -3,19 +3,18 @@
const _ = require("lodash");
const path = require("path");
const MemoryFs = require("memory-fs");
const webpack = require("../");
const captureStdio = require("./helpers/captureStdio");
let webpack;
describe("ProgressPlugin", function() {
let _env;
let stderr;
beforeEach(() => {
_env = process.env;
stderr = captureStdio(process.stderr);
stderr = captureStdio(process.stderr, true);
webpack = require("../");
});
afterEach(() => {
process.env = _env;
stderr && stderr.restore();
});
@ -38,9 +37,18 @@ describe("ProgressPlugin", function() {
expect(logs.length).toBeGreaterThan(20);
logs.forEach(log => expect(log.length).toBeLessThanOrEqual(30));
expect(logs).toContain(
" 10% building ...ules ...tive",
"77% ...timization ...nksPlugin",
"trims each detail string equally"
);
expect(logs).toContain(
"10% building ...dules 0 active",
"remove empty arguments"
);
expect(logs).toContain(
"10% building ...dules 1 active",
"omit arguments when no space"
);
expect(logs).toContain("93% ...hunk asset optimization");
});
});
@ -88,7 +96,7 @@ const createSimpleCompiler = () => {
return compiler;
};
const getLogs = logsStr => logsStr.split(/\u0008+/).filter(v => !(v === " "));
const getLogs = logsStr => logsStr.split(/\r/).filter(v => !(v === " "));
const RunCompilerAsync = compiler =>
new Promise((resolve, reject) => {

View File

@ -4,10 +4,11 @@
const path = require("path");
const fs = require("fs");
const webpack = require("../lib/webpack");
const Stats = require("../lib/Stats");
const captureStdio = require("./helpers/captureStdio");
let webpack;
/**
* Escapes regular expression metacharacters
* @param {string} str String to quote
@ -37,6 +38,14 @@ const tests = fs
});
describe("StatsTestCases", () => {
let stderr;
beforeEach(() => {
stderr = captureStdio(process.stderr, true);
webpack = require("../lib/webpack");
});
afterEach(() => {
stderr.restore();
});
tests.forEach(testName => {
it("should print correct stats for " + testName, done => {
jest.setTimeout(10000);
@ -67,9 +76,7 @@ describe("StatsTestCases", () => {
})
);
});
const captured = captureStdio(process.stderr);
const c = webpack(options);
captured.restore();
const compilers = c.compilers ? c.compilers : [c];
compilers.forEach(c => {
const ifs = c.inputFileSystem;
@ -114,7 +121,7 @@ describe("StatsTestCases", () => {
let actual = stats.toString(toStringOptions);
expect(typeof actual).toBe("string");
if (!hasColorSetting) {
actual = captured.toString() + actual;
actual = stderr.toString() + actual;
actual = actual
.replace(/\u001b\[[0-9;]*m/g, "")
.replace(/[.0-9]+(\s?ms)/g, "X$1")
@ -123,7 +130,7 @@ describe("StatsTestCases", () => {
"$1 Thu Jan 01 1970 00:00:00 GMT"
);
} else {
actual = captured.toStringRaw() + actual;
actual = stderr.toStringRaw() + actual;
actual = actual
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, "<CLR=$1,BOLD>")
.replace(/\u001b\[1m/g, "<CLR=BOLD>")

View File

@ -1,13 +1,22 @@
const stripAnsi = require("strip-ansi");
module.exports = stdio => {
module.exports = (stdio, tty) => {
let logs = [];
const write = stdio.write;
const isTTY = stdio.isTTY;
stdio.write = function(str) {
logs.push(str);
};
if (tty !== undefined) stdio.isTTY = tty;
// isTTY flag is only read once on initialization
// therefore we need to clear some module caches
// to get the mocked value
delete require.cache[require.resolve("../../")];
delete require.cache[require.resolve("../../lib/node/NodeEnvironmentPlugin")];
delete require.cache[require.resolve("../../lib/node/nodeConsole")];
return {
data: logs,
@ -24,6 +33,13 @@ module.exports = stdio => {
restore() {
stdio.write = write;
stdio.isTTY = isTTY;
delete require.cache[require.resolve("../../")];
delete require.cache[
require.resolve("../../lib/node/NodeEnvironmentPlugin")
];
delete require.cache[require.resolve("../../lib/node/nodeConsole")];
}
};
};