1const { resolve } = require('path') 2const t = require('tap') 3const { load: loadMockNpm } = require('../fixtures/mock-npm') 4const tmock = require('../fixtures/tmock') 5 6const mockArboristCmd = async (t, exec, workspace, { mocks = {}, ...opts } = {}) => { 7 const ArboristCmd = tmock(t, '{LIB}/arborist-cmd.js', mocks) 8 9 const config = (typeof workspace === 'function') 10 ? (dirs) => ({ workspace: workspace(dirs) }) 11 : { workspace } 12 13 const mock = await loadMockNpm(t, { 14 config, 15 prefixDir: { 16 'package.json': JSON.stringify({ 17 name: 'simple-workspaces-list', 18 version: '1.1.1', 19 workspaces: [ 20 'a', 21 'b', 22 'group/*', 23 ], 24 }), 25 node_modules: { 26 abbrev: { 27 'package.json': JSON.stringify({ name: 'abbrev', version: '1.1.1' }), 28 }, 29 a: t.fixture('symlink', '../a'), 30 b: t.fixture('symlink', '../b'), 31 }, 32 a: { 33 'package.json': JSON.stringify({ name: 'a', version: '1.0.0' }), 34 }, 35 b: { 36 'package.json': JSON.stringify({ name: 'b', version: '1.0.0' }), 37 }, 38 group: { 39 c: { 40 'package.json': JSON.stringify({ 41 name: 'c', 42 version: '1.0.0', 43 dependencies: { 44 abbrev: '^1.1.1', 45 }, 46 }), 47 }, 48 d: { 49 'package.json': JSON.stringify({ name: 'd', version: '1.0.0' }), 50 }, 51 }, 52 }, 53 ...opts, 54 }) 55 56 let execArg 57 class TestCmd extends ArboristCmd { 58 async exec (arg) { 59 execArg = arg 60 } 61 } 62 63 const cmd = new TestCmd(mock.npm) 64 if (exec) { 65 await cmd.execWorkspaces(exec) 66 } 67 68 return { ...mock, cmd, getArg: () => execArg } 69} 70 71t.test('arborist-cmd', async t => { 72 await t.test('single name', async t => { 73 const { cmd, getArg } = await mockArboristCmd(t, ['foo'], 'a') 74 75 t.same(cmd.workspaceNames, ['a'], 'should set array with single ws name') 76 t.same(getArg(), ['foo'], 'should get received args') 77 }) 78 79 await t.test('single path', async t => { 80 const { cmd } = await mockArboristCmd(t, [], './a') 81 82 t.same(cmd.workspaceNames, ['a'], 'should set array with single ws name') 83 }) 84 85 await t.test('single full path', async t => { 86 const { cmd } = await mockArboristCmd(t, [], ({ prefix }) => resolve(prefix, 'a')) 87 88 t.same(cmd.workspaceNames, ['a'], 'should set array with single ws name') 89 }) 90 91 await t.test('multiple names', async t => { 92 const { cmd } = await mockArboristCmd(t, [], ['a', 'c']) 93 94 t.same(cmd.workspaceNames, ['a', 'c'], 'should set array with single ws name') 95 }) 96 97 await t.test('multiple paths', async t => { 98 const { cmd } = await mockArboristCmd(t, [], ['./a', 'group/c']) 99 100 t.same(cmd.workspaceNames, ['a', 'c'], 'should set array with single ws name') 101 }) 102 103 await t.test('parent path', async t => { 104 const { cmd } = await mockArboristCmd(t, [], './group') 105 106 t.same(cmd.workspaceNames, ['c', 'd'], 'should set array with single ws name') 107 }) 108 109 await t.test('parent path', async t => { 110 const { cmd } = await mockArboristCmd(t, [], './group') 111 112 t.same(cmd.workspaceNames, ['c', 'd'], 'should set array with single ws name') 113 }) 114 115 await t.test('prefix inside cwd', async t => { 116 const { npm, cmd, prefix } = await mockArboristCmd(t, null, ['a', 'c'], { 117 chdir: (dirs) => dirs.testdir, 118 }) 119 120 npm.localPrefix = prefix 121 await cmd.execWorkspaces([]) 122 123 t.same(cmd.workspaceNames, ['a', 'c'], 'should set array with single ws name') 124 }) 125}) 126 127t.test('handle getWorkspaces raising an error', async t => { 128 const { cmd } = await mockArboristCmd(t, null, 'a', { 129 mocks: { 130 '{LIB}/workspaces/get-workspaces.js': async () => { 131 throw new Error('oopsie') 132 }, 133 }, 134 }) 135 136 await t.rejects( 137 cmd.execWorkspaces(['foo']), 138 { message: 'oopsie' } 139 ) 140}) 141