2021-04-15 21:53:45 -04:00
|
|
|
const t = require('tap')
|
2023-01-16 22:38:23 -05:00
|
|
|
const tmock = require('../../fixtures/tmock')
|
2023-06-08 05:24:49 -07:00
|
|
|
const mockNpm = require('../../fixtures/mock-npm')
|
2021-02-08 16:16:45 -05:00
|
|
|
|
2023-06-08 05:24:49 -07:00
|
|
|
const mockOpenUrl = async (t, args, { openerResult, ...config } = {}) => {
|
|
|
|
let openerUrl = null
|
|
|
|
let openerOpts = null
|
|
|
|
|
|
|
|
const open = async (url, options) => {
|
|
|
|
openerUrl = url
|
|
|
|
openerOpts = options
|
|
|
|
if (openerResult) {
|
|
|
|
throw openerResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mock = await mockNpm(t, { config })
|
|
|
|
|
|
|
|
const openUrl = tmock(t, '{LIB}/utils/open-url.js', {
|
|
|
|
'@npmcli/promise-spawn': { open },
|
|
|
|
})
|
2021-02-08 16:16:45 -05:00
|
|
|
|
2023-06-08 05:24:49 -07:00
|
|
|
const openWithNpm = (...a) => openUrl(mock.npm, ...a)
|
2022-12-06 22:18:33 -05:00
|
|
|
|
2023-06-08 05:24:49 -07:00
|
|
|
if (args) {
|
|
|
|
await openWithNpm(...args)
|
2022-12-06 22:18:33 -05:00
|
|
|
}
|
2021-02-08 16:16:45 -05:00
|
|
|
|
2023-06-08 05:24:49 -07:00
|
|
|
return {
|
|
|
|
...mock,
|
|
|
|
openUrl: openWithNpm,
|
|
|
|
openerUrl: () => openerUrl,
|
|
|
|
openerOpts: () => openerOpts,
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 16:16:45 -05:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('opens a url', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t,
|
|
|
|
['https://www.npmjs.com', 'npm home'])
|
|
|
|
t.equal(openerUrl(), 'https://www.npmjs.com', 'opened the given url')
|
|
|
|
t.same(openerOpts(), { command: null }, 'passed command as null (the default)')
|
|
|
|
t.same(joinedOutput(), '', 'printed no output')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('returns error for non-https url', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openUrl, openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t)
|
2021-11-18 20:58:02 +00:00
|
|
|
await t.rejects(
|
2023-06-08 05:24:49 -07:00
|
|
|
openUrl('ftp://www.npmjs.com', 'npm home'),
|
2021-11-18 20:58:02 +00:00
|
|
|
/Invalid URL/,
|
|
|
|
'got the correct error'
|
|
|
|
)
|
2023-06-08 05:24:49 -07:00
|
|
|
t.equal(openerUrl(), null, 'did not open')
|
|
|
|
t.same(openerOpts(), null, 'did not open')
|
|
|
|
t.same(joinedOutput(), '', 'printed no output')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('returns error for file url', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openUrl, openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t)
|
2021-11-18 20:58:02 +00:00
|
|
|
await t.rejects(
|
2023-06-08 05:24:49 -07:00
|
|
|
openUrl('file:///usr/local/bin/ls', 'npm home'),
|
2021-11-18 20:58:02 +00:00
|
|
|
/Invalid URL/,
|
|
|
|
'got the correct error'
|
|
|
|
)
|
2023-06-08 05:24:49 -07:00
|
|
|
t.equal(openerUrl(), null, 'did not open')
|
|
|
|
t.same(openerOpts(), null, 'did not open')
|
|
|
|
t.same(joinedOutput(), '', 'printed no output')
|
2021-10-07 20:21:11 +00:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('file url allowed if explicitly asked for', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t,
|
|
|
|
['file:///man/page/npm-install', 'npm home', true])
|
|
|
|
t.equal(openerUrl(), 'file:///man/page/npm-install', 'opened the given url')
|
|
|
|
t.same(openerOpts(), { command: null }, 'passed command as null (the default)')
|
|
|
|
t.same(joinedOutput(), '', 'printed no output')
|
2021-11-18 20:58:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('returns error for non-parseable url', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openUrl, openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t)
|
2021-11-18 20:58:02 +00:00
|
|
|
await t.rejects(
|
2023-06-08 05:24:49 -07:00
|
|
|
openUrl('git+ssh://user@host:repo.git', 'npm home'),
|
2021-11-18 20:58:02 +00:00
|
|
|
/Invalid URL/,
|
|
|
|
'got the correct error'
|
|
|
|
)
|
2023-06-08 05:24:49 -07:00
|
|
|
t.equal(openerUrl(), null, 'did not open')
|
|
|
|
t.same(openerOpts(), null, 'did not open')
|
|
|
|
t.same(joinedOutput(), '', 'printed no output')
|
2021-11-18 20:58:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('encodes non-URL-safe characters in url provided', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t,
|
|
|
|
['https://www.npmjs.com/|cat', 'npm home'])
|
|
|
|
t.equal(openerUrl(), 'https://www.npmjs.com/%7Ccat', 'opened the encoded url')
|
|
|
|
t.same(openerOpts(), { command: null }, 'passed command as null (the default)')
|
|
|
|
t.same(joinedOutput(), '', 'printed no output')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('opens a url with the given browser', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t,
|
|
|
|
['https://www.npmjs.com', 'npm home'], { browser: 'chrome' })
|
|
|
|
t.equal(openerUrl(), 'https://www.npmjs.com', 'opened the given url')
|
|
|
|
// FIXME: browser string is parsed as a boolean in config layer
|
|
|
|
// this is a bug that should be fixed or the config should not allow it
|
|
|
|
t.same(openerOpts(), { command: null }, 'passed the given browser as command')
|
|
|
|
t.same(joinedOutput(), '', 'printed no output')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('prints where to go when browser is disabled', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t,
|
|
|
|
['https://www.npmjs.com', 'npm home'], { browser: false })
|
|
|
|
t.equal(openerUrl(), null, 'did not open')
|
|
|
|
t.same(openerOpts(), null, 'did not open')
|
|
|
|
t.matchSnapshot(joinedOutput(), 'printed expected message')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('prints where to go when browser is disabled and json is enabled', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t,
|
|
|
|
['https://www.npmjs.com', 'npm home'], { browser: false, json: true })
|
|
|
|
t.equal(openerUrl(), null, 'did not open')
|
|
|
|
t.same(openerOpts(), null, 'did not open')
|
|
|
|
t.matchSnapshot(joinedOutput(), 'printed expected message')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('prints where to go when given browser does not exist', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openerUrl, openerOpts, joinedOutput } = await mockOpenUrl(t,
|
|
|
|
['https://www.npmjs.com', 'npm home'],
|
|
|
|
{
|
2023-11-16 09:12:25 -08:00
|
|
|
openerResult: Object.assign(new Error('failed'), { code: 127 }),
|
2023-06-08 05:24:49 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
t.equal(openerUrl(), 'https://www.npmjs.com', 'tried to open the correct url')
|
|
|
|
t.same(openerOpts(), { command: null }, 'tried to use the correct browser')
|
|
|
|
t.matchSnapshot(joinedOutput(), 'printed expected message')
|
2021-03-04 17:40:28 -05:00
|
|
|
})
|
2021-02-08 16:16:45 -05:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('handles unknown opener error', async t => {
|
2023-06-08 05:24:49 -07:00
|
|
|
const { openUrl } = await mockOpenUrl(t, null, {
|
|
|
|
browser: 'firefox',
|
|
|
|
openerResult: Object.assign(new Error('failed'), { code: 'ENOBRIAN' }),
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
2023-06-08 05:24:49 -07:00
|
|
|
|
|
|
|
await t.rejects(openUrl('https://www.npmjs.com', 'npm home'), 'failed', 'got the correct error')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|