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: don't suggest npm update outside of valid engine range #8050

Merged
merged 5 commits into from
Feb 19, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: test refactor
milaninfy committed Feb 19, 2025
commit bf1ea3de8a3c84deeda13c7d67e6443f718ece08
104 changes: 33 additions & 71 deletions test/lib/cli/update-notifier.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ const { basename } = require('node:path')
const tmock = require('../../fixtures/tmock')
const mockNpm = require('../../fixtures/mock-npm')
const MockRegistry = require('@npmcli/mock-registry')
const pacote = require('pacote')
const mockGlobals = require('@npmcli/mock-globals')

const CURRENT_VERSION = '123.420.69'
@@ -90,22 +89,7 @@ const runUpdateNotifier = async (t, {
},
}

const MANIFEST_REQUEST = []
const mockPacote = {
manifest: async (spec, ...opts) => {
if (!spec.match(/^npm@/)) {
t.fail('no pacote manifest allowed for non npm packages')
}
MANIFEST_REQUEST.push(spec)
if (PACOTE_ERROR) {
throw PACOTE_ERROR
}
return pacote.manifest(spec, ...opts)
},
}

const mocks = {
pacote: mockPacote,
'node:fs/promises': mockFs,
'{ROOT}/package.json': { version },
'ci-info': { isCI: false, name: null },
@@ -136,30 +120,27 @@ const runUpdateNotifier = async (t, {
return {
wroteFile,
result,
MANIFEST_REQUEST,
}
}

t.test('duration has elapsed, no updates', async t => {
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t)
const { wroteFile, result } = await runUpdateNotifier(t)
t.equal(wroteFile, true)
t.not(result)
t.equal(MANIFEST_REQUEST.length, 1)
})

t.test('situations in which we do not notify', t => {
t.test('nothing to do if notifier disabled', async t => {
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, {
const { wroteFile, result } = await runUpdateNotifier(t, {
PACOTE_MOCK_REQ_COUNT: 0,
'update-notifier': false,
})
t.equal(wroteFile, false)
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, [], 'no requests for manifests')
})

t.test('do not suggest update if already updating', async t => {
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, {
const { wroteFile, result } = await runUpdateNotifier(t, {
PACOTE_MOCK_REQ_COUNT: 0,
command: 'install',
prefixDir: { 'package.json': `{"name":"${t.testName}"}` },
@@ -168,11 +149,10 @@ t.test('situations in which we do not notify', t => {
})
t.equal(wroteFile, false)
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, [], 'no requests for manifests')
})

t.test('do not suggest update if already updating with spec', async t => {
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, {
const { wroteFile, result } = await runUpdateNotifier(t, {
PACOTE_MOCK_REQ_COUNT: 0,
command: 'install',
prefixDir: { 'package.json': `{"name":"${t.testName}"}` },
@@ -181,81 +161,68 @@ t.test('situations in which we do not notify', t => {
})
t.equal(wroteFile, false)
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, [], 'no requests for manifests')
})

t.test('do not update if same as latest', async t => {
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t)
const { wroteFile, result } = await runUpdateNotifier(t)
t.equal(wroteFile, true)
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, ['npm@*'], 'requested latest version')
})
t.test('check if stat errors (here for coverage)', async t => {
const STAT_ERROR = new Error('blorg')
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, { STAT_ERROR })
const { wroteFile, result } = await runUpdateNotifier(t, { STAT_ERROR })
t.equal(wroteFile, true)
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, ['npm@*'], 'requested latest version')
})
t.test('ok if write errors (here for coverage)', async t => {
const WRITE_ERROR = new Error('grolb')
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, { WRITE_ERROR })
const { wroteFile, result } = await runUpdateNotifier(t, { WRITE_ERROR })
t.equal(wroteFile, true)
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, ['npm@*'], 'requested latest version')
})
t.test('ignore pacote failures (here for coverage)', async t => {
const PACOTE_ERROR = new Error('pah-KO-tchay')
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, {
const { wroteFile, result } = await runUpdateNotifier(t, {
PACOTE_ERROR, PACOTE_MOCK_REQ_COUNT: 0,
})
t.equal(result, null)
t.equal(wroteFile, true)
t.strictSame(MANIFEST_REQUEST, ['npm@*'], 'requested latest version')
})
t.test('do not update if newer than latest, but same as next', async t => {
const {
wroteFile,
result,
MANIFEST_REQUEST,
} = await runUpdateNotifier(t, { version: NEXT_VERSION })
t.equal(result, null)
t.equal(wroteFile, true)
const reqs = ['npm@*', `npm@^${NEXT_VERSION}`]
t.strictSame(MANIFEST_REQUEST, reqs, 'requested latest and next versions')
})
t.test('do not update if on the latest beta', async t => {
const {
wroteFile,
result,
MANIFEST_REQUEST,
} = await runUpdateNotifier(t, { version: CURRENT_BETA })
t.equal(result, null)
t.equal(wroteFile, true)
const reqs = [`npm@^${CURRENT_BETA}`]
t.strictSame(MANIFEST_REQUEST, reqs, 'requested latest and next versions')
})

t.test('do not update in CI', async t => {
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, { mocks: {
const { wroteFile, result } = await runUpdateNotifier(t, { mocks: {
'ci-info': { isCI: true, name: 'something' },
},
PACOTE_MOCK_REQ_COUNT: 0 })
t.equal(wroteFile, false)
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, [], 'no requests for manifests')
})

t.test('only check weekly for GA releases', async t => {
// One week (plus five minutes to account for test environment fuzziness)
const STAT_MTIME = Date.now() - 1000 * 60 * 60 * 24 * 7 + 1000 * 60 * 5
const { wroteFile, result, MANIFEST_REQUEST } = await runUpdateNotifier(t, {
const { wroteFile, result } = await runUpdateNotifier(t, {
STAT_MTIME,
PACOTE_MOCK_REQ_COUNT: 0,
})
t.equal(wroteFile, false, 'duration was not reset')
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, [], 'no requests for manifests')
})

t.test('only check daily for betas', async t => {
@@ -264,54 +231,49 @@ t.test('situations in which we do not notify', t => {
const {
wroteFile,
result,
MANIFEST_REQUEST,
} = await runUpdateNotifier(t, { STAT_MTIME, version: HAVE_BETA, PACOTE_MOCK_REQ_COUNT: 0 })
t.equal(wroteFile, false, 'duration was not reset')
t.equal(result, null)
t.strictSame(MANIFEST_REQUEST, [], 'no requests for manifests')
})

t.end()
})

t.test('notification situation with engine compatibility', async t => {
// no version which are greater than node 1.0.0 should be selected.
mockGlobals(t, { 'process.version': 'v1.0.0' }, { replace: true })

const {
wroteFile,
result,
} = await runUpdateNotifier(t, {
version: NEXT_VERSION_ENGINE_COMPATIBLE_MINOR,
PACOTE_MOCK_REQ_COUNT: 1 })

t.matchSnapshot(result)
t.equal(wroteFile, true)
})

t.test('notification situations', async t => {
const cases = {
[HAVE_BETA]: [`^{V}`],
[NEXT_PATCH]: [`*`, `^{V}`],
[NEXT_MINOR]: [`*`, `^{V}`],
[CURRENT_PATCH]: ['*'],
[CURRENT_MINOR]: ['*'],
[CURRENT_MAJOR]: ['*'],
[HAVE_BETA]: 1,
[NEXT_PATCH]: 2,
[NEXT_MINOR]: 2,
[CURRENT_PATCH]: 1,
[CURRENT_MINOR]: 1,
[CURRENT_MAJOR]: 1,
}

for (const [version, reqs] of Object.entries(cases)) {
for (const [version, noOfRequests] of Object.entries(cases)) {
for (const color of [false, 'always']) {
await t.test(`${version} - color=${color}`, async t => {
const {
wroteFile,
result,
MANIFEST_REQUEST,
} = await runUpdateNotifier(t, { version, color, PACOTE_MOCK_REQ_COUNT: reqs.length })
} = await runUpdateNotifier(t, { version, color, PACOTE_MOCK_REQ_COUNT: noOfRequests })
t.matchSnapshot(result)
t.equal(wroteFile, true)
t.strictSame(MANIFEST_REQUEST, reqs.map(r => `npm@${r.replace('{V}', version)}`))
})
}
}
})

t.test('notification situation with engine compatibility', async t => {
mockGlobals(t, { 'process.version': 'v1.0.0' }, { replace: true })

const {
wroteFile,
result,
MANIFEST_REQUEST,
} = await runUpdateNotifier(t, {
version: NEXT_VERSION_ENGINE_COMPATIBLE_MINOR,
PACOTE_MOCK_REQ_COUNT: 1 })

t.matchSnapshot(result)
t.equal(wroteFile, true)
t.strictSame(MANIFEST_REQUEST, [`npm@*`])
})