-
Notifications
You must be signed in to change notification settings - Fork 0
169 lines (135 loc) · 5.95 KB
/
test.yml
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: Test Project
on: push
jobs:
test:
name: Test Project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 'latest'
- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
cache: 'pnpm'
- name: Install project dependencies
run: pnpm install
- name: Install test dependencies
run: pnpm add --save-dev strip-ansi
- name: Create test file
# Create a test file that will run index.js from the project
# with Node.js and check that it roughly matches the
# following pattern (9 rows, 31 columns, with a space in the
# center and a hex code in the middle):
#
# ###############################
# ###############################
# ###############################
# ##### #####
# ##### #edff28 #####
# ##### #####
# ###############################
# ###############################
# ###############################
#
# The pattern will be matched in a forgiving way:
# - Unlimited number of spaces at the start of each line
# - Minimum 1 # character left and right border
# - Minimum 1 # character top and bottom border
# - Minimum 1 space surrounding the hex code
# - Minimum 11 columns
# - Minimum 5 rows
run: |
cat > test.js <<'END_SCRIPT'
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import stripAnsi from 'strip-ansi';
const execAsync = promisify(exec);
const { stdout: stdoutNoArgs1 } = await execAsync('node index.js');
const pattern =
/^( *#{11,}\n){1,} *#{1,} +#{1,}\n *#{1,} +(#[A-Fa-f0-9]{6}) +#{1,}\n *#{1,} +#{1,}\n( *#{11,}\n){1,}/m;
// Get hex color from the output
const matchNoArgs1 = stripAnsi(stdoutNoArgs1).match(pattern);
if (!matchNoArgs1) {
console.log('❌ `node index.js` (run 1): pattern did not match');
process.exit(1);
}
console.log('✔️ `node index.js` (run 1): pattern matched');
const [, , hexCodeNoArgs1] = matchNoArgs1;
const { stdout: stdoutNoArgs2 } = await execAsync('node index.js');
const match2 = stripAnsi(stdoutNoArgs2).match(pattern);
if (!match2) {
console.log('❌ `node index.js` (run 2): pattern did not match');
process.exit(1);
}
console.log('✔️ `node index.js` (run 2): pattern matched');
const [, , hexCodeNoArgs2] = match2;
const { stdout: stdoutNoArgs3 } = await execAsync('node index.js');
const match3 = stripAnsi(stdoutNoArgs3).match(pattern);
if (!match3) {
console.log('❌ `node index.js` (run 3): pattern did not match');
process.exit(1);
}
console.log('✔️ `node index.js` (run 3): pattern matched');
const [, , hexCodeNoArgs3] = match3;
if (hexCodeNoArgs1 === hexCodeNoArgs2 && hexCodeNoArgs2 === hexCodeNoArgs3) {
console.log(
`❌ \`node index.js\` (all 3 runs): the hex code was the same: ${hexCodeNoArgs1}`,
);
process.exit(1);
}
console.log(
`✔️ \`node index.js\` (all 3 runs): the hex code was different: ${hexCodeNoArgs1}, ${hexCodeNoArgs2}, ${hexCodeNoArgs3}`,
);
const { stdout: stdoutRed } = await execAsync('node index.js red');
const matchRed = stripAnsi(stdoutRed).match(pattern);
if (!matchRed) {
console.log('❌ `node index.js red`: pattern did not match');
process.exit(1);
}
console.log('✔️ `node index.js red`: pattern matched');
const [, , hexCodeRed] = matchRed;
function hexCodeLikeColor(hexCode, color) {
const r = parseInt(hexCode.slice(1, 3), 16);
const g = parseInt(hexCode.slice(3, 5), 16);
const b = parseInt(hexCode.slice(5, 7), 16);
if (color === 'red') return r >= Math.max(g, b);
const luminosity = 0.2126 * r + 0.7152 * g + 0.0722 * b; // #ffffff is 255
return (
b >=
Math.max(
r,
// Adjust green for turquoises
g - 10,
) && luminosity <= 128
); // blue dark
}
if (!hexCodeLikeColor(hexCodeRed, 'red')) {
console.log(
`❌ \`node index.js red\`: the hex code ${hexCodeRed} is not red`,
);
process.exit(1);
}
console.log(`✔️ \`node index.js red\`: the hex code ${hexCodeRed} is red`);
const { stdout: stdoutBlueDark } = await execAsync('node index.js blue dark');
const matchBlueDark = stripAnsi(stdoutBlueDark).match(pattern);
if (!matchBlueDark) {
console.log('❌ `node index.js blue dark`: pattern did not match');
process.exit(1);
}
console.log('✔️ `node index.js blue dark`: pattern matched');
const [, , hexCodeBlueDark] = matchBlueDark;
if (!hexCodeLikeColor(hexCodeBlueDark, 'blue')) {
console.log(
`❌ \`node index.js blue dark\`: the hex code ${hexCodeBlueDark} is not dark blue`,
);
process.exit(1);
}
console.log(
`✔️ \`node index.js blue dark\`: the hex code ${hexCodeBlueDark} is dark blue`,
);
console.log('✔️ All tests passed');
END_SCRIPT
- name: Run test file
run: node test.js