Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if a test run looks like it's hung during coverage, kill it #1406

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
},
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