From ac5f5a433005e6233dd705982af6c0da786744af Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Wed, 14 Aug 2024 18:30:35 +0200 Subject: [PATCH] test_runner: avoid coverage report partial file names Co-author: Medhansh404 <21ucs126@lnmiit.ac.in> --- lib/internal/test_runner/utils.js | 46 +++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/internal/test_runner/utils.js b/lib/internal/test_runner/utils.js index aae2a756800a0f..40094fbfb98e57 100644 --- a/lib/internal/test_runner/utils.js +++ b/lib/internal/test_runner/utils.js @@ -342,9 +342,6 @@ function addTableLine(prefix, width) { } const kHorizontalEllipsis = '\u2026'; -function truncateStart(string, width) { - return string.length > width ? `${kHorizontalEllipsis}${StringPrototypeSlice(string, string.length - width + 1)}` : string; -} function truncateEnd(string, width) { return string.length > width ? `${StringPrototypeSlice(string, 0, width - 1)}${kHorizontalEllipsis}` : string; @@ -419,7 +416,6 @@ function getCoverageReport(pad, summary, symbol, color, table) { } } - function getCell(string, width, pad, truncate, coverage) { if (!table) return string; @@ -434,6 +430,46 @@ function getCoverageReport(pad, summary, symbol, color, table) { return result; } + function getfilePathMultiline(string, width, pad, coverage) { + if (!table) return string; + + const lines = []; + let currentLine = ''; + + for (const word of StringPrototypeSplit(string, '\\')) { + if (currentLine.length + word.length + 1 <= width) { + // If adding the next word fits in the current line, append it + currentLine += (currentLine.length > 0 ? '\\' : '') + word; + } else { + // If adding the next word exceeds the width, start a new line + ArrayPrototypePush(lines, currentLine); + currentLine = word; + } + } + + // Add the last line if any + if (currentLine.length > 0) { + ArrayPrototypePush(lines, currentLine); + } + + const truncatedLines = ArrayPrototypeMap(lines, (line) => StringPrototypeSlice(line, 0, width)); + + // Delete empty lines if any + for (let i = 0; i < truncatedLines.length; ++i) { + if (truncatedLines[i].length === 0) { + truncatedLines.splice(i, 1); + } + } + + return getCell( + ArrayPrototypeJoin(truncatedLines, '\n'), + width, + pad, + false, + coverage, + ); + } + // Head if (table) report += addTableLine(prefix, tableWidth); report += `${prefix}${getCell('file', filePadLength, StringPrototypePadEnd, truncateEnd)}${kSeparator}` + @@ -454,7 +490,7 @@ function getCoverageReport(pad, summary, symbol, color, table) { }); fileCoverage /= kColumnsKeys.length; - report += `${prefix}${getCell(relativePath, filePadLength, StringPrototypePadEnd, truncateStart, fileCoverage)}${kSeparator}` + + report += `${prefix}${getfilePathMultiline(relativePath, filePadLength, StringPrototypePadEnd, fileCoverage)}${kSeparator}` + `${ArrayPrototypeJoin(ArrayPrototypeMap(coverages, (coverage, j) => getCell(NumberPrototypeToFixed(coverage, 2), columnPadLengths[j], StringPrototypePadStart, false, coverage)), kSeparator)}${kSeparator}` + `${getCell(formatUncoveredLines(getUncoveredLines(file.lines), table), uncoveredLinesPadLength, false, truncateEnd)}\n`; }