Skip to content

Commit 4f78690

Browse files
committed
test new refactor
1 parent 4bd12f4 commit 4f78690

File tree

3 files changed

+58
-69
lines changed

3 files changed

+58
-69
lines changed

dist/index.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26163,14 +26163,17 @@ exports.formatResults = formatResults;
2616326163
*/
2616426164
function formatResults(results, coverageResults, showCoverage) {
2616526165
let output = `# ${process.env.pr_comment_title || "🧪 OPA Rego Policy Test Results"}\n\n`;
26166+
// Build header row
26167+
let header = "| File | Status | Passed | Total |";
26168+
let separator = "|------|--------|--------|-------|";
2616626169
if (showCoverage) {
26167-
output += "| File | Status | Passed | Total | Coverage | Details |\n";
26168-
output += "|------|--------|--------|-------|----------|----------|\n";
26169-
}
26170-
else {
26171-
output += "| File | Status | Passed | Total | Details |\n";
26172-
output += "|------|--------|--------|-------|----------|\n";
26170+
header += " Coverage |";
26171+
separator += "----------|";
2617326172
}
26173+
header += " Details |\n";
26174+
separator += "----------|\n";
26175+
output += header;
26176+
output += separator;
2617426177
for (const result of results) {
2617526178
let statusEmoji, statusText;
2617626179
switch (result.status) {
@@ -26188,49 +26191,41 @@ function formatResults(results, coverageResults, showCoverage) {
2618826191
break;
2618926192
}
2619026193
const testFileName = result.file;
26191-
let coverageInfo;
26192-
// Find the corresponding coverage test information for the test result we're on
26194+
const details = result.status === "NO TESTS"
26195+
? "No test file found"
26196+
: result.details.join("<br>");
26197+
const detailsColumn = `<details><summary>Show Details</summary>${details}</details>`;
26198+
let row = `| ${testFileName} | ${statusText} | ${result.passed} | ${result.total} |`;
2619326199
if (showCoverage) {
26194-
coverageInfo = coverageResults.find((cr) => {
26200+
const coverageInfo = coverageResults.find((cr) => {
2619526201
const lastSlashIndex = cr.file.lastIndexOf("/");
2619626202
const dotRegoIndex = cr.file.lastIndexOf(".rego");
26197-
// Check if the file paths are valid
2619826203
if (lastSlashIndex === -1 || dotRegoIndex === -1)
2619926204
return false;
26200-
// Extract the base file name without extension from the coverage report
2620126205
const fileNameWithoutExtension = cr.file.slice(lastSlashIndex + 1, dotRegoIndex);
26202-
// Match the test file with its corresponding implementation file in the coverage results
26203-
// Test files typically have names like 'abc_test.rego', while coverage is reported for 'abc.rego' because the test file is testing the implementation file, and the coverage is on how much the implementation file is covered.
26204-
// We want to associate the coverage data from 'abc.rego' with the test results from 'abc_test.rego',
26205-
// as long as it contains the same base name.
2620626206
return (testFileName.includes(fileNameWithoutExtension) &&
2620726207
!cr.file.includes(testFileName));
2620826208
});
26209-
}
26210-
const details = result.status === "NO TESTS"
26211-
? "No test file found"
26212-
: result.details.join("<br>");
26213-
const detailsColumn = `<details><summary>Show Details</summary>${details}</details>`;
26214-
let row = `| ${testFileName} | ${statusText} | ${result.passed} | ${result.total} `;
26215-
if (showCoverage) {
2621626209
let coverageText = "N/A";
26217-
try {
26218-
let uncoveredLinesDetails = "";
26219-
if (coverageInfo) {
26210+
let uncoveredLinesDetails = "";
26211+
if (coverageInfo) {
26212+
try {
2622026213
coverageText = `${coverageInfo.coverage.toFixed(2)}%`;
2622126214
if (coverageInfo.notCoveredLines &&
2622226215
coverageInfo.notCoveredLines !== "N/A") {
26223-
uncoveredLinesDetails = `<details><summary>Uncovered Lines</summary>${coverageInfo.notCoveredLines}</details>`;
26216+
uncoveredLinesDetails = ` <details><summary>Uncovered Lines</summary>${coverageInfo.notCoveredLines}</details>`;
2622426217
}
26218+
coverageText += uncoveredLinesDetails; // Combine text and details
26219+
}
26220+
catch (error) {
26221+
console.error("Error processing coverage information:", error);
26222+
console.log("Coverage Info:", coverageInfo);
26223+
// Keep coverageText as "N/A" or set to an error message if preferred
2622526224
}
26226-
row += `| ${coverageText} ${uncoveredLinesDetails} `;
26227-
}
26228-
catch (error) {
26229-
console.error("Error processing coverage information:", error);
26230-
console.log("Coverage Info:", coverageInfo);
2623126225
}
26226+
row += ` ${coverageText} |`;
2623226227
}
26233-
row += `| ${detailsColumn} |\n`;
26228+
row += ` ${detailsColumn} |\n`;
2623426229
output += row;
2623526230
}
2623626231
if (process.env.indicate_source_message === "true") {

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/formatResults.ts

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ProcessedTestResult, ProcessedCoverageResult } from "./interfaces"
1+
import { ProcessedTestResult, ProcessedCoverageResult } from "./interfaces";
22

33
/**
44
* Formats the test results and coverage results into a Markdown table for GitHub comments.
@@ -14,13 +14,18 @@ export function formatResults(
1414
): string {
1515
let output = `# ${process.env.pr_comment_title || "🧪 OPA Rego Policy Test Results"}\n\n`;
1616

17+
// Build header row
18+
let header = "| File | Status | Passed | Total |";
19+
let separator = "|------|--------|--------|-------|";
20+
1721
if (showCoverage) {
18-
output += "| File | Status | Passed | Total | Coverage | Details |\n";
19-
output += "|------|--------|--------|-------|----------|----------|\n";
20-
} else {
21-
output += "| File | Status | Passed | Total | Details |\n";
22-
output += "|------|--------|--------|-------|----------|\n";
22+
header += " Coverage |";
23+
separator += "----------|";
2324
}
25+
header += " Details |\n";
26+
separator += "----------|\n";
27+
output += header;
28+
output += separator;
2429

2530
for (const result of results) {
2631
let statusEmoji, statusText;
@@ -40,64 +45,53 @@ export function formatResults(
4045
}
4146

4247
const testFileName = result.file;
48+
const details =
49+
result.status === "NO TESTS"
50+
? "No test file found"
51+
: result.details.join("<br>");
52+
const detailsColumn = `<details><summary>Show Details</summary>${details}</details>`;
53+
54+
let row = `| ${testFileName} | ${statusText} | ${result.passed} | ${result.total} |`;
4355

44-
let coverageInfo;
45-
// Find the corresponding coverage test information for the test result we're on
4656
if (showCoverage) {
47-
coverageInfo = coverageResults.find((cr) => {
57+
const coverageInfo = coverageResults.find((cr) => {
4858
const lastSlashIndex = cr.file.lastIndexOf("/");
4959
const dotRegoIndex = cr.file.lastIndexOf(".rego");
5060

51-
// Check if the file paths are valid
5261
if (lastSlashIndex === -1 || dotRegoIndex === -1) return false;
5362

54-
// Extract the base file name without extension from the coverage report
5563
const fileNameWithoutExtension = cr.file.slice(
5664
lastSlashIndex + 1,
5765
dotRegoIndex,
5866
);
59-
60-
// Match the test file with its corresponding implementation file in the coverage results
61-
// Test files typically have names like 'abc_test.rego', while coverage is reported for 'abc.rego' because the test file is testing the implementation file, and the coverage is on how much the implementation file is covered.
62-
// We want to associate the coverage data from 'abc.rego' with the test results from 'abc_test.rego',
63-
// as long as it contains the same base name.
6467
return (
6568
testFileName.includes(fileNameWithoutExtension) &&
6669
!cr.file.includes(testFileName)
6770
);
6871
});
69-
}
7072

71-
const details =
72-
result.status === "NO TESTS"
73-
? "No test file found"
74-
: result.details.join("<br>");
75-
76-
const detailsColumn = `<details><summary>Show Details</summary>${details}</details>`;
77-
78-
let row = `| ${testFileName} | ${statusText} | ${result.passed} | ${result.total} `;
79-
80-
if (showCoverage) {
8173
let coverageText = "N/A";
82-
try {
83-
let uncoveredLinesDetails = "";
84-
if (coverageInfo) {
74+
let uncoveredLinesDetails = "";
75+
if (coverageInfo) {
76+
try {
8577
coverageText = `${coverageInfo.coverage.toFixed(2)}%`;
8678
if (
8779
coverageInfo.notCoveredLines &&
8880
coverageInfo.notCoveredLines !== "N/A"
8981
) {
90-
uncoveredLinesDetails = `<details><summary>Uncovered Lines</summary>${coverageInfo.notCoveredLines}</details>`;
82+
uncoveredLinesDetails = ` <details><summary>Uncovered Lines</summary>${coverageInfo.notCoveredLines}</details>`;
9183
}
84+
coverageText += uncoveredLinesDetails; // Combine text and details
85+
} catch (error) {
86+
console.error("Error processing coverage information:", error);
87+
console.log("Coverage Info:", coverageInfo);
88+
// Keep coverageText as "N/A" or set to an error message if preferred
9289
}
93-
row += `| ${coverageText} ${uncoveredLinesDetails} `;
94-
} catch (error) {
95-
console.error("Error processing coverage information:", error);
96-
console.log("Coverage Info:", coverageInfo);
9790
}
91+
row += ` ${coverageText} |`;
9892
}
9993

100-
row += `| ${detailsColumn} |\n`;
94+
row += ` ${detailsColumn} |\n`;
10195
output += row;
10296
}
10397

0 commit comments

Comments
 (0)