/
test.js
executable file
·188 lines (167 loc) · 5.31 KB
/
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
#!/usr/bin/env node
'use strict';
const path = require('path');
const Listr = require('listr');
const tempy = require('tempy');
const execa = require('execa');
const del = require('del');
const chalk = require('chalk');
const {isCI} = require('ci-info');
const packages = new Map([
['ava', 'https://github.com/avajs/ava'],
['chalk', 'https://github.com/chalk/chalk'],
['wrap-ansi', 'https://github.com/chalk/wrap-ansi'],
['np', 'https://github.com/sindresorhus/np'],
['ora', 'https://github.com/sindresorhus/ora'],
['p-map', 'https://github.com/sindresorhus/p-map'],
['os-locale', 'https://github.com/sindresorhus/os-locale'],
['execa', 'https://github.com/sindresorhus/execa'],
['pify', 'https://github.com/sindresorhus/pify'],
['boxen', 'https://github.com/sindresorhus/boxen'],
['make-dir', 'https://github.com/sindresorhus/make-dir'],
['listr', 'http://github.com/SamVerschueren/listr'],
['listr-update-renderer', 'http://github.com/SamVerschueren/listr-update-renderer'],
['clinton', 'http://github.com/SamVerschueren/clinton'],
['bragg', 'http://github.com/SamVerschueren/bragg'],
['bragg-router', 'http://github.com/SamVerschueren/bragg-router'],
['dev-time', 'http://github.com/SamVerschueren/dev-time'],
['decode-uri-component', 'https://github.com/SamVerschueren/decode-uri-component'],
['to-ico', 'https://github.com/kevva/to-ico'],
['download', 'https://github.com/kevva/download'],
['brightness', 'https://github.com/kevva/brightness'],
['decompress', 'https://github.com/kevva/decompress'],
['npm-conf', 'https://github.com/kevva/npm-conf'],
['imagemin', 'https://github.com/imagemin/imagemin'],
['color-convert', 'https://github.com/qix-/color-convert'],
['eslint-plugin-unicorn', 'https://github.com/sindresorhus/eslint-plugin-unicorn'],
['ky', 'https://github.com/sindresorhus/ky'],
['query-string', 'https://github.com/sindresorhus/query-string'],
['meow', 'https://github.com/sindresorhus/meow'],
['globby', 'https://github.com/sindresorhus/globby'],
['emittery', 'https://github.com/sindresorhus/emittery'],
['p-queue', 'https://github.com/sindresorhus/p-queue'],
['pretty-bytes', 'https://github.com/sindresorhus/pretty-bytes'],
['normalize-url', 'https://github.com/sindresorhus/normalize-url'],
['pageres', 'https://github.com/sindresorhus/pageres'],
['got', 'https://github.com/sindresorhus/got']
]);
const typescriptPackages = new Set([
'pageres',
'got',
'p-queue'
]);
const cwd = path.join(__dirname, 'eslint-config-unicorn-tester');
const enrichErrors = (packageName, cliArguments, f) => async (...arguments_) => {
try {
return await f(...arguments_);
} catch (error) {
error.packageName = packageName;
error.cliArgs = cliArguments;
throw error;
}
};
const makeEslintTask = (packageName, destination, extraArguments = []) => {
const isTypescriptPackage = typescriptPackages.has(packageName);
const typescriptArguments = isTypescriptPackage ? ['--parser', '@typescript-eslint/parser', '--ext', '.ts'] : [];
const arguments_ = ['eslint', '--format', 'json', '--config', path.join(cwd, 'index.js'), destination, ...typescriptArguments, ...extraArguments];
return enrichErrors(packageName, arguments_, async () => {
let stdout;
let processError;
try {
({stdout} = await execa('npx', arguments_, {cwd, 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.eslintFile = file;
error.eslintMessage = message;
throw error;
}
}
}
});
};
const execute = name => {
const destination = tempy.directory();
return new Listr([
{
title: 'Cloning',
task: () => execa('git', ['clone', packages.get(name), '--single-branch', destination])
},
{
title: 'Running eslint',
task: makeEslintTask(name, destination)
},
{
title: 'Running eslint --fix',
task: makeEslintTask(name, destination, ['--fix-dry-run'])
},
{
title: 'Clean up',
task: () => del(destination, {force: true})
}
].map(({title, ...task}) => ({
title: [name, title].join(' / '),
...task
})), {
exitOnError: false
});
};
const list = new Listr([
{
title: 'Setup',
task: () => execa('npm', ['install'], {cwd})
},
{
title: 'Integration tests',
task: () => {
const tests = new Listr({concurrent: true});
for (const [name] of packages) {
tests.add([
{
title: name,
task: () => execute(name)
}
]);
}
return tests;
}
}
], {
renderer: isCI ? 'verbose' : 'default'
});
list.run()
.catch(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) {
console.error(chalk.gray(error2.eslintFile.filePath), chalk.gray(JSON.stringify(error2.eslintMessage, null, 2)));
}
}
} else {
console.error(error);
}
process.exit(1);
});