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: properly catch missing url opener error on interactive prompt #7005

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 2 additions & 3 deletions lib/utils/open-url-prompt.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const readline = require('readline')
const promiseSpawn = require('@npmcli/promise-spawn')
const open = require('./open-url.js')

function print (npm, title, url) {
const json = npm.config.get('json')
Expand Down Expand Up @@ -63,8 +63,7 @@ const promptOpen = async (npm, url, title, prompt, emitter) => {
return
}

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

module.exports = promptOpen
8 changes: 8 additions & 0 deletions tap-snapshots/test/lib/utils/open-url-prompt.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
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`] = `
npm home:
https://www.npmjs.com
Browser unavailable. Please open the URL manually:
https://www.npmjs.com

`

exports[`test/lib/utils/open-url-prompt.js TAP opens a url > must match snapshot 1`] = `
npm home:
https://www.npmjs.com
Expand Down
13 changes: 12 additions & 1 deletion test/lib/utils/open-url-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,24 @@ t.test('does not open url if canceled', async t => {

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

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

t.test('does not error when opener can not find command', async t => {
const { OUTPUT, error, openerUrl } = await mockOpenUrlPrompt(t, {
// openerResult: new Error('Opener failed'),
openerResult: Object.assign(new Error('Opener failed'), { code: 127 }),
})

t.notOk(error, 'Did not error')
t.equal(openerUrl, 'https://www.npmjs.com', 'did not open')
t.matchSnapshot(OUTPUT, 'Outputs extra Browser unavailable message and url')
})

t.test('throws "canceled" error on SIGINT', async t => {
const emitter = new EventEmitter()
const { open } = await mockOpenUrlPrompt(t, {
Expand Down