-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
71 lines (65 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const core = require('@actions/core');
const lineReader = require('line-by-line');
const fs = require('fs');
try {
const regex = /(\s*[\w\d]+_test.go:\d+:)(.*?)(Test:\s+Test[\w\d]*?\S+)/gu; // Extracts only the failure from the logs (including whitespace)
const testResultsPath = core.getInput('test-results');
const customPackageName = core.getInput('package-name');
const workingDirectory = core.getInput('working-directory');
if (!fs.existsSync(testResultsPath)) {
core.warning(
`No file was found with the provided path: ${testResultsPath}.`
)
return
}
let obj = {};
let lr = new lineReader(testResultsPath);
lr.on('line', function (line) {
const currentLine = JSON.parse(line);
const testName = currentLine.Test;
if (typeof testName === "undefined") {
return;
}
let output = currentLine.Output;
if (typeof output === "undefined") {
return;
}
output = output.replace("\n", "%0A").replace("\r", "%0D")
// Strip github.com/owner/repo package from the path by default
let packageName = currentLine.Package.split("/").slice(3).join("/");
// If custom package is provided, strip custom package name from the path
if (customPackageName !== "") {
if (!currentLine.Package.startsWith(customPackageName)) {
core.warning(
`Expected ${currentLine.Package} to start with ${customPackageName} since "package-name" was provided.`
)
} else {
packageName = currentLine.Package.replace(customPackageName + "/", "")
}
}
if (workingDirectory !== "") {
packageName = workingDirectory + "/" + packageName
}
let newEntry = packageName + "/" + testName;
if (!obj.hasOwnProperty(newEntry)) {
obj[newEntry] = output;
} else {
obj[newEntry] += output;
}
});
lr.on('end', function () {
for (const [key, value] of Object.entries(obj)) {
if (value.includes("FAIL") && value.includes("_test.go")) {
var result;
while ((result = regex.exec(value)) !== null) {
const parts = result[0].split(":");
const file = key.split("/").slice(0, -2).join("/") + "/" + parts[0].trimStart();
const lineNumber = parts[1];
core.info(`::error file=${file},line=${lineNumber}::${result[0]}`);
}
}
}
});
} catch (error) {
core.setFailed(error.message);
}