Skip to content

Commit 6267f54

Browse files
authored
fix: properly catch missing url opener error on interactive prompt (#7005)
Closes: #7002
1 parent 7788ef2 commit 6267f54

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/utils/open-url-prompt.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const readline = require('readline')
2-
const promiseSpawn = require('@npmcli/promise-spawn')
2+
const open = require('./open-url.js')
33

44
function print (npm, title, url) {
55
const json = npm.config.get('json')
@@ -63,8 +63,7 @@ const promptOpen = async (npm, url, title, prompt, emitter) => {
6363
return
6464
}
6565

66-
const command = browser === true ? null : browser
67-
await promiseSpawn.open(url, { command })
66+
await open(npm, url, 'Browser unavailable. Please open the URL manually')
6867
}
6968

7069
module.exports = promptOpen

tap-snapshots/test/lib/utils/open-url-prompt.js.test.cjs

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
* Make sure to inspect the output below. Do not ignore changes!
66
*/
77
'use strict'
8+
exports[`test/lib/utils/open-url-prompt.js TAP does not error when opener can not find command > Outputs extra Browser unavailable message and url 1`] = `
9+
npm home:
10+
https://www.npmjs.com
11+
Browser unavailable. Please open the URL manually:
12+
https://www.npmjs.com
13+
14+
`
15+
816
exports[`test/lib/utils/open-url-prompt.js TAP opens a url > must match snapshot 1`] = `
917
npm home:
1018
https://www.npmjs.com

test/lib/utils/open-url-prompt.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,24 @@ t.test('does not open url if canceled', async t => {
126126

127127
t.test('returns error when opener errors', async t => {
128128
const { error, openerUrl } = await mockOpenUrlPrompt(t, {
129-
openerResult: new Error('Opener failed'),
129+
openerResult: Object.assign(new Error('Opener failed'), { code: 1 }),
130130
})
131131

132132
t.match(error, /Opener failed/, 'got the correct error')
133133
t.equal(openerUrl, 'https://www.npmjs.com', 'did not open')
134134
})
135135

136+
t.test('does not error when opener can not find command', async t => {
137+
const { OUTPUT, error, openerUrl } = await mockOpenUrlPrompt(t, {
138+
// openerResult: new Error('Opener failed'),
139+
openerResult: Object.assign(new Error('Opener failed'), { code: 127 }),
140+
})
141+
142+
t.notOk(error, 'Did not error')
143+
t.equal(openerUrl, 'https://www.npmjs.com', 'did not open')
144+
t.matchSnapshot(OUTPUT, 'Outputs extra Browser unavailable message and url')
145+
})
146+
136147
t.test('throws "canceled" error on SIGINT', async t => {
137148
const emitter = new EventEmitter()
138149
const { open } = await mockOpenUrlPrompt(t, {

0 commit comments

Comments
 (0)