Skip to content

Commit

Permalink
added testing formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
DE7924 committed Dec 23, 2024
1 parent 26c0797 commit 579d72f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 16 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ inputs:
required: false
default: "true"

test-results-path:
description: "The path to the test results file"
required: false
default: "./test-results.xml"

run-coverage:
description: "Whether to run coverage"
required: false
Expand Down
85 changes: 78 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const checkIfLocal = (): boolean => {

const getInputs = (
isLocal: boolean,
): [string, string, boolean, boolean, boolean, boolean] => {
): [string, string, boolean, boolean, boolean, string, boolean] => {
// get the token and octokit
let token = "";
if (process.env.GITHUB_TOKEN && isLocal) {
Expand All @@ -105,6 +105,10 @@ const getInputs = (
// get tests input
const doTests: boolean = isLocal ? true : getBooleanInput("run-tests");

const testResultsPath: string = isLocal
? "./test-results.xml"
: getInput("test-results-path");

// const runCoverage: boolean = getBooleanInput('run-coverage');
// const coveragePassScore: string = getInput('coverage-pass-score');
// get comment input
Expand All @@ -118,6 +122,7 @@ const getInputs = (
doStaticAnalysis,
doCodeFormatting,
doTests,
testResultsPath,
createComment,
];
};
Expand All @@ -136,6 +141,7 @@ export async function run(): Promise<void> {
doStaticAnalysis,
doCodeFormatting,
doTests,
testResultsPath,
createComment,
] = getInputs(isLocal);

Expand Down Expand Up @@ -182,10 +188,13 @@ export async function run(): Promise<void> {
: undefined;

const testingStr: StepResponse | undefined = doTests
? await testing({
label: "Testing",
command: "npm run test -- --coverage --reporter json",
})
? await testing(
{
label: "Testing",
command: "npm run test -- --coverage",
},
testResultsPath,
)
: undefined;

const tsDocStr: StepResponse | undefined = doTests
Expand Down
56 changes: 53 additions & 3 deletions src/scripts/testing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { setFailed } from "@actions/core";
import { exec } from "@actions/exec";
import * as fs from "fs";
import {
buildComment,
Command,
Expand All @@ -24,9 +26,57 @@ export const playwright = async (command: Command): Promise<StepResponse> => {
return await commandComment(command);
};

/// TODO: format this comment
export const testing = async (command: Command): Promise<StepResponse> => {
const [response, outputStr] = await runCommand(command);
export const testing = async (
command: Command,
testResultsPath: string,
): Promise<StepResponse> => {
let response: StepResponse = { output: "", error: false };
let outputStr = "";
try {
await exec(command.command, [], {
listeners: {
stdout: (data) => {
outputStr += data.toString();
},
},
});
} catch (error) {
response.error = true;
setFailed(`Failed ${command.label}: ${error as string}`);
}

let testResults = "";
try {
testResults = fs.readFileSync(testResultsPath, "utf8");
} catch (error) {
response.error = true;
setFailed(`Failed to read test results: ${error as string}`);
}

if (response.error) {
outputStr +=
"<table><tr><th>File</th><th>Test Name</th><th>Line</th><th>Message</th></tr>";

const parser = new DOMParser();
const doc = parser.parseFromString(testResults, "text/xml");

const testCases = doc.getElementsByTagName("testcase");
for (let i = 0; i < testCases.length; i++) {

Check warning on line 64 in src/scripts/testing.ts

View workflow job for this annotation

GitHub Actions / code-quality

Expected a `for-of` loop instead of a `for` loop with this simple iteration
const testCase = testCases[i];
const testCaseName = testCase.getAttribute("name");
const testCaseFailure = testCase.getElementsByTagName("failure");
if (testCaseFailure) {
const testCaseFile = testCase.getAttribute("file");
const testCaseLine = testCase.getAttribute("line");
const testCaseMessage = testCase
.getElementsByTagName("failure")[0]
.getAttribute("message");
outputStr += `<tr><td>${testCaseFile}</td><td>${testCaseName}</td><td>${testCaseLine}</td><td>${testCaseMessage}</td></tr>`;
}
}

outputStr += "</table>";
}
return await buildComment(response, outputStr, command.label);

// const commands = [
Expand Down

0 comments on commit 579d72f

Please sign in to comment.