Skip to content

Commit 4333c6a

Browse files
committed
fix: properly catch missing url opener error on interactive prompt
Closes: #7002
1 parent 7788ef2 commit 4333c6a

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

lib/utils/open-url-prompt.js

Lines changed: 2 additions & 3 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,26 @@ 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: new Error('Opener failed'),
130+
openerResult: Object.assign(new Error('Opener failed'), { code: 1 }),
130131
})
131132

133+
// console.log(error)
132134
t.match(error, /Opener failed/, 'got the correct error')
133135
t.equal(openerUrl, 'https://www.npmjs.com', 'did not open')
134136
})
135137

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

0 commit comments

Comments
 (0)