This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
load-actual.js
249 lines (226 loc) · 8.5 KB
/
load-actual.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const t = require('tap')
const { format } = require('tcompare')
const Arborist = require('../../lib/arborist')
const { resolve, dirname, relative } = require('path')
const { realpathSync } = require('fs')
const Node = require('../../lib/node.js')
const Shrinkwrap = require('../../lib/shrinkwrap.js')
const {
fixtures,
roots,
symlinks,
} = require('../fixtures/index.js')
// little helper functions to make the loaded trees
// easier to look at in the snapshot results.
const pp = path => path && path.substr(fixtures.length + 1).replace(/\\/g, '/')
const printEdge = (edge, inout) => ({
name: edge.name,
type: edge.type,
spec: edge.spec,
...(inout === 'in' ? {
from: edge.from && pp(edge.from.realpath),
} : {
to: edge.to && pp(edge.to.realpath),
}),
...(edge.error ? { error: edge.error } : {}),
__proto__: { constructor: edge.constructor },
})
const stringify = require('json-stringify-nice')
const printTree = tree => ({
name: tree.name,
location: tree.location,
realpath: pp(tree.realpath),
top: pp(tree.top.realpath),
...(tree.extraneous ? { extraneous: true } : {
...(tree.dev ? { dev: true } : {}),
...(tree.optional ? { optional: true } : {}),
...(tree.devOptional && !tree.dev && !tree.optional
? { devOptional: true } : {}),
...(tree.peer ? { peer: true } : {}),
}),
...(tree.fsParent ? { fsParent: pp(tree.fsParent) } : {}),
...(tree.errors.length
? {
errors: tree.errors.map(error => ({
code: error.code,
...(error.path ? { path: relative(__dirname, error.path) }
: {}),
})),
} : {}),
...(tree.isLink ? {
target: {
name: tree.target.name,
...(tree.target.parent ? { parent: pp(tree.target.parent.realpath) } : {}),
...(tree.target.fsParent ? { fsParent: pp(tree.target.fsParent.realpath) } : {}),
}
} : {}),
...(tree.inBundle ? { bundled: true } : {}),
...(tree.edgesIn.size ? {
edgesIn: new Set([...tree.edgesIn]
.sort((a, b) => pp(a.from.realpath).localeCompare(pp(b.from.realpath)))
.map(edge => printEdge(edge, 'in'))),
} : {}),
...(tree.edgesOut.size ? {
edgesOut: new Map([...tree.edgesOut.entries()]
.sort((a, b) => a[0].localeCompare(b[0]))
.map(([name, edge]) => [name, printEdge(edge, 'out')]))
} : {}),
...( tree.target || !tree.children.size ? {}
: {
children: new Map([...tree.children.entries()]
.sort((a, b) => a[0].localeCompare(b[0]))
.map(([name, tree]) => [name, printTree(tree)]))
}),
...(tree.target || !tree.fsChildren.size ? {}
: {
fsChildren: [...tree.fsChildren]
.sort((a, b) => a.path.localeCompare(b.path))
.map(tree => tree.location)
}),
__proto__: { constructor: tree.constructor },
...( !tree.meta ? {} : {
// stringify and re-parse to sort consistently
meta: JSON.parse(stringify(tree.meta.commit())),
})
})
t.formatSnapshot = tree => format(printTree(tree), { sort: true })
const loadActual = (path, opts) => new Arborist({path}).loadActual(opts)
roots.forEach(path => {
const dir = resolve(fixtures, path)
t.test(path, t => loadActual(dir).then(tree =>
t.matchSnapshot(tree, 'loaded tree')))
})
t.test('already loaded', t => new Arborist({
path: resolve(__dirname, '../fixtures/selflink'),
}).loadActual().then(actualTree => new Arborist({
path: resolve(__dirname, '../fixtures/selflink'),
actualTree,
}).loadActual().then(tree2 => t.equal(tree2, actualTree))))
t.test('load a tree rooted on a different node', async t => {
const path = resolve(fixtures, 'workspace')
const other = resolve(fixtures.replace(/[a-z]/gi, 'X'), 'workspace')
const root = new Node({
meta: await Shrinkwrap.reset({path: other}),
path: other,
realpath: other,
pkg: require(path + '/package.json'),
})
root.extraneous = false
root.dev = false
root.devOptional = false
root.optional = false
root.peer = false
const actual = await new Arborist({path}).loadActual()
const transp = await new Arborist({path}).loadActual({root})
// verify that the transp nodes have the right paths
t.equal(transp.children.get('a').path, resolve(other, 'node_modules/a'))
t.equal(transp.children.get('b').path, resolve(other, 'node_modules/b'))
t.equal(transp.children.get('c').path, resolve(other, 'node_modules/c'))
t.equal(transp.children.get('a').realpath, resolve(other, 'packages/a'))
t.equal(transp.children.get('b').realpath, resolve(other, 'packages/b'))
t.equal(transp.children.get('c').realpath, resolve(other, 'packages/c'))
// should look the same, once we strip off the other/fixture paths
t.equal(format(printTree(actual)), format(printTree(transp)), 'similar trees')
// now try with a transplant filter that keeps out the 'a' module
const rootFiltered = new Node({
meta: await Shrinkwrap.reset({path: other}),
path: other,
realpath: other,
pkg: require(path + '/package.json'),
})
rootFiltered.extraneous = false
rootFiltered.dev = false
rootFiltered.devOptional = false
rootFiltered.optional = false
rootFiltered.peer = false
const transpFilter = await new Arborist({path}).loadActual({
root: rootFiltered,
transplantFilter: n => n.name !== 'a'
})
t.equal(transpFilter.children.get('a'), undefined)
t.equal(transpFilter.children.get('b').path, resolve(other, 'node_modules/b'))
t.equal(transpFilter.children.get('c').path, resolve(other, 'node_modules/c'))
t.equal(transpFilter.children.get('a'), undefined)
t.equal(transpFilter.children.get('b').realpath, resolve(other, 'packages/b'))
t.equal(transpFilter.children.get('c').realpath, resolve(other, 'packages/c'))
})
t.test('looking outside of cwd', t => {
const cwd = process.cwd()
t.teardown(() => process.chdir(cwd))
process.chdir(resolve(__dirname, '../fixtures/selflink'))
const dir = '../root'
return loadActual(dir).then(tree =>
t.matchSnapshot(tree, 'loaded tree'))
})
t.test('cwd is default root', t => {
const cwd = process.cwd()
t.teardown(() => process.chdir(cwd))
process.chdir('test/fixtures/root')
return loadActual().then(tree =>
t.matchSnapshot(tree, 'loaded tree'))
})
t.test('shake out Link target timing issue', t => {
process.env._TEST_ARBORIST_SLOW_LINK_TARGET_ = '1'
t.teardown(() => process.env._TEST_ARBORIST_SLOW_LINK_TARGET_ = '')
const dir = resolve(fixtures, 'selflink')
return loadActual(dir).then(tree =>
t.matchSnapshot(tree, 'loaded tree'))
})
t.test('broken json', t =>
loadActual(resolve(fixtures, 'bad')).then(d => {
t.ok(d.errors.length, 'Got an error object')
t.equal(d.errors[0] && d.errors[0].code, 'EJSONPARSE')
t.ok(d, 'Got a tree')
}))
t.test('missing json does not obscure deeper errors', t =>
loadActual(resolve(fixtures, 'empty')).then(d => {
t.match(d, { errors: [{ code: 'ENOENT' }] },
'Error reading json of top level')
t.match(d.children.get('foo'), { errors: [{ code: 'EJSONPARSE' }] },
'Error parsing JSON of child node')
}))
t.test('missing folder', t =>
t.rejects(loadActual(resolve(fixtures, 'does-not-exist')), {
code: 'ENOENT'
}))
t.test('missing symlinks', t =>
loadActual(resolve(fixtures, 'badlink')).then(d => {
t.is(d.children.size, 2, 'both broken children are included')
t.match(d.children.get('foo'), { errors: [{ code: 'ELOOP' }] },
'foo has error')
t.match(d.children.get('bar'), { errors: [{ code: 'ENOENT' }] },
'bar has error')
}))
t.test('load from a hidden lockfile', t =>
t.resolveMatchSnapshot(loadActual(resolve(fixtures, 'hidden-lockfile'))))
t.test('load a global space', t =>
t.resolveMatchSnapshot(loadActual(resolve(fixtures, 'global-style/lib'), {
global: true,
})))
t.test('load a global space symlink', t =>
t.resolveMatchSnapshot(loadActual(resolve(fixtures, 'global-style/lib-link'), {
global: true,
})))
t.test('load a global space with a filter', t =>
t.resolveMatchSnapshot(loadActual(resolve(fixtures, 'global-style/lib'), {
global: true,
filter: (parent, kid) => parent.parent || kid === 'semver'
})))
t.test('realpath gutchecks', t => {
// the realpath module is tested pretty thoroughly, but
// while we've got a bunch of symlinks being created, may as well
// give it a quick integration pass.
const d = resolve(__dirname, '../fixtures')
const realpath = require('../../lib/realpath.js')
Object.keys(symlinks).map(link => t.test(link, t =>
realpath(
resolve(d, link),
new Map(),
new Map(),
0
).then(
real => t.equal(real, realpathSync(resolve(d, link))),
er => t.throws(()=> realpathSync(resolve(d, link)))
)))
t.end()
})