1const t = require('tap') 2const { load: loadMockNpm } = require('../../fixtures/mock-npm') 3 4const pacote = { 5 manifest: async (spec) => { 6 return spec === 'nobugs' ? { 7 name: 'nobugs', 8 version: '1.2.3', 9 } : spec === 'nullbugs' ? { 10 name: 'nullbugs', 11 version: '1.2.3', 12 bugs: null, 13 } : spec === 'bugsurl' ? { 14 name: 'bugsurl', 15 version: '1.2.3', 16 bugs: 'https://bugzilla.localhost/bugsurl', 17 } : spec === 'bugsobj' ? { 18 name: 'bugsobj', 19 version: '1.2.3', 20 bugs: { url: 'https://bugzilla.localhost/bugsobj' }, 21 } : spec === 'bugsobj-nourl' ? { 22 name: 'bugsobj-nourl', 23 version: '1.2.3', 24 bugs: { no: 'url here' }, 25 } : spec === 'repourl' ? { 26 name: 'repourl', 27 version: '1.2.3', 28 repository: 'https://github.com/foo/repourl', 29 } : spec === 'repoobj' ? { 30 name: 'repoobj', 31 version: '1.2.3', 32 repository: { url: 'https://github.com/foo/repoobj' }, 33 } : spec === 'mailtest' ? { 34 name: 'mailtest', 35 version: '3.7.4', 36 bugs: { email: 'hello@example.com' }, 37 } : spec === 'secondmailtest' ? { 38 name: 'secondmailtest', 39 version: '0.1.1', 40 bugs: { email: 'ABC432abc@a.b.example.net' }, 41 } : spec === '.' ? { 42 name: 'thispkg', 43 version: '1.2.3', 44 bugs: 'https://example.com', 45 } : null 46 }, 47} 48 49t.test('usage', async (t) => { 50 const { bugs } = await loadMockNpm(t, { command: 'bugs' }) 51 t.match(bugs.usage, 'bugs', 'usage has command name in it') 52}) 53 54t.test('open bugs urls & emails', async t => { 55 // keep a tally of which urls got opened 56 let opened = {} 57 58 const openUrl = async (_, url) => { 59 opened[url] = opened[url] || 0 60 opened[url]++ 61 } 62 63 const { npm } = await loadMockNpm(t, { 64 mocks: { 65 pacote, 66 '{LIB}/utils/open-url.js': openUrl, 67 }, 68 }) 69 70 const expected = { 71 '.': 'https://example.com', 72 nobugs: 'https://www.npmjs.com/package/nobugs', 73 nullbugs: 'https://www.npmjs.com/package/nullbugs', 74 'bugsobj-nourl': 'https://www.npmjs.com/package/bugsobj-nourl', 75 bugsurl: 'https://bugzilla.localhost/bugsurl', 76 bugsobj: 'https://bugzilla.localhost/bugsobj', 77 repourl: 'https://github.com/foo/repourl/issues', 78 repoobj: 'https://github.com/foo/repoobj/issues', 79 mailtest: 'mailto:hello@example.com', 80 secondmailtest: 'mailto:ABC432abc@a.b.example.net', 81 } 82 83 for (const [pkg, expect] of Object.entries(expected)) { 84 await t.test(pkg, async t => { 85 await npm.exec('bugs', [pkg]) 86 t.equal(opened[expect], 1, 'opened expected url', { opened }) 87 }) 88 } 89 90 opened = {} 91 92 await t.test('open default package if none specified', async t => { 93 await npm.exec('bugs', []) 94 t.equal(opened['https://example.com'], 1, 'opened expected url', { opened }) 95 }) 96}) 97