refactor: split cli call from runner logic (#15)
This commit is contained in:
parent
786149b0d7
commit
33c3bb6afa
3 changed files with 45 additions and 41 deletions
|
@ -2,7 +2,7 @@
|
||||||
"name": "works-on-my-machine",
|
"name": "works-on-my-machine",
|
||||||
"description": "A no-dependency test runner",
|
"description": "A no-dependency test runner",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"main": "dist/runner.js",
|
"main": "dist/cli.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"packageManager": "yarn@3.5.0",
|
"packageManager": "yarn@3.5.0",
|
||||||
"files": [
|
"files": [
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
"prebuild": "rm -rf dist",
|
"prebuild": "rm -rf dist",
|
||||||
"lint": "rome format src tests && rome check src tests",
|
"lint": "rome format src tests && rome check src tests",
|
||||||
"lint:fix": "rome format src tests --write && rome check src tests --apply",
|
"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 ."
|
"build": "tsc --project ."
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
37
src/cli.ts
Normal file
37
src/cli.ts
Normal file
|
@ -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
|
||||||
|
})
|
|
@ -1,9 +1,5 @@
|
||||||
#!/usr/bin/env node
|
import { greenText, redText, exec, splitIntoBatches } from './utils'
|
||||||
|
|
||||||
import { getContext, greenText, redText, exec, splitIntoBatches } from './utils'
|
|
||||||
import helpText from './help'
|
|
||||||
import { type Args, type IContext, type TestServer } from './types'
|
import { type Args, type IContext, type TestServer } from './types'
|
||||||
import parseArgs from './argumentParser'
|
|
||||||
import { type Buffer } from 'buffer'
|
import { type Buffer } from 'buffer'
|
||||||
|
|
||||||
import { promises as fs } from 'fs'
|
import { promises as fs } from 'fs'
|
||||||
|
@ -16,7 +12,7 @@ class UnknownArgumentError extends Error {}
|
||||||
* Collects test files recursively starting from the provided root
|
* Collects test files recursively starting from the provided root
|
||||||
* path.
|
* path.
|
||||||
*/
|
*/
|
||||||
async function collectTests(roots: Array<string>): Promise<Array<string>> {
|
export async function collectTests(roots: Array<string>): Promise<Array<string>> {
|
||||||
const collectedHere = []
|
const collectedHere = []
|
||||||
|
|
||||||
for (const root of roots) {
|
for (const root of roots) {
|
||||||
|
@ -46,7 +42,7 @@ async function collectTests(roots: Array<string>): Promise<Array<string>> {
|
||||||
* Splits the list of collected test files into `workerCount` batches and starts
|
* Splits the list of collected test files into `workerCount` batches and starts
|
||||||
* worker processes.
|
* worker processes.
|
||||||
*/
|
*/
|
||||||
async function assignTestsToWorkers(context: IContext, collectedPaths: Array<string>, workerCount: number = 1) {
|
export async function assignTestsToWorkers(context: IContext, collectedPaths: Array<string>, workerCount: number = 1) {
|
||||||
const batchedCollectedPaths = splitIntoBatches(collectedPaths, workerCount)
|
const batchedCollectedPaths = splitIntoBatches(collectedPaths, workerCount)
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
|
@ -56,7 +52,7 @@ async function assignTestsToWorkers(context: IContext, collectedPaths: Array<str
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function collectCases(context: IContext, collectedPaths: Array<string>, workerCount: number = 1) {
|
export async function collectCases(context: IContext, collectedPaths: Array<string>, workerCount: number = 1) {
|
||||||
const batchedCollectedPaths = splitIntoBatches(collectedPaths, workerCount)
|
const batchedCollectedPaths = splitIntoBatches(collectedPaths, workerCount)
|
||||||
|
|
||||||
const batchResults = await Promise.all(
|
const batchResults = await Promise.all(
|
||||||
|
@ -72,7 +68,7 @@ async function collectCases(context: IContext, collectedPaths: Array<string>, wo
|
||||||
console.log(greenText(`Collected ${collectedCount} cases`))
|
console.log(greenText(`Collected ${collectedCount} cases`))
|
||||||
}
|
}
|
||||||
|
|
||||||
function setUpSocket(socketPath: string): TestServer {
|
export function setUpSocket(socketPath: string): TestServer {
|
||||||
const server: TestServer = net.createServer()
|
const server: TestServer = net.createServer()
|
||||||
server.listen(socketPath, () => {
|
server.listen(socketPath, () => {
|
||||||
console.log('Listening for workers')
|
console.log('Listening for workers')
|
||||||
|
@ -93,33 +89,4 @@ function setUpSocket(socketPath: string): TestServer {
|
||||||
})
|
})
|
||||||
|
|
||||||
return server
|
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
|
|
||||||
})
|
|
||||||
|
|
Reference in a new issue