Skip to content

Commit

Permalink
clean up additional files generated due to foreground-child watchdog (#1
Browse files Browse the repository at this point in the history
)
  • Loading branch information
frattaro authored Aug 30, 2024
1 parent c87fa50 commit e4e4613
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
16 changes: 15 additions & 1 deletion bin/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const { cliWrapper, suppressEPIPE } = require('../lib/commands/helpers')
const { foregroundChild } = require('foreground-child')
const resolveFrom = require('resolve-from')
const NYC = require('../index.js')
const path = require('path')
const fs = require('fs')

// parse configuration and command-line arguments;
// we keep these values in a few different forms,
Expand Down Expand Up @@ -86,10 +88,22 @@ async function main () {
// set process.exitCode. Keep track so that both children are run, but
// a non-zero exit codes in either one leads to an overall non-zero exit code.
process.exitCode = 0
foregroundChild(childArgs, async (code) => {
foregroundChild(childArgs, async (code, signal, processInfo) => {
let exitCode = process.exitCode || code

try {
// clean up foreground-child watchdog process info
const parentDir = path.resolve(nyc.tempDirectory())
const dir = path.resolve(nyc.tempDirectory(), 'processinfo')
const files = await nyc.coverageFiles(dir)
for (let i = 0; i < files.length; i++) {
const data = await nyc.coverageFileLoad(files[i], dir)
if (data.pid === processInfo.watchdogPid) {
fs.unlinkSync(path.resolve(parentDir, files[i]))
fs.unlinkSync(path.resolve(dir, files[i]))
}
}

await nyc.writeProcessIndex()

nyc.maybePurgeSourceMapCache()
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"decamelize": "^1.2.0",
"find-cache-dir": "^3.2.0",
"find-up": "^4.1.0",
"foreground-child": "^3.0.0",
"foreground-child": "^3.3.0",
"get-package-type": "^0.1.0",
"glob": "^7.1.6",
"istanbul-lib-coverage": "^3.0.0",
Expand Down
4 changes: 1 addition & 3 deletions test/nyc-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ t.test('interprets first args after -- as Node.js execArgv', t => testSuccess(t,
args: ['--', '--expose-gc', path.resolve(fixturesCLI, 'gc.js')]
}))

// TODO; get this test to pass with `foreground-child@^3.0.0` or delete.
// See https://github.com/istanbuljs/nyc/pull/1546
t.skip('--show-process-tree displays a tree of spawned processes', t => testSuccess(t, {
t.test('--show-process-tree displays a tree of spawned processes', t => testSuccess(t, {
args: ['--show-process-tree', process.execPath, 'selfspawn-fibonacci.js', '5']
}))

Expand Down
13 changes: 3 additions & 10 deletions test/processinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ const resolvedJS = resolve(fixturesCLI, 'selfspawn-fibonacci.js')
rimraf.sync(resolve(fixturesCLI, tmp))
t.teardown(() => rimraf(resolve(fixturesCLI, tmp)))

// TODO; get this test to pass with `foreground-child@^3.0.0` or delete.
// See https://github.com/istanbuljs/nyc/pull/1546
//
// The subprocess is failing with the following error:
// " External ID blorp used by multiple processes"
t.skip('build some processinfo', t => {
t.test('build some processinfo', t => {
var args = [
bin, '-t', tmp,
node, 'selfspawn-fibonacci.js', '5'
Expand All @@ -43,8 +38,7 @@ t.skip('build some processinfo', t => {
})
})

// This test is skipped because it relies on the above "build some processinfo" test.
t.skip('validate the created processinfo data', async t => {
t.test('validate the created processinfo data', async t => {
const covs = (await fs.readdir(resolve(fixturesCLI, tmp)))
.filter(f => f !== 'processinfo')

Expand Down Expand Up @@ -73,8 +67,7 @@ t.skip('validate the created processinfo data', async t => {
}))
})

// This test is skipped because it relies on the above "build some processinfo" test.
t.skip('check out the index', async t => {
t.test('check out the index', async t => {
const indexFile = resolve(fixturesCLI, tmp, 'processinfo', 'index.json')
const indexJson = await fs.readFile(indexFile, 'utf-8')
const index = JSON.parse(indexJson)
Expand Down
6 changes: 3 additions & 3 deletions test/temp-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ t.test('creates the default \'tempDir\' when none is specified', async t => {
t.equal(cliFiles.includes('.temp_directory'), false)

const tempFiles = await fs.readdir(path.resolve(fixturesCLI, '.nyc_output'))
t.equal(tempFiles.length, 3) // two coverage files, and processinfo
t.equal(tempFiles.length, 2) // a coverage file, and processinfo
})

t.test('prefers \'tempDirectory\' to \'tempDir\'', async t => {
Expand All @@ -56,7 +56,7 @@ t.test('prefers \'tempDirectory\' to \'tempDir\'', async t => {
t.equal(cliFiles.includes('.temp_directory'), true)

const tempFiles = await fs.readdir(path.resolve(fixturesCLI, '.temp_directory'))
t.equal(tempFiles.length, 3)
t.equal(tempFiles.length, 2)
})

t.test('uses the \'tempDir\' option if \'tempDirectory\' is not set', async t => {
Expand All @@ -77,5 +77,5 @@ t.test('uses the \'tempDir\' option if \'tempDirectory\' is not set', async t =>
t.equal(cliFiles.includes('.temp_directory'), false)

const tempFiles = await fs.readdir(path.resolve(fixturesCLI, '.temp_dir'))
t.equal(tempFiles.length, 3)
t.equal(tempFiles.length, 2)
})

0 comments on commit e4e4613

Please sign in to comment.