diff --git a/package.json b/package.json index 84c0439..71e45e0 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "works-on-my-machine", "description": "A no-dependency test runner", "version": "0.0.0", - "main": "dist/runner.js", + "main": "dist/cli.js", "license": "MIT", "packageManager": "yarn@3.5.0", "files": [ @@ -16,7 +16,7 @@ "prebuild": "rm -rf dist", "lint": "rome format src tests && rome check src tests", "lint:fix": "rome format src tests --write && rome check src tests --apply", - "test": "ts-node ./src/runner.ts ./tests", + "test": "ts-node ./src/cli.ts ./tests", "build": "tsc --project ." }, "devDependencies": { diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..1b8ce5a --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,37 @@ +#!/usr/bin/env node + +import helpText from './help' +import parseArgs from './argumentParser' +import { getContext, redText } from './utils' +import { setUpSocket, collectTests, collectCases, assignTestsToWorkers } from './runner' + +/* + * Logic executed when running the test runner CLI. + */ +;(async () => { + const args = parseArgs(process.argv) + + if (args.help) { + console.log(helpText) + return + } + + const context = getContext(args.runtimePath) + let server + + try { + server = setUpSocket(context.runnerSocket) + const collectedTests = await collectTests(args.targets) + await collectCases(context, collectedTests) + + await assignTestsToWorkers(context, collectedTests, args.workers) + + if (server.failure) throw new Error() + } catch (e) { + console.log(redText('Test run failed')) + } finally { + server?.close() + } +})().catch((e) => { + throw e +}) diff --git a/src/runner.ts b/src/runner.ts index 55aae28..10e4471 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -1,9 +1,5 @@ -#!/usr/bin/env node - -import { getContext, greenText, redText, exec, splitIntoBatches } from './utils' -import helpText from './help' +import { greenText, redText, exec, splitIntoBatches } from './utils' import { type Args, type IContext, type TestServer } from './types' -import parseArgs from './argumentParser' import { type Buffer } from 'buffer' import { promises as fs } from 'fs' @@ -16,7 +12,7 @@ class UnknownArgumentError extends Error {} * Collects test files recursively starting from the provided root * path. */ -async function collectTests(roots: Array): Promise> { +export async function collectTests(roots: Array): Promise> { const collectedHere = [] for (const root of roots) { @@ -46,7 +42,7 @@ async function collectTests(roots: Array): Promise> { * Splits the list of collected test files into `workerCount` batches and starts * worker processes. */ -async function assignTestsToWorkers(context: IContext, collectedPaths: Array, workerCount: number = 1) { +export async function assignTestsToWorkers(context: IContext, collectedPaths: Array, workerCount: number = 1) { const batchedCollectedPaths = splitIntoBatches(collectedPaths, workerCount) await Promise.all( @@ -56,7 +52,7 @@ async function assignTestsToWorkers(context: IContext, collectedPaths: Array, workerCount: number = 1) { +export async function collectCases(context: IContext, collectedPaths: Array, workerCount: number = 1) { const batchedCollectedPaths = splitIntoBatches(collectedPaths, workerCount) const batchResults = await Promise.all( @@ -72,7 +68,7 @@ async function collectCases(context: IContext, collectedPaths: Array, wo console.log(greenText(`Collected ${collectedCount} cases`)) } -function setUpSocket(socketPath: string): TestServer { +export function setUpSocket(socketPath: string): TestServer { const server: TestServer = net.createServer() server.listen(socketPath, () => { console.log('Listening for workers') @@ -93,33 +89,4 @@ function setUpSocket(socketPath: string): TestServer { }) return server -} /* - * Logic executed when running the test runner CLI. - */ -;(async () => { - const args = parseArgs(process.argv) - - if (args.help) { - console.log(helpText) - return - } - - const context = getContext(args.runtimePath) - let server - - try { - server = setUpSocket(context.runnerSocket) - const collectedTests = await collectTests(args.targets) - await collectCases(context, collectedTests) - - await assignTestsToWorkers(context, collectedTests, args.workers) - - if (server.failure) throw new Error() - } catch (e) { - console.log(redText('Test run failed')) - } finally { - server?.close() - } -})().catch((e) => { - throw e -}) +}