quicktype/test/test.ts

88 строки
2.6 KiB
TypeScript
Исходник Обычный вид История

import * as os from "os";
import * as _ from "lodash";
import { inParallel } from "./lib/multicore";
2018-04-29 01:08:43 +03:00
import { execAsync, Sample } from "./utils";
2017-10-23 16:38:47 +03:00
import { Fixture, allFixtures } from "./fixtures";
2018-02-16 04:41:55 +03:00
import { affectedFixtures, divideParallelJobs } from "./buildkite";
2017-09-11 07:08:10 +03:00
const exit = require("exit");
2017-09-29 13:06:26 +03:00
const CPUs = parseInt(process.env.CPUs || "0", 10) || os.cpus().length;
//////////////////////////////////////
// Test driver
/////////////////////////////////////
2018-02-16 04:41:55 +03:00
export type WorkItem = { sample: Sample; fixtureName: string };
async function main(sources: string[]) {
let fixtures = allFixtures;
2017-09-29 13:06:26 +03:00
const fixturesFromCmdline = process.env.FIXTURE;
if (fixturesFromCmdline) {
const fixtureNames = fixturesFromCmdline.split(",");
2018-02-16 04:41:55 +03:00
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(", ")}`);
}
}
2017-09-29 13:06:26 +03:00
// 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 => ({
2017-09-11 07:08:10 +03:00
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 }))
);
2018-02-16 04:41:55 +03:00
const tests = divideParallelJobs(_.concat(priority, others));
2017-09-11 07:08:10 +03:00
await inParallel({
queue: tests,
workers: CPUs,
setup: async () => {
testCLI();
console.error(`* Running ${tests.length} tests between ${fixtures.length} fixtures`);
2017-09-11 07:08:10 +03:00
for (const fixture of fixtures) {
await execAsync(`rm -rf test/runs`);
await execAsync(`mkdir -p test/runs`);
2017-09-11 07:08:10 +03:00
await fixture.setup();
}
},
map: async ({ sample, fixtureName }: WorkItem, index) => {
2017-09-29 13:06:26 +03:00
let fixture = _.find(fixtures, { name: fixtureName }) as Fixture;
2017-09-11 07:08:10 +03:00
try {
await fixture.runWithSample(sample, index, tests.length);
} catch (e) {
console.trace(e);
exit(1);
}
}
});
}
function testCLI() {
2017-09-11 07:08:10 +03:00
console.log(`* CLI sanity check`);
2018-04-29 01:08:43 +03:00
//const qt = (args: string) => exec(`node dist/cli/index.js ${args}`);
2018-04-29 01:08:43 +03:00
//console.log("* Ensure we can quicktype a URL");
//qt(`https://blockchain.info/latestblock`);
}
// skip 2 `node` args
main(process.argv.slice(2)).catch(reason => {
2017-09-11 07:08:10 +03:00
console.error(reason);
process.exit(1);
});