diff --git a/lib/logging/truncateArgs.js b/lib/logging/truncateArgs.js index 714e4624c..53f52c835 100644 --- a/lib/logging/truncateArgs.js +++ b/lib/logging/truncateArgs.js @@ -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); diff --git a/lib/node/nodeConsole.js b/lib/node/nodeConsole.js index 016bbf2b9..dd09e0ecd 100644 --- a/lib/node/nodeConsole.js +++ b/lib/node/nodeConsole.js @@ -111,6 +111,7 @@ module.exports = { }), status: tty ? (name, ...args) => { + args = args.filter(Boolean); if (name === undefined && args.length === 0) { clearStatusMessage(); currentStatusMessage = undefined; diff --git a/test/ProgressPlugin.test.js b/test/ProgressPlugin.test.js index dbd6b377f..f202a31eb 100644 --- a/test/ProgressPlugin.test.js +++ b/test/ProgressPlugin.test.js @@ -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) => { diff --git a/test/StatsTestCases.test.js b/test/StatsTestCases.test.js index 80e3c5498..84e0802cf 100644 --- a/test/StatsTestCases.test.js +++ b/test/StatsTestCases.test.js @@ -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, "") .replace(/\u001b\[1m/g, "") diff --git a/test/helpers/captureStdio.js b/test/helpers/captureStdio.js index 86f4a75ef..db1bdf173 100644 --- a/test/helpers/captureStdio.js +++ b/test/helpers/captureStdio.js @@ -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")]; } }; };