fix Progress and Stats Tests for colors and output
This commit is contained in:
parent
b89853ea12
commit
0b0e9e9608
|
@ -23,7 +23,7 @@ const truncateArgs = (args, maxLength) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if there is space for at least 4 chars per arg
|
// 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
|
// remove args
|
||||||
if (args.length > 1)
|
if (args.length > 1)
|
||||||
return truncateArgs(args.slice(0, args.length - 1), maxLength);
|
return truncateArgs(args.slice(0, args.length - 1), maxLength);
|
||||||
|
@ -61,7 +61,7 @@ const truncateArgs = (args, maxLength) => {
|
||||||
const length = lengths[i];
|
const length = lengths[i];
|
||||||
if (str.length === length) {
|
if (str.length === length) {
|
||||||
return str;
|
return str;
|
||||||
} else if (length > 6) {
|
} else if (length > 5) {
|
||||||
return "..." + str.slice(-length + 3);
|
return "..." + str.slice(-length + 3);
|
||||||
} else if (length > 0) {
|
} else if (length > 0) {
|
||||||
return str.slice(-length);
|
return str.slice(-length);
|
||||||
|
|
|
@ -111,6 +111,7 @@ module.exports = {
|
||||||
}),
|
}),
|
||||||
status: tty
|
status: tty
|
||||||
? (name, ...args) => {
|
? (name, ...args) => {
|
||||||
|
args = args.filter(Boolean);
|
||||||
if (name === undefined && args.length === 0) {
|
if (name === undefined && args.length === 0) {
|
||||||
clearStatusMessage();
|
clearStatusMessage();
|
||||||
currentStatusMessage = undefined;
|
currentStatusMessage = undefined;
|
||||||
|
|
|
@ -3,19 +3,18 @@
|
||||||
const _ = require("lodash");
|
const _ = require("lodash");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const MemoryFs = require("memory-fs");
|
const MemoryFs = require("memory-fs");
|
||||||
const webpack = require("../");
|
|
||||||
const captureStdio = require("./helpers/captureStdio");
|
const captureStdio = require("./helpers/captureStdio");
|
||||||
|
|
||||||
|
let webpack;
|
||||||
|
|
||||||
describe("ProgressPlugin", function() {
|
describe("ProgressPlugin", function() {
|
||||||
let _env;
|
|
||||||
let stderr;
|
let stderr;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
_env = process.env;
|
stderr = captureStdio(process.stderr, true);
|
||||||
stderr = captureStdio(process.stderr);
|
webpack = require("../");
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
process.env = _env;
|
|
||||||
stderr && stderr.restore();
|
stderr && stderr.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -38,9 +37,18 @@ describe("ProgressPlugin", function() {
|
||||||
expect(logs.length).toBeGreaterThan(20);
|
expect(logs.length).toBeGreaterThan(20);
|
||||||
logs.forEach(log => expect(log.length).toBeLessThanOrEqual(30));
|
logs.forEach(log => expect(log.length).toBeLessThanOrEqual(30));
|
||||||
expect(logs).toContain(
|
expect(logs).toContain(
|
||||||
" 10% building ...ules ...tive",
|
"77% ...timization ...nksPlugin",
|
||||||
"trims each detail string equally"
|
"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;
|
return compiler;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getLogs = logsStr => logsStr.split(/\u0008+/).filter(v => !(v === " "));
|
const getLogs = logsStr => logsStr.split(/\r/).filter(v => !(v === " "));
|
||||||
|
|
||||||
const RunCompilerAsync = compiler =>
|
const RunCompilerAsync = compiler =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
|
|
|
@ -4,10 +4,11 @@
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
const webpack = require("../lib/webpack");
|
|
||||||
const Stats = require("../lib/Stats");
|
const Stats = require("../lib/Stats");
|
||||||
const captureStdio = require("./helpers/captureStdio");
|
const captureStdio = require("./helpers/captureStdio");
|
||||||
|
|
||||||
|
let webpack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escapes regular expression metacharacters
|
* Escapes regular expression metacharacters
|
||||||
* @param {string} str String to quote
|
* @param {string} str String to quote
|
||||||
|
@ -37,6 +38,14 @@ const tests = fs
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("StatsTestCases", () => {
|
describe("StatsTestCases", () => {
|
||||||
|
let stderr;
|
||||||
|
beforeEach(() => {
|
||||||
|
stderr = captureStdio(process.stderr, true);
|
||||||
|
webpack = require("../lib/webpack");
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
stderr.restore();
|
||||||
|
});
|
||||||
tests.forEach(testName => {
|
tests.forEach(testName => {
|
||||||
it("should print correct stats for " + testName, done => {
|
it("should print correct stats for " + testName, done => {
|
||||||
jest.setTimeout(10000);
|
jest.setTimeout(10000);
|
||||||
|
@ -67,9 +76,7 @@ describe("StatsTestCases", () => {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
const captured = captureStdio(process.stderr);
|
|
||||||
const c = webpack(options);
|
const c = webpack(options);
|
||||||
captured.restore();
|
|
||||||
const compilers = c.compilers ? c.compilers : [c];
|
const compilers = c.compilers ? c.compilers : [c];
|
||||||
compilers.forEach(c => {
|
compilers.forEach(c => {
|
||||||
const ifs = c.inputFileSystem;
|
const ifs = c.inputFileSystem;
|
||||||
|
@ -114,7 +121,7 @@ describe("StatsTestCases", () => {
|
||||||
let actual = stats.toString(toStringOptions);
|
let actual = stats.toString(toStringOptions);
|
||||||
expect(typeof actual).toBe("string");
|
expect(typeof actual).toBe("string");
|
||||||
if (!hasColorSetting) {
|
if (!hasColorSetting) {
|
||||||
actual = captured.toString() + actual;
|
actual = stderr.toString() + actual;
|
||||||
actual = actual
|
actual = actual
|
||||||
.replace(/\u001b\[[0-9;]*m/g, "")
|
.replace(/\u001b\[[0-9;]*m/g, "")
|
||||||
.replace(/[.0-9]+(\s?ms)/g, "X$1")
|
.replace(/[.0-9]+(\s?ms)/g, "X$1")
|
||||||
|
@ -123,7 +130,7 @@ describe("StatsTestCases", () => {
|
||||||
"$1 Thu Jan 01 1970 00:00:00 GMT"
|
"$1 Thu Jan 01 1970 00:00:00 GMT"
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
actual = captured.toStringRaw() + actual;
|
actual = stderr.toStringRaw() + actual;
|
||||||
actual = actual
|
actual = actual
|
||||||
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, "<CLR=$1,BOLD>")
|
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, "<CLR=$1,BOLD>")
|
||||||
.replace(/\u001b\[1m/g, "<CLR=BOLD>")
|
.replace(/\u001b\[1m/g, "<CLR=BOLD>")
|
||||||
|
|
|
@ -1,13 +1,22 @@
|
||||||
const stripAnsi = require("strip-ansi");
|
const stripAnsi = require("strip-ansi");
|
||||||
|
|
||||||
module.exports = stdio => {
|
module.exports = (stdio, tty) => {
|
||||||
let logs = [];
|
let logs = [];
|
||||||
|
|
||||||
const write = stdio.write;
|
const write = stdio.write;
|
||||||
|
const isTTY = stdio.isTTY;
|
||||||
|
|
||||||
stdio.write = function(str) {
|
stdio.write = function(str) {
|
||||||
logs.push(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 {
|
return {
|
||||||
data: logs,
|
data: logs,
|
||||||
|
@ -24,6 +33,13 @@ module.exports = stdio => {
|
||||||
|
|
||||||
restore() {
|
restore() {
|
||||||
stdio.write = write;
|
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")];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue