Skip to content

Commit

Permalink
basic code coverage support
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Nov 27, 2023
1 parent 937d7d2 commit a58617a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
36 changes: 33 additions & 3 deletions borp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import { parseArgs } from 'node:util'
import { tap, spec } from 'node:test/reporters'
import { mkdtemp, rm } from 'node:fs/promises'
import { finished } from 'node:stream/promises'
import { join } from 'node:path'
import runWithTypeScript from './lib/run.js'
import { Report } from 'c8'

let reporter
if (process.stdout.isTTY) {
Expand All @@ -18,7 +22,8 @@ const args = parseArgs({
only: { type: 'boolean', short: 'o' },
watch: { type: 'boolean', short: 'w' },
pattern: { type: 'string', short: 'p' },
concurrency: { type: 'string', short: 'c' }
concurrency: { type: 'string', short: 'c' },
coverage: { type: 'boolean', short: 'C' }
},
allowPositionals: true
})
Expand All @@ -27,13 +32,38 @@ if (args.values.concurrency) {
args.values.concurrency = parseInt(args.values.concurrency)
}


Check failure on line 35 in borp.js

View workflow job for this annotation

GitHub Actions / test (18.x)

More than 1 blank line not allowed

Check failure on line 35 in borp.js

View workflow job for this annotation

GitHub Actions / test (20.x)

More than 1 blank line not allowed

Check failure on line 35 in borp.js

View workflow job for this annotation

GitHub Actions / test (21.x)

More than 1 blank line not allowed
let covDir
if (args.values.coverage) {
covDir = await mkdtemp(join(process.cwd(), 'coverage-'))
process.env.NODE_V8_COVERAGE = covDir
}

const config = {
...args.values,
files: args.positionals,
pattern: args.values.pattern,
cwd: process.cwd()
}

;(await runWithTypeScript(config))
.compose(reporter)
const stream = await runWithTypeScript(config)

stream.compose(reporter)
.pipe(process.stdout)

await finished(stream)

if (covDir) {
const report = Report({
reporter: ['text'],
tempDirectory: covDir
})

try {
await report.run()
} catch (err) {
console.error(err)
} finally {
await rm(covDir, { recursive: true })
}
}
4 changes: 2 additions & 2 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { glob } from 'glob'
import { findUp } from 'find-up'
import { createRequire } from 'node:module'
import { resolve, join, dirname } from 'node:path'
import { access, readFile } from 'node:fs/promises'
import { access, readFile, mkdtemp } from 'node:fs/promises'

Check failure on line 6 in lib/run.js

View workflow job for this annotation

GitHub Actions / test (18.x)

'mkdtemp' is defined but never used

Check failure on line 6 in lib/run.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'mkdtemp' is defined but never used

Check failure on line 6 in lib/run.js

View workflow job for this annotation

GitHub Actions / test (21.x)

'mkdtemp' is defined but never used
import { execa } from 'execa'

async function isFileAccessible (filename, directory) {
Expand All @@ -17,7 +17,7 @@ async function isFileAccessible (filename, directory) {
}

export default async function runWithTypeScript (config) {
const { cwd } = config
const { cwd, coverage } = config

Check failure on line 20 in lib/run.js

View workflow job for this annotation

GitHub Actions / test (18.x)

'coverage' is assigned a value but never used

Check failure on line 20 in lib/run.js

View workflow job for this annotation

GitHub Actions / test (20.x)

'coverage' is assigned a value but never used

Check failure on line 20 in lib/run.js

View workflow job for this annotation

GitHub Actions / test (21.x)

'coverage' is assigned a value but never used
const tsconfigPath = await findUp('tsconfig.json', { cwd })

let prefix = ''
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"typescript": "^5.3.2"
},
"dependencies": {
"c8": "^8.0.1",
"execa": "^8.0.1",
"find-up": "^7.0.0",
"glob": "^10.3.10"
Expand Down

0 comments on commit a58617a

Please sign in to comment.