Skip to content

Commit 41e3244

Browse files
authored
chore: run e2e fixture generator in parallel (#222)
1 parent 453a23b commit 41e3244

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed
Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
const fs = require('fs');
3+
const fs = require('fs/promises');
44
const path = require('path');
55
const child_process = require('child_process');
66

@@ -9,32 +9,40 @@ const fixturesDir = 'fixtures/locks-e2e';
99

1010
const OSV_DETECTOR_CMD = process.env.OSV_DETECTOR_CMD ?? 'osv-detector';
1111

12-
const files = fs
13-
.readdirSync(path.join(root, fixturesDir), { withFileTypes: true })
14-
.filter(dirent => dirent.isFile() && !dirent.name.endsWith('.out.txt'));
12+
const runOsvDetector = async (...args) => {
13+
return new Promise((resolve, reject) => {
14+
const child = child_process.spawn(OSV_DETECTOR_CMD, args, {
15+
encoding: 'utf-8',
16+
cwd: root
17+
});
1518

16-
const runOsvDetector = (...args) => {
17-
const { stdout, stderr, status, error } = child_process.spawnSync(
18-
OSV_DETECTOR_CMD,
19-
args,
20-
{ encoding: 'utf-8', cwd: root }
21-
);
19+
let stdout = '';
20+
let stderr = '';
2221

23-
if (status > 1) {
24-
throw new Error(
25-
`osv-detector exited with unexpected code ${status}: ${stderr}`
26-
);
27-
}
22+
child.stdout.on('data', data => {
23+
stdout += data;
24+
});
2825

29-
if (error) {
30-
throw error;
31-
}
26+
child.stderr.on('data', data => {
27+
stderr += data;
28+
});
3229

33-
if (stderr.length) {
34-
console.warn('unexpected output to stderr', stderr);
35-
}
30+
child.on('error', reject);
31+
32+
child.on('close', status => {
33+
if (status > 1) {
34+
reject(
35+
new Error(
36+
`osv-detector exited with unexpected code ${status}: ${stderr}`
37+
)
38+
);
39+
} else if (stderr.length) {
40+
console.warn('unexpected output to stderr', stderr);
41+
}
3642

37-
return stdout;
43+
resolve(stdout);
44+
});
45+
});
3846
};
3947

4048
const wildcardDatabaseStats = output => {
@@ -44,19 +52,28 @@ const wildcardDatabaseStats = output => {
4452
);
4553
};
4654

47-
for (const file of files) {
48-
const [, parseAs] = /\d+-(.*)/u.exec(file.name) ?? [];
55+
const regenerateFixture = async fileName => {
56+
const [, parseAs] = /\d+-(.*)/u.exec(fileName) ?? [];
4957

50-
const p = path.join(fixturesDir, file.name);
58+
const p = path.join(fixturesDir, fileName);
5159
if (!parseAs) {
5260
console.error('could not determine parser for', p);
5361
}
5462

55-
console.log('(re)generating', p, 'fixture', `(parsing as ${parseAs})`);
56-
const output = runOsvDetector(`${parseAs}:${p}`);
63+
const output = await runOsvDetector(`${parseAs}:${p}`);
5764

58-
fs.writeFileSync(
59-
path.join(root, fixturesDir, `${file.name}.out.txt`),
65+
console.log('(re)generated', p, 'fixture', `(parsed as ${parseAs})`);
66+
67+
await fs.writeFile(
68+
path.join(root, fixturesDir, `${fileName}.out.txt`),
6069
wildcardDatabaseStats(output)
6170
);
62-
}
71+
};
72+
73+
(async () => {
74+
const files = (
75+
await fs.readdir(path.join(root, fixturesDir), { withFileTypes: true })
76+
).filter(dirent => dirent.isFile() && !dirent.name.endsWith('.out.txt'));
77+
78+
await Promise.all(files.map(file => regenerateFixture(file.name)));
79+
})();

0 commit comments

Comments
 (0)