Skip to content

Commit

Permalink
fix: store unref promises for awaiting in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Apr 24, 2024
1 parent 10cf86f commit 347655a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/cli/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ module.exports = async (process, validateEngines) => {
return exitHandler()
}

const command = this.argv.shift()
const args = this.argv
const command = npm.argv.shift()
const args = npm.argv

if (!command) {
output.standard(this.usage)
output.standard(npm.usage)
process.exitCode = 1
return { exec: false }
}
Expand Down
5 changes: 3 additions & 2 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Npm {
return require(`./commands/${command}.js`)
}

unrefPromises = []
updateNotification = null
argv = []

Expand Down Expand Up @@ -224,11 +225,11 @@ class Npm {
log.verbose('argv', this.#argvClean.map(JSON.stringify).join(' '))
})

this.#logFile.load({
this.unrefPromises.push(this.#logFile.load({
path: this.logPath,
logsMax: this.config.get('logs-max'),
timing: this.config.get('timing'),
})
}))

this.#timers.load({
path: this.logPath,
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ const getMockNpm = async (t, { mocks, init, load, npm: npmOpts }) => {
})
}

async load () {
const res = await super.load()
await Promise.all(this.unrefPromises)
return res
}

async exec (...args) {
const [res, err] = await super.exec(...args).then((r) => [r]).catch(e => [null, e])
// This mimics how the exit handler flushes output for commands that have
Expand Down
9 changes: 1 addition & 8 deletions test/lib/cli/exit-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ t.test('handles unknown error with logs and debug file', async (t) => {
})

await exitHandler(err('Unknown error', 'ECODE'))
// force logfile cleaning logs to happen since those are purposefully not awaited
await require('timers/promises').setTimeout(200)

const fileLogs = await debugFile()
const fileLines = fileLogs.split('\n')
Expand All @@ -140,19 +138,14 @@ t.test('handles unknown error with logs and debug file', async (t) => {

t.equal(process.exitCode, 1)

let skippedLogs = 0
logs.forEach((logItem, i) => {
const logLines = logItem.split('\n').map(l => `${i} ${l}`)
for (const line of logLines) {
if (line.includes('logfile') && line.includes('cleaning')) {
skippedLogs++
continue
}
t.match(fileLogs.trim(), line, 'log appears in debug file')
}
})

t.equal(logs.length - skippedLogs, parseInt(lastLog) + 1)
t.equal(logs.length, parseInt(lastLog) + 1)
t.match(logs.error, [
'code ECODE',
'ERR SUMMARY Unknown error',
Expand Down

0 comments on commit 347655a

Please sign in to comment.