-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
102 lines (94 loc) · 2.48 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const { getInput, setFailed, setOutput } = require('@actions/core');
const DEFAULT_COMPARISON = 'exact';
const SUPPORTED_COMPARISONS = [
DEFAULT_COMPARISON,
'notexact',
'notequal',
'startsWith',
'notstartsWith',
'endsWith',
'notendsWith',
'contains',
'notcontains',
];
function throwAssertError(expected, actual, comparison) {
const msg = `Expected '${actual}' to ${comparison} '${expected}'`;
setOutput('result', 'failed');
throw new Error(msg);
}
async function runAction() {
const expected = getInput('expected');
const actual = getInput('actual');
const comparison = getInput('comparison') || DEFAULT_COMPARISON;
if (
!SUPPORTED_COMPARISONS.some(
(c) => c.toLowerCase() === comparison.toLowerCase(),
)
) {
throw new Error(
`Comparison input "${comparison}" not supported. Supported: [${SUPPORTED_COMPARISONS.join(
',',
)}]`,
);
}
switch (comparison.toLowerCase()) {
case 'exact':
case 'equal':
if (actual !== expected) {
throwAssertError(expected, actual, 'equal');
}
break;
case 'notequal':
case 'notexact':
if (actual === expected) {
throwAssertError(expected, actual, 'not equal');
}
break;
case 'startswith':
if (!actual.startsWith(expected)) {
throwAssertError(expected, actual, 'start with');
}
break;
case 'notstartswith':
if (actual.startsWith(expected)) {
throwAssertError(expected, actual, 'not start with');
}
break;
case 'endswith':
if (!actual.endsWith(expected)) {
throwAssertError(expected, actual, 'end with');
}
break;
case 'notendswith':
if (actual.endsWith(expected)) {
throwAssertError(expected, actual, 'not end with');
}
break;
case 'contains':
if (!actual.includes(expected)) {
throwAssertError(expected, actual, 'contain');
}
break;
case 'notcontains':
if (actual.includes(expected)) {
throwAssertError(expected, actual, 'not contain');
}
break;
default:
throw new Error(
`Comparison input ${comparison} supported but not yet implemented.`,
);
}
setOutput('result', 'passed');
}
// this is dumb but makes it easier to test
if (!process.env.TEST_RUNNING) {
runAction().catch((err) => {
setFailed(err.message), setOutput('result', 'failed');
process.exit(1);
});
}
// only exported for testing purposes
module.exports = {
runAction,
};