forked from glideapps/quicktype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
executable file
·87 lines (72 loc) · 2.63 KB
/
test.ts
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
import * as os from "os";
import * as _ from "lodash";
import { inParallel } from "./lib/multicore";
import { execAsync, Sample } from "./utils";
import { Fixture, allFixtures } from "./fixtures";
import { affectedFixtures, divideParallelJobs } from "./buildkite";
const exit = require("exit");
const CPUs = parseInt(process.env.CPUs || "0", 10) || os.cpus().length;
//////////////////////////////////////
// Test driver
/////////////////////////////////////
export type WorkItem = { sample: Sample; fixtureName: string };
async function main(sources: string[]) {
let fixtures = allFixtures;
const fixturesFromCmdline = process.env.FIXTURE;
if (fixturesFromCmdline) {
const fixtureNames = fixturesFromCmdline.split(",");
fixtures = _.filter(fixtures, fixture => _.some(fixtureNames, name => fixture.runForName(name)));
} else {
fixtures = affectedFixtures();
if (allFixtures.length !== fixtures.length) {
console.error(`* Running a subset of fixtures: ${fixtures.map(f => f.name).join(", ")}`);
}
}
// Get an array of all { sample, fixtureName } objects we'll run.
// We can't just put the fixture in there because these WorkItems
// will be sent in a message, removing all code.
const samples = _.map(fixtures, fixture => ({
fixtureName: fixture.name,
samples: fixture.getSamples(sources)
}));
const priority = _.flatMap(samples, x =>
_.map(x.samples.priority, s => ({ fixtureName: x.fixtureName, sample: s }))
);
const others = _.flatMap(samples, x =>
_.map(x.samples.others, s => ({ fixtureName: x.fixtureName, sample: s }))
);
const tests = divideParallelJobs(_.concat(priority, others));
await inParallel({
queue: tests,
workers: CPUs,
setup: async () => {
testCLI();
console.error(`* Running ${tests.length} tests between ${fixtures.length} fixtures`);
for (const fixture of fixtures) {
await execAsync(`rm -rf test/runs`);
await execAsync(`mkdir -p test/runs`);
await fixture.setup();
}
},
map: async ({ sample, fixtureName }: WorkItem, index) => {
let fixture = _.find(fixtures, { name: fixtureName }) as Fixture;
try {
await fixture.runWithSample(sample, index, tests.length);
} catch (e) {
console.trace(e);
exit(1);
}
}
});
}
function testCLI() {
console.log(`* CLI sanity check`);
//const qt = (args: string) => exec(`node dist/cli/index.js ${args}`);
//console.log("* Ensure we can quicktype a URL");
//qt(`https://blockchain.info/latestblock`);
}
// skip 2 `node` args
main(process.argv.slice(2)).catch(reason => {
console.error(reason);
process.exit(1);
});