Skip to content

Commit d1479fb

Browse files
committed
adding helper
1 parent 6142cc3 commit d1479fb

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

tests/helper.mjs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { styleText } from "node:util";
2+
import core from "@actions/core";
3+
import commandLineUsage from "command-line-usage";
4+
5+
export const GITHUB_ACTIONS_OUTPUT = "GITHUB_ACTIONS_OUTPUT" in process.env;
6+
7+
export function log(...args) {
8+
if (GITHUB_ACTIONS_OUTPUT)
9+
core.info(args.join(" "));
10+
else
11+
console.log(...args);
12+
}
13+
14+
export function logInfo(...args) {
15+
const text = args.join(" ")
16+
if (GITHUB_ACTIONS_OUTPUT)
17+
core.info(styleText("yellow", text));
18+
else
19+
console.log(styleText("yellow", text));
20+
}
21+
22+
export function logError(...args) {
23+
let error;
24+
if (args.length == 1 && args[0] instanceof Error)
25+
error = args[0];
26+
const text = args.join(" ");
27+
if (GITHUB_ACTIONS_OUTPUT) {
28+
if (error?.stack)
29+
core.error(error.stack);
30+
else
31+
core.error(styleText("red", text));
32+
} else {
33+
if (error?.stack)
34+
console.error(styleText("red", error.stack));
35+
else
36+
console.error(styleText("red", text));
37+
}
38+
}
39+
40+
export async function logGroup(name, body) {
41+
if (GITHUB_ACTIONS_OUTPUT) {
42+
core.startGroup(name);
43+
} else {
44+
logInfo("=".repeat(80));
45+
logInfo(name);
46+
logInfo(".".repeat(80));
47+
}
48+
try {
49+
return await body();
50+
} finally {
51+
if (GITHUB_ACTIONS_OUTPUT)
52+
core.endGroup();
53+
}
54+
}
55+
56+
57+
export function printHelp(message = "", optionDefinitions) {
58+
const usage = commandLineUsage([
59+
{
60+
header: "Run all tests",
61+
},
62+
{
63+
header: "Options",
64+
optionList: optionDefinitions,
65+
},
66+
]);
67+
if (!message) {
68+
console.log(usage);
69+
process.exit(0);
70+
} else {
71+
console.error(message);
72+
console.error();
73+
console.error(usage);
74+
process.exit(1);
75+
}
76+
}
77+
78+
79+
export async function runTest(label, testFunction) {
80+
try {
81+
await logGroup(label, testFunction);
82+
logInfo("✅ Test completed!");
83+
} catch(e) {
84+
logError("❌ Test failed!");
85+
logError(e);
86+
return false;
87+
}
88+
return true;
89+
}

0 commit comments

Comments
 (0)