|
| 1 | +const t = require('tap') |
| 2 | +const mockGlobals = require('../../fixtures/mock-globals.js') |
| 3 | +const EventEmitter = require('events') |
| 4 | + |
| 5 | +const OUTPUT = [] |
| 6 | +const output = (...args) => OUTPUT.push(args) |
| 7 | +const npm = { |
| 8 | + _config: { |
| 9 | + json: false, |
| 10 | + browser: true, |
| 11 | + }, |
| 12 | + config: { |
| 13 | + get: k => npm._config[k], |
| 14 | + set: (k, v) => { |
| 15 | + npm._config[k] = v |
| 16 | + }, |
| 17 | + }, |
| 18 | + output, |
| 19 | +} |
| 20 | + |
| 21 | +let openerUrl = null |
| 22 | +let openerOpts = null |
| 23 | +let openerResult = null |
| 24 | +const opener = (url, opts, cb) => { |
| 25 | + openerUrl = url |
| 26 | + openerOpts = opts |
| 27 | + return cb(openerResult) |
| 28 | +} |
| 29 | + |
| 30 | +let questionShouldResolve = true |
| 31 | +const readline = { |
| 32 | + createInterface: () => ({ |
| 33 | + question: (_q, cb) => { |
| 34 | + if (questionShouldResolve === true) { |
| 35 | + cb() |
| 36 | + } |
| 37 | + }, |
| 38 | + close: () => {}, |
| 39 | + }), |
| 40 | +} |
| 41 | + |
| 42 | +const openUrlPrompt = t.mock('../../../lib/utils/open-url-prompt.js', { |
| 43 | + opener, |
| 44 | + readline, |
| 45 | +}) |
| 46 | + |
| 47 | +mockGlobals(t, { |
| 48 | + 'process.stdin.isTTY': true, |
| 49 | + 'process.stdout.isTTY': true, |
| 50 | +}) |
| 51 | + |
| 52 | +t.test('does not open a url in non-interactive environments', async t => { |
| 53 | + t.teardown(() => { |
| 54 | + openerUrl = null |
| 55 | + openerOpts = null |
| 56 | + OUTPUT.length = 0 |
| 57 | + }) |
| 58 | + |
| 59 | + mockGlobals(t, { |
| 60 | + 'process.stdin.isTTY': false, |
| 61 | + 'process.stdout.isTTY': false, |
| 62 | + }) |
| 63 | + |
| 64 | + await openUrlPrompt(npm, 'https://www.npmjs.com', 'npm home', 'prompt') |
| 65 | + t.equal(openerUrl, null, 'did not open') |
| 66 | + t.same(openerOpts, null, 'did not open') |
| 67 | +}) |
| 68 | + |
| 69 | +t.test('opens a url', async t => { |
| 70 | + t.teardown(() => { |
| 71 | + openerUrl = null |
| 72 | + openerOpts = null |
| 73 | + OUTPUT.length = 0 |
| 74 | + npm._config.browser = true |
| 75 | + }) |
| 76 | + |
| 77 | + npm._config.browser = 'browser' |
| 78 | + await openUrlPrompt(npm, 'https://www.npmjs.com', 'npm home', 'prompt') |
| 79 | + t.equal(openerUrl, 'https://www.npmjs.com', 'opened the given url') |
| 80 | + t.same(openerOpts, { command: 'browser' }, 'passed command as null (the default)') |
| 81 | + t.matchSnapshot(OUTPUT) |
| 82 | +}) |
| 83 | + |
| 84 | +t.test('prints json output', async t => { |
| 85 | + t.teardown(() => { |
| 86 | + openerUrl = null |
| 87 | + openerOpts = null |
| 88 | + OUTPUT.length = 0 |
| 89 | + npm._config.json = false |
| 90 | + }) |
| 91 | + |
| 92 | + npm._config.json = true |
| 93 | + await openUrlPrompt(npm, 'https://www.npmjs.com', 'npm home', 'prompt') |
| 94 | + t.matchSnapshot(OUTPUT) |
| 95 | +}) |
| 96 | + |
| 97 | +t.test('returns error for non-https url', async t => { |
| 98 | + t.teardown(() => { |
| 99 | + openerUrl = null |
| 100 | + openerOpts = null |
| 101 | + OUTPUT.length = 0 |
| 102 | + }) |
| 103 | + await t.rejects( |
| 104 | + openUrlPrompt(npm, 'ftp://www.npmjs.com', 'npm home', 'prompt'), |
| 105 | + /Invalid URL/, |
| 106 | + 'got the correct error' |
| 107 | + ) |
| 108 | + t.equal(openerUrl, null, 'did not open') |
| 109 | + t.same(openerOpts, null, 'did not open') |
| 110 | + t.same(OUTPUT, [], 'printed no output') |
| 111 | +}) |
| 112 | + |
| 113 | +t.test('does not open url if canceled', async t => { |
| 114 | + t.teardown(() => { |
| 115 | + openerUrl = null |
| 116 | + openerOpts = null |
| 117 | + OUTPUT.length = 0 |
| 118 | + questionShouldResolve = true |
| 119 | + }) |
| 120 | + |
| 121 | + questionShouldResolve = false |
| 122 | + const emitter = new EventEmitter() |
| 123 | + |
| 124 | + const open = openUrlPrompt(npm, 'https://www.npmjs.com', 'npm home', 'prompt', emitter) |
| 125 | + |
| 126 | + emitter.emit('abort') |
| 127 | + |
| 128 | + await open |
| 129 | + |
| 130 | + t.equal(openerUrl, null, 'did not open') |
| 131 | + t.same(openerOpts, null, 'did not open') |
| 132 | +}) |
| 133 | + |
| 134 | +t.test('returns error when opener errors', async t => { |
| 135 | + t.teardown(() => { |
| 136 | + openerUrl = null |
| 137 | + openerOpts = null |
| 138 | + openerResult = null |
| 139 | + OUTPUT.length = 0 |
| 140 | + }) |
| 141 | + |
| 142 | + openerResult = new Error('Opener failed') |
| 143 | + |
| 144 | + await t.rejects( |
| 145 | + openUrlPrompt(npm, 'https://www.npmjs.com', 'npm home', 'prompt'), |
| 146 | + /Opener failed/, |
| 147 | + 'got the correct error' |
| 148 | + ) |
| 149 | + t.equal(openerUrl, 'https://www.npmjs.com', 'did not open') |
| 150 | +}) |
0 commit comments