generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.test.js
249 lines (226 loc) · 8.4 KB
/
index.test.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const process = require('process');
const cp = require('child_process');
const path = require('path');
const { fail } = require('assert');
const getErrorOutput = (error) => {
const output = Array(...error.output)
.filter((line) => !!line)
.map((line) => line.toString().replace(/%0A/g, '\n'))
.filter((line) => line.trim().length > 0)
.join('\n');
return output;
};
afterEach(() => {
delete process.env['INPUT_PATH'];
delete process.env['INPUT_EXCLUDE'];
delete process.env['INPUT_MIN_COVERAGE'];
});
test('empty LCOV throws an error', () => {
const lcovPath = './fixtures/lcov.empty.info';
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();
const errorMessage = err.stdout.toString().replace(/%0A/g, '\n');
expect(errorMessage).toContain(
`❌ Found an empty lcov file at "${lcovPath}".
An empty lcov file was found but with no coverage data. This might be because \
you have no test files or your tests are not generating any coverage data.
`,
);
}
});
test('non-existent LCOV throws an error', () => {
const lcovPath = './fixtures/not-found.info';
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();
const errorMessage = err.stdout.toString().replace(/%0A/g, '\n');
expect(errorMessage).toContain(
`❌ Failed to find an lcov file at ${lcovPath}.
Make sure to generate an lcov file before running VeryGoodCoverage and set the \
path input to the correct location.
For example:
uses: VeryGoodOpenSource/very_good_coverage@v2
with:
path: 'my_project/coverage/lcov.info'
`,
);
}
});
test('invalid LCOV format throws an error', () => {
const lcovPath = './fixtures/lcov.error.info';
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env }).toString();
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();
const errorMessage = err.stdout.toString();
expect(errorMessage).toContain('parsing error!');
}
});
test('completes when the coverage is 100 and min_coverage is not provided', () => {
const lcovPath = './fixtures/lcov.100.info';
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
cp.execSync(`node ${ip}`, { env: process.env }).toString();
});
test('logs message when the coverage is 100 and min_coverage is not provided', () => {
const lcovPath = './fixtures/lcov.100.info';
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
let result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain('Coverage: 100%.');
});
test('completes when the coverage is higher than the threshold after excluding files', () => {
const lcovPath = './fixtures/lcov.100.info';
const exclude = '**/*_observer.dart';
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_EXCLUDE'] = exclude;
const ip = path.join(__dirname, 'index.js');
cp.execSync(`node ${ip}`, { env: process.env }).toString();
});
test('fails when the coverage is not 100 and min_coverage is not provided', () => {
const lcovPath = './fixtures/lcov.95.info';
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('95 is less than min_coverage 100');
}
});
test('fails when the coverage is below the min_coverage, even if we exclude files', () => {
const lcovPath = './fixtures/lcov.100.info';
const exclude = '**/does_not_exist.dart';
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_EXCLUDE'] = exclude;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env }).toString();
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();
}
});
test('show message when the coverage is above the given min_threshold', () => {
const lcovPath = './fixtures/lcov.95.info';
const minCoverage = 80;
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_MIN_COVERAGE'] = minCoverage;
const ip = path.join(__dirname, 'index.js');
cp.execSync(`node ${ip}`, { env: process.env }).toString();
});
test('show message when the coverage is above the given min_threshold', () => {
const lcovPath = './fixtures/lcov.95.info';
const minCoverage = 80;
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_MIN_COVERAGE'] = minCoverage;
const ip = path.join(__dirname, 'index.js');
cp.execSync(`node ${ip}`, { env: process.env }).toString();
let result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain(
'Coverage: 95%.\n95 is greater than or equal to min_coverage 80.',
);
});
test('fails when the coverage is below the given min_threshold', () => {
const lcovPath = './fixtures/lcov.95.info';
const minCoverage = 98;
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_MIN_COVERAGE'] = minCoverage;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env }).toString();
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();
}
});
test('shows lines that are missing coverage when failure occurs', () => {
const lcovPath = './fixtures/lcov.95.info';
const minCoverage = 100;
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_MIN_COVERAGE'] = minCoverage;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env }).toString();
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('Lines not covered');
expect(output).toContain(
'/Users/felix/Development/github.com/felangel/bloc/packages/bloc/lib/src/bloc_observer.dart: 20, 27, 36, 43, 51',
);
}
});
test('shows lines that are missing coverage when coverage is less than 100%', () => {
const lcovPath = './fixtures/lcov.95.info';
const minCoverage = 80;
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_MIN_COVERAGE'] = minCoverage;
const ip = path.join(__dirname, 'index.js');
let result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain('Lines not covered');
expect(result).toContain(
'/Users/felix/Development/github.com/felangel/bloc/packages/bloc/lib/src/bloc_observer.dart: 20, 27, 36, 43, 51',
);
});
test('reports 0 coverage when no lines are found ', () => {
const lcovPath = './fixtures/lcov.0.info';
const minCoverage = 100;
process.env['INPUT_PATH'] = lcovPath;
process.env['INPUT_MIN_COVERAGE'] = minCoverage;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env }).toString();
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();
const errorMessage = err.stdout.toString();
expect(errorMessage).toContain('0 is less than min_coverage 100');
}
});
test('fails when min_coverage is not a number', () => {
process.env['INPUT_MIN_COVERAGE'] = '10%';
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('❌ Failed to parse min_coverage.');
}
});
test('fails when min_coverage is lower than 0', () => {
process.env['INPUT_MIN_COVERAGE'] = -1;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('❌ Failed to parse min_coverage.');
}
});
test('fails when min_coverage is greater than 100', () => {
process.env['INPUT_MIN_COVERAGE'] = 101;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('❌ Failed to parse min_coverage.');
}
});