/
test.mjs
187 lines (165 loc) · 4.14 KB
/
test.mjs
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
#!/usr/bin/env node
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import Listr from 'listr';
import execa from 'execa';
import chalk from 'chalk';
import {isCI} from 'ci-info';
import mem from 'mem';
import allProjects from './projects.mjs';
const dirname = path.dirname(fileURLToPath(import.meta.url));
const projectsArguments = process.argv.slice(2);
const projects = projectsArguments.length === 0
? allProjects
: allProjects.filter(({name}) => projectsArguments.includes(name));
const enrichErrors = (packageName, cliArguments, f) => async (...arguments_) => {
try {
return await f(...arguments_);
} catch (error) {
error.packageName = packageName;
error.cliArgs = cliArguments;
throw error;
}
};
const makeEslintTask = (project, destination) => {
const arguments_ = [
'eslint',
project.path || '.',
'--fix-dry-run',
'--no-eslintrc',
'--ext',
'.js,.ts,.vue',
'--format',
'json',
'--config',
path.join(dirname, 'config.js'),
];
for (const pattern of project.ignore) {
arguments_.push('--ignore-pattern', pattern);
}
return enrichErrors(project.name, arguments_, async () => {
let stdout;
let processError;
try {
({stdout} = await execa('npx', arguments_, {cwd: destination, localDir: dirname}));
} catch (error) {
({stdout} = error);
processError = error;
if (!stdout) {
throw error;
}
}
let files;
try {
files = JSON.parse(stdout);
} catch (error) {
console.error('Error while parsing eslint output:', error);
if (processError) {
throw processError;
}
throw error;
}
for (const file of files) {
for (const message of file.messages) {
if (message.fatal) {
const error = new Error(message.message);
error.eslintJob = {
destination,
project,
file,
};
error.eslintMessage = message;
throw error;
}
}
}
});
};
const getBranch = mem(async dirname => {
const {stdout} = await execa('git', ['branch', '--show-current'], {cwd: dirname});
return stdout;
});
const execute = project => {
const destination = project.location || path.join(dirname, 'fixtures', project.name);
return new Listr([
{
title: 'Cloning',
skip: () => fs.existsSync(destination) ? 'Project already downloaded.' : false,
task: () => execa('git', [
'clone',
project.repository,
'--single-branch',
'--depth',
'1',
destination,
]),
},
{
title: 'Running eslint',
task: makeEslintTask(project, destination),
},
].map(({title, task, skip}) => ({
title: `${project.name} / ${title}`,
skip,
task,
})), {
exitOnError: false,
});
};
const list = new Listr([
{
title: 'Setup',
task: () => execa('npm', ['install'], {cwd: dirname}),
},
{
title: 'Integration tests',
task: () => {
const tests = new Listr({concurrent: true});
for (const project of projects) {
tests.add([
{
title: project.name,
task: () => execute(project),
},
]);
}
return tests;
},
},
], {
renderer: isCI ? 'verbose' : 'default',
});
list.run()
.catch(async error => {
if (error.errors) {
for (const error2 of error.errors) {
console.error('\n', chalk.red.bold.underline(error2.packageName), chalk.gray('(' + error2.cliArgs.join(' ') + ')'));
console.error(error2.message);
if (error2.stderr) {
console.error(chalk.gray(error2.stderr));
}
if (error2.eslintMessage) {
const {file, project, destination} = error2.eslintJob;
const {line} = error2.eslintMessage;
if (project.repository) {
// eslint-disable-next-line no-await-in-loop
const branch = await getBranch(destination);
console.error(chalk.gray(`${project.repository}/blob/${branch}/${path.relative(destination, file.filePath)}#L${line}`));
} else {
console.error(chalk.gray(`${path.relative(destination, file.filePath)}#L${line}`));
}
console.error(chalk.gray(JSON.stringify(error2.eslintMessage, undefined, 2)));
}
}
} else {
console.error(error);
}
process.exit(1);
})
// Catch errors in last `.catch`
.catch(error => {
console.error(error);
process.exit(1);
});