Skip to content

Commit

Permalink
fix: if a test run looks like it's hung during coverage, kill it
Browse files Browse the repository at this point in the history
Watch for test output that looks like the end of a successful test
run - if collecting coverage takes too long, kill the process but
do not cause the test run to fail.

Temporary workaround for #1206
  • Loading branch information
achingbrain committed Nov 1, 2023
1 parent 95a545f commit 8f1c940
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/cmds/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export default {
describe: 'Enable coverage output. Output is already in Istanbul JSON format and can be uploaded directly to codecov.',
type: 'boolean',
default: userConfig.test.cov
},
covTimeout: {
describe: 'How long to wait for coverage collection. Workaround for https://github.com/ipfs/aegir/issues/1206',
type: 'number',
default: userConfig.test.covTimeout
}
})
},
Expand Down
1 change: 1 addition & 0 deletions src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const defaults = {
bail: false,
progress: false,
cov: false,
covTimeout: 60000,
browser: {
config: {
buildConfig: {
Expand Down
52 changes: 50 additions & 2 deletions src/test/node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import { fileURLToPath } from 'url'
import { execa } from 'execa'
import kleur from 'kleur'
import merge from 'merge-options'
import * as tempy from 'tempy'

Expand Down Expand Up @@ -68,7 +69,7 @@ export default async function testNode (argv, execaOptions) {
const beforeEnv = before && before.env ? before.env : {}

// run mocha
await execa(exec, args,
const proc = execa(exec, args,
merge(
{
env: {
Expand All @@ -78,11 +79,58 @@ export default async function testNode (argv, execaOptions) {
},
preferLocal: true,
localDir: path.join(__dirname, '../..'),
stdio: 'inherit'
stdio: argv.cov ? 'pipe' : 'inherit'
},
execaOptions
)
)

let killedWhileCollectingCoverage = false

/** @type {ReturnType<setTimeout> | undefined} */
let timeout

if (argv.cov) {
proc.stderr?.addListener('data', (data) => {
process.stderr.write(data)
})

let lastLine = ''
proc.stdout?.addListener('data', (data) => {
process.stdout.write(data)

lastLine = data.toString()

if (lastLine.trim() !== '') {
// more output has been sent, reset timer
clearTimeout(timeout)
}

if (lastLine.match(/^ {2}\d+ (passing|pending)/m) != null) {
// if we see something that looks like the successful end of a mocha
// run, set a timer - if the process does not exit before the timer
// fires, kill it and log a warning, though don't cause the test run
// to fail
timeout = setTimeout(() => {
console.warn(kleur.red('!!! Collecting coverage has hung, killing process')) // eslint-disable-line no-console
console.warn(kleur.red('!!! See https://github.com/ipfs/aegir/issues/1206 for more information')) // eslint-disable-line no-console
killedWhileCollectingCoverage = true
proc.kill()
}, argv.covTimeout).unref()
}
})
}

Check warning on line 122 in src/test/node.js

View check run for this annotation

Codecov / codecov/patch

src/test/node.js#L94-L122

Added lines #L94 - L122 were not covered by tests

try {
await proc
} catch (err) {
if (!killedWhileCollectingCoverage) {
throw err
}

Check warning on line 129 in src/test/node.js

View check run for this annotation

Codecov / codecov/patch

src/test/node.js#L127-L129

Added lines #L127 - L129 were not covered by tests
} finally {
clearTimeout(timeout)
}

// after hook
await argv.fileConfig.test.after(argv, before)
}
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ interface TestOptions {
* Enable coverage output
*/
cov: boolean
/**
* How long to wait for collecting code coverage. Workaround for @see https://github.com/ipfs/aegir/issues/1206
*/
covTimeout: number
/**
* Runner enviroment
*/
Expand Down

0 comments on commit 8f1c940

Please sign in to comment.