-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy patharborist-cmd.js
218 lines (188 loc) · 6.02 KB
/
arborist-cmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const { resolve } = require('path')
const t = require('tap')
const { load: loadMockNpm } = require('../fixtures/mock-npm')
const tmock = require('../fixtures/tmock')
const mockArboristCmd = async (t, exec, workspace, { mocks = {}, ...opts } = {}) => {
const ArboristCmd = tmock(t, '{LIB}/arborist-cmd.js', mocks)
const config = (typeof workspace === 'function')
? (dirs) => ({ workspace: workspace(dirs) })
: { workspace }
const mock = await loadMockNpm(t, {
config,
prefixDir: {
'package.json': JSON.stringify({
name: 'simple-workspaces-list',
version: '1.1.1',
workspaces: [
'a',
'b',
'group/*',
],
}),
node_modules: {
abbrev: {
'package.json': JSON.stringify({ name: 'abbrev', version: '1.1.1' }),
},
a: t.fixture('symlink', '../a'),
b: t.fixture('symlink', '../b'),
},
a: {
'package.json': JSON.stringify({ name: 'a', version: '1.0.0' }),
},
b: {
'package.json': JSON.stringify({ name: 'b', version: '1.0.0' }),
},
group: {
c: {
'package.json': JSON.stringify({
name: 'c',
version: '1.0.0',
dependencies: {
abbrev: '^1.1.1',
},
}),
},
d: {
'package.json': JSON.stringify({ name: 'd', version: '1.0.0' }),
},
},
},
...opts,
})
let execArg
class TestCmd extends ArboristCmd {
async exec (arg) {
execArg = arg
}
}
const cmd = new TestCmd(mock.npm)
if (exec) {
await cmd.execWorkspaces(exec)
}
return { ...mock, cmd, getArg: () => execArg }
}
t.test('arborist-cmd', async t => {
await t.test('single name', async t => {
const { cmd, getArg } = await mockArboristCmd(t, ['foo'], 'a')
t.same(cmd.workspaceNames, ['a'], 'should set array with single ws name')
t.same(getArg(), ['foo'], 'should get received args')
})
await t.test('single path', async t => {
const { cmd } = await mockArboristCmd(t, [], './a')
t.same(cmd.workspaceNames, ['a'], 'should set array with single ws name')
})
await t.test('single full path', async t => {
const { cmd } = await mockArboristCmd(t, [], ({ prefix }) => resolve(prefix, 'a'))
t.same(cmd.workspaceNames, ['a'], 'should set array with single ws name')
})
await t.test('multiple names', async t => {
const { cmd } = await mockArboristCmd(t, [], ['a', 'c'])
t.same(cmd.workspaceNames, ['a', 'c'], 'should set array with single ws name')
})
await t.test('multiple paths', async t => {
const { cmd } = await mockArboristCmd(t, [], ['./a', 'group/c'])
t.same(cmd.workspaceNames, ['a', 'c'], 'should set array with single ws name')
})
await t.test('parent path', async t => {
const { cmd } = await mockArboristCmd(t, [], './group')
t.same(cmd.workspaceNames, ['c', 'd'], 'should set array with single ws name')
})
await t.test('parent path', async t => {
const { cmd } = await mockArboristCmd(t, [], './group')
t.same(cmd.workspaceNames, ['c', 'd'], 'should set array with single ws name')
})
await t.test('prefix inside cwd', async t => {
const { npm, cmd, prefix } = await mockArboristCmd(t, null, ['a', 'c'], {
chdir: (dirs) => dirs.testdir,
})
npm.localPrefix = prefix
await cmd.execWorkspaces([])
t.same(cmd.workspaceNames, ['a', 'c'], 'should set array with single ws name')
})
})
t.test('handle getWorkspaces raising an error', async t => {
const { cmd } = await mockArboristCmd(t, null, 'a', {
mocks: {
'{LIB}/workspaces/get-workspaces.js': async () => {
throw new Error('oopsie')
},
},
})
await t.rejects(
cmd.execWorkspaces(['foo']),
{ message: 'oopsie' }
)
})
t.test('location detection and audit', async (t) => {
await t.test('audit false without package.json', async t => {
const { npm } = await loadMockNpm(t, {
command: 'install',
prefixDir: {
// no package.json
'readme.txt': 'just a file',
'other-dir': { a: 'a' },
},
})
t.equal(npm.config.get('location'), 'user')
t.equal(npm.config.get('audit'), false)
})
await t.test('audit true with package.json', async t => {
const { npm } = await loadMockNpm(t, {
command: 'install',
prefixDir: {
'package.json': '{ "name": "testpkg", "version": "1.0.0" }',
'readme.txt': 'just a file',
},
})
t.equal(npm.config.get('location'), 'user')
t.equal(npm.config.get('audit'), true)
})
await t.test('audit true without package.json when set', async t => {
const { npm } = await loadMockNpm(t, {
command: 'install',
prefixDir: {
// no package.json
'readme.txt': 'just a file',
'other-dir': { a: 'a' },
},
config: {
audit: true,
},
})
t.equal(npm.config.get('location'), 'user')
t.equal(npm.config.get('audit'), true)
})
await t.test('audit true in root config without package.json', async t => {
const { npm } = await loadMockNpm(t, {
command: 'install',
prefixDir: {
// no package.json
'readme.txt': 'just a file',
'other-dir': { a: 'a' },
},
// change npmRoot to get it to use a builtin rc file
otherDirs: { npmrc: 'audit=true' },
npm: ({ other }) => ({ npmRoot: other }),
})
t.equal(npm.config.get('location'), 'user')
t.equal(npm.config.get('audit'), true)
})
await t.test('test for warning when --global & --audit', async t => {
const { npm, logs } = await loadMockNpm(t, {
command: 'install',
prefixDir: {
// no package.json
'readme.txt': 'just a file',
'other-dir': { a: 'a' },
},
config: {
audit: true,
global: true,
},
})
t.equal(npm.config.get('location'), 'user')
t.equal(npm.config.get('audit'), true)
t.equal(logs.warn[0][0], 'config')
t.equal(logs.warn[0][1], 'includes both --global and --audit, which is currently unsupported.')
})
})