Skip to content

Commit 2def359

Browse files
authoredMar 21, 2023
fix: updated ebadplatform messaging to be generated based on the error (#6277)
1 parent 2e3b11a commit 2def359

File tree

3 files changed

+80
-26
lines changed

3 files changed

+80
-26
lines changed
 

‎lib/utils/error-message.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,34 @@ const errorMessage = (er, npm) => {
247247
break
248248

249249
case 'EBADPLATFORM': {
250-
const validOs =
251-
er.required && er.required.os && er.required.os.join
252-
? er.required.os.join(',')
253-
: er.required.os
254-
const validArch =
255-
er.required && er.required.cpu && er.required.cpu.join
256-
? er.required.cpu.join(',')
257-
: er.required.cpu
258-
const expected = { os: validOs, arch: validArch }
259-
const actual = { os: process.platform, arch: process.arch }
250+
const actual = er.current
251+
const expected = { ...er.required }
252+
const checkedKeys = []
253+
for (const key in expected) {
254+
if (Array.isArray(expected[key]) && expected[key].length > 0) {
255+
expected[key] = expected[key].join(',')
256+
checkedKeys.push(key)
257+
} else if (expected[key] === undefined ||
258+
Array.isArray(expected[key]) && expected[key].length === 0) {
259+
delete expected[key]
260+
delete actual[key]
261+
} else {
262+
checkedKeys.push(key)
263+
}
264+
}
265+
266+
const longestKey = Math.max(...checkedKeys.map((key) => key.length))
267+
const detailEntry = []
268+
for (const key of checkedKeys) {
269+
const padding = key.length === longestKey
270+
? 1
271+
: 1 + (longestKey - key.length)
272+
273+
// padding + 1 because 'actual' is longer than 'valid'
274+
detailEntry.push(`Valid ${key}:${' '.repeat(padding + 1)}${expected[key]}`)
275+
detailEntry.push(`Actual ${key}:${' '.repeat(padding)}${actual[key]}`)
276+
}
277+
260278
short.push([
261279
'notsup',
262280
[
@@ -270,12 +288,7 @@ const errorMessage = (er, npm) => {
270288
])
271289
detail.push([
272290
'notsup',
273-
[
274-
'Valid OS: ' + validOs,
275-
'Valid Arch: ' + validArch,
276-
'Actual OS: ' + process.platform,
277-
'Actual Arch: ' + process.arch,
278-
].join('\n'),
291+
detailEntry.join('\n'),
279292
])
280293
break
281294
}

‎tap-snapshots/test/lib/utils/error-message.js.test.cjs

+30-10
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,37 @@ Object {
241241
Array [
242242
"notsup",
243243
String(
244-
Valid OS: !yours,mine
245-
Valid Arch: x867,x5309
246-
Actual OS: posix
247-
Actual Arch: x64
244+
Valid os: !yours,mine
245+
Actual os: posix
246+
Valid cpu: x867,x5309
247+
Actual cpu: x64
248248
),
249249
],
250250
],
251251
"summary": Array [
252252
Array [
253253
"notsup",
254-
"Unsupported platform for lodash@1.0.0: wanted {/"os/":/"!yours,mine/",/"arch/":/"x867,x5309/"} (current: {/"os/":/"posix/",/"arch/":/"x64/"})",
254+
"Unsupported platform for lodash@1.0.0: wanted {/"os/":/"!yours,mine/",/"cpu/":/"x867,x5309/"} (current: {/"os/":/"posix/",/"cpu/":/"x64/"})",
255+
],
256+
],
257+
}
258+
`
259+
260+
exports[`test/lib/utils/error-message.js TAP bad platform omits keys with no required value > must match snapshot 1`] = `
261+
Object {
262+
"detail": Array [
263+
Array [
264+
"notsup",
265+
String(
266+
Valid os: !yours,mine
267+
Actual os: posix
268+
),
269+
],
270+
],
271+
"summary": Array [
272+
Array [
273+
"notsup",
274+
"Unsupported platform for lodash@1.0.0: wanted {/"os/":/"!yours,mine/"} (current: {/"os/":/"posix/"})",
255275
],
256276
],
257277
}
@@ -263,17 +283,17 @@ Object {
263283
Array [
264284
"notsup",
265285
String(
266-
Valid OS: !yours
267-
Valid Arch: x420
268-
Actual OS: posix
269-
Actual Arch: x64
286+
Valid os: !yours
287+
Actual os: posix
288+
Valid cpu: x420
289+
Actual cpu: x64
270290
),
271291
],
272292
],
273293
"summary": Array [
274294
Array [
275295
"notsup",
276-
"Unsupported platform for lodash@1.0.0: wanted {/"os/":/"!yours/",/"arch/":/"x420/"} (current: {/"os/":/"posix/",/"arch/":/"x64/"})",
296+
"Unsupported platform for lodash@1.0.0: wanted {/"os/":/"!yours/",/"cpu/":/"x420/"} (current: {/"os/":/"posix/",/"cpu/":/"x64/"})",
277297
],
278298
],
279299
}

‎test/lib/utils/error-message.js

+21
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,27 @@ t.test('bad platform', async t => {
384384
t.matchSnapshot(errorMessage(er))
385385
t.end()
386386
})
387+
t.test('omits keys with no required value', t => {
388+
const er = Object.assign(new Error('a bad plat'), {
389+
pkgid: 'lodash@1.0.0',
390+
current: {
391+
os: 'posix',
392+
cpu: 'x64',
393+
libc: 'musl',
394+
},
395+
required: {
396+
os: ['!yours', 'mine'],
397+
libc: [], // empty arrays should also lead to a key being removed
398+
cpu: undefined, // XXX npm-install-checks sets unused keys to undefined
399+
},
400+
code: 'EBADPLATFORM',
401+
})
402+
const msg = errorMessage(er)
403+
t.matchSnapshot(msg)
404+
t.notMatch(msg, /Valid cpu/, 'omits cpu from message')
405+
t.notMatch(msg, /Valid libc/, 'omits libc from message')
406+
t.end()
407+
})
387408
})
388409

389410
t.test('explain ERESOLVE errors', async t => {

0 commit comments

Comments
 (0)
Please sign in to comment.