Skip to content

Commit

Permalink
chore: fix failing tests in windows + node 18
Browse files Browse the repository at this point in the history
Some of our tests were failing in windows after testing on node 18. The
reason was the inability to clean up the logs dir. This changes forces
a few tests to run in order and also cleans up any use of multiple
`t.testdir` calls in a single child test which can cause problems.
  • Loading branch information
lukekarrys committed Sep 7, 2022
1 parent fc9acd7 commit c656f96
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 276 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const LoadMockNpm = async (t, {
globalPrefixDir = { lib: {} },
config = {},
mocks = {},
otherDirs = {},
globals = null,
} = {}) => {
// Mock some globals with their original values so they get torn down
Expand Down Expand Up @@ -107,13 +108,15 @@ const LoadMockNpm = async (t, {
prefix: prefixDir,
cache: cacheDir,
global: globalPrefixDir,
other: otherDirs,
})
const dirs = {
testdir: dir,
prefix: path.join(dir, 'prefix'),
cache: path.join(dir, 'cache'),
globalPrefix: path.join(dir, 'global'),
home: path.join(dir, 'home'),
other: path.join(dir, 'other'),
}

// Set cache to testdir via env var so it is available when load is run
Expand Down
35 changes: 7 additions & 28 deletions test/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const { load: loadMockNpm } = require('../fixtures/mock-npm.js')
const cliMock = async (t, opts) => {
let exitHandlerArgs = null
let npm = null

const exitHandlerMock = (...args) => {
exitHandlerArgs = args
npm.unload()
}
exitHandlerMock.setNpm = _npm => npm = _npm
exitHandlerMock.setNpm = (_npm) => npm = _npm

const { Npm, outputs, logMocks, logs } = await loadMockNpm(t, { ...opts, init: false })
const cli = t.mock('../../lib/cli.js', {
Expand Down Expand Up @@ -172,37 +173,15 @@ t.test('load error calls error handler', async t => {
t.strictSame(exitHandlerCalled(), [err])
})

t.test('known broken node version', async t => {
const errors = []
let exitCode
const { cli } = await cliMock(t, {
globals: {
'console.error': (msg) => errors.push(msg),
'process.version': '6.0.0',
'process.exit': e => exitCode = e,
},
})
await cli(process)
t.match(errors, [
'ERROR: npm is known not to run on Node.js 6.0.0',
'You\'ll need to upgrade to a newer Node.js version in order to use this',
'version of npm. You can find the latest version at https://nodejs.org/',
])
t.match(exitCode, 1)
})

t.test('unsupported node version', async t => {
const errors = []
const { cli } = await cliMock(t, {
const { cli, logs } = await cliMock(t, {
globals: {
'console.error': (msg) => errors.push(msg),
'process.version': '12.6.0',
},
})
await cli(process)
t.match(errors, [
'npm does not support Node.js 12.6.0',
'You should probably upgrade to a newer version of node as we',
'can\'t make any promises that npm will work with this version.',
])
t.match(
logs.warn[0][1],
/npm v.* does not support Node\.js 12\.6\.0\./
)
})
Loading

0 comments on commit c656f96

Please sign in to comment.