2021-04-15 21:53:45 -04:00
|
|
|
const t = require('tap')
|
2021-02-08 16:16:45 -05:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
const OUTPUT = []
|
|
|
|
const output = (...args) => OUTPUT.push(args)
|
2021-02-08 16:16:45 -05:00
|
|
|
const npm = {
|
|
|
|
_config: {
|
|
|
|
json: false,
|
|
|
|
browser: true,
|
|
|
|
},
|
|
|
|
config: {
|
2021-11-18 20:58:02 +00:00
|
|
|
get: k => npm._config[k],
|
2021-02-08 16:16:45 -05:00
|
|
|
set: (k, v) => {
|
|
|
|
npm._config[k] = v
|
|
|
|
},
|
|
|
|
},
|
2021-03-11 17:54:23 -05:00
|
|
|
output,
|
2021-02-08 16:16:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let openerUrl = null
|
|
|
|
let openerOpts = null
|
|
|
|
let openerResult = null
|
2022-12-06 22:18:33 -05:00
|
|
|
|
|
|
|
const open = async (url, options) => {
|
2021-02-08 16:16:45 -05:00
|
|
|
openerUrl = url
|
2022-12-06 22:18:33 -05:00
|
|
|
openerOpts = options
|
|
|
|
if (openerResult) {
|
|
|
|
throw openerResult
|
|
|
|
}
|
2021-02-08 16:16:45 -05:00
|
|
|
}
|
|
|
|
|
2021-04-15 21:53:45 -04:00
|
|
|
const openUrl = t.mock('../../../lib/utils/open-url.js', {
|
2022-12-06 22:18:33 -05:00
|
|
|
'@npmcli/promise-spawn': {
|
|
|
|
open,
|
|
|
|
},
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('opens a url', async t => {
|
2021-02-08 16:16:45 -05:00
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
await openUrl(npm, '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(OUTPUT, [], '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 => {
|
2021-02-08 16:16:45 -05:00
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
})
|
2021-11-18 20:58:02 +00:00
|
|
|
await t.rejects(
|
|
|
|
openUrl(npm, 'ftp://www.npmjs.com', 'npm home'),
|
|
|
|
/Invalid URL/,
|
|
|
|
'got the correct error'
|
|
|
|
)
|
2021-03-04 17:40:28 -05:00
|
|
|
t.equal(openerUrl, null, 'did not open')
|
|
|
|
t.same(openerOpts, null, 'did not open')
|
|
|
|
t.same(OUTPUT, [], '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 => {
|
2021-02-08 16:16:45 -05:00
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
})
|
2021-11-18 20:58:02 +00:00
|
|
|
await t.rejects(
|
|
|
|
openUrl(npm, 'file:///usr/local/bin/ls', 'npm home'),
|
|
|
|
/Invalid URL/,
|
|
|
|
'got the correct error'
|
|
|
|
)
|
2021-03-04 17:40:28 -05:00
|
|
|
t.equal(openerUrl, null, 'did not open')
|
|
|
|
t.same(openerOpts, null, 'did not open')
|
|
|
|
t.same(OUTPUT, [], '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 => {
|
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
})
|
|
|
|
await openUrl(npm, '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(OUTPUT, [], 'printed no output')
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('returns error for non-parseable url', async t => {
|
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
})
|
|
|
|
await t.rejects(
|
|
|
|
openUrl(npm, 'git+ssh://user@host:repo.git', 'npm home'),
|
|
|
|
/Invalid URL/,
|
|
|
|
'got the correct error'
|
|
|
|
)
|
|
|
|
t.equal(openerUrl, null, 'did not open')
|
|
|
|
t.same(openerOpts, null, 'did not open')
|
|
|
|
t.same(OUTPUT, [], 'printed no output')
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('encodes non-URL-safe characters in url provided', async t => {
|
2021-10-07 20:21:11 +00:00
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
})
|
|
|
|
await openUrl(npm, '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(OUTPUT, [], '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 => {
|
2021-02-08 16:16:45 -05:00
|
|
|
npm.config.set('browser', 'chrome')
|
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
npm.config.set('browser', true)
|
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
await openUrl(npm, 'https://www.npmjs.com', 'npm home')
|
|
|
|
t.equal(openerUrl, 'https://www.npmjs.com', 'opened the given url')
|
|
|
|
t.same(openerOpts, { command: 'chrome' }, 'passed the given browser as command')
|
|
|
|
t.same(OUTPUT, [], '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 => {
|
2021-02-08 16:16:45 -05:00
|
|
|
npm.config.set('browser', false)
|
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
npm.config.set('browser', true)
|
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
await openUrl(npm, 'https://www.npmjs.com', 'npm home')
|
|
|
|
t.equal(openerUrl, null, 'did not open')
|
|
|
|
t.same(openerOpts, null, 'did not open')
|
|
|
|
t.equal(OUTPUT.length, 1, 'got one logged message')
|
|
|
|
t.equal(OUTPUT[0].length, 1, 'logged message had one value')
|
|
|
|
t.matchSnapshot(OUTPUT[0][0], '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 => {
|
2021-02-08 16:16:45 -05:00
|
|
|
npm.config.set('browser', false)
|
|
|
|
npm.config.set('json', true)
|
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
npm.config.set('browser', true)
|
|
|
|
npm.config.set('json', false)
|
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
await openUrl(npm, 'https://www.npmjs.com', 'npm home')
|
|
|
|
t.equal(openerUrl, null, 'did not open')
|
|
|
|
t.same(openerOpts, null, 'did not open')
|
|
|
|
t.equal(OUTPUT.length, 1, 'got one logged message')
|
|
|
|
t.equal(OUTPUT[0].length, 1, 'logged message had one value')
|
|
|
|
t.matchSnapshot(OUTPUT[0][0], '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 => {
|
2021-02-08 16:16:45 -05:00
|
|
|
npm.config.set('browser', 'firefox')
|
|
|
|
openerResult = Object.assign(new Error('failed'), { code: 'ENOENT' })
|
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
npm.config.set('browser', true)
|
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
await openUrl(npm, 'https://www.npmjs.com', 'npm home')
|
|
|
|
t.equal(openerUrl, 'https://www.npmjs.com', 'tried to open the correct url')
|
|
|
|
t.same(openerOpts, { command: 'firefox' }, 'tried to use the correct browser')
|
|
|
|
t.equal(OUTPUT.length, 1, 'got one logged message')
|
|
|
|
t.equal(OUTPUT[0].length, 1, 'logged message had one value')
|
|
|
|
t.matchSnapshot(OUTPUT[0][0], 'printed expected message')
|
|
|
|
})
|
2021-02-08 16:16:45 -05:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
t.test('handles unknown opener error', async t => {
|
2021-03-04 17:40:28 -05:00
|
|
|
npm.config.set('browser', 'firefox')
|
|
|
|
openerResult = Object.assign(new Error('failed'), { code: 'ENOBRIAN' })
|
|
|
|
t.teardown(() => {
|
|
|
|
openerUrl = null
|
|
|
|
openerOpts = null
|
|
|
|
OUTPUT.length = 0
|
|
|
|
npm.config.set('browser', true)
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
t.rejects(openUrl(npm, 'https://www.npmjs.com', 'npm home'), 'failed', 'got the correct error')
|
2021-02-08 16:16:45 -05:00
|
|
|
})
|