This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
/
basic.js
191 lines (167 loc) · 4.92 KB
/
basic.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
var test = require('tap').test
var rpt = require('../rpt.js')
var path = require('path')
var fs = require('fs')
var archy = require('archy')
var mkdirp = require('mkdirp')
var fixtures = path.resolve(__dirname, 'fixtures')
var roots = [ 'root', 'other', 'selflink', 'noname' ]
var cwd = path.resolve(__dirname, '..')
var symlinks = {
'selflink/node_modules/@scope/z/node_modules/glob':
'../../../foo/node_modules/glob',
'other/node_modules/glob':
'../../root/node_modules/@scope/x/node_modules/glob',
'linkedroot':
'root',
'deep/root':
'../root',
'deeproot':
'deep',
'badlink/node_modules/foo':
'foo',
'badlink/node_modules/bar':
'baz'
}
function cleanup () {
Object.keys(symlinks).forEach(function (s) {
var p = path.resolve(cwd, 'test/fixtures', s)
try {
fs.unlinkSync(p)
} catch (er) {}
})
}
test('setup symlinks', function (t) {
cleanup()
Object.keys(symlinks).forEach(function (s) {
var p = path.resolve(cwd, 'test/fixtures', s)
mkdirp.sync(path.dirname(p))
fs.symlinkSync(symlinks [ s ], p, 'dir')
})
t.end()
})
roots.forEach(function (root) {
var dir = path.resolve(fixtures, root)
// test promisey nature
test(root, t => rpt(dir).then(d =>
t.matchSnapshot(archy(archyize(d)).trim(), root + ' tree')))
})
test('linkedroot', function (t) {
var dir = path.resolve(fixtures, 'linkedroot')
// test legacy cb nature
rpt(dir, function (er, d) {
if (er && er.code !== 'ENOENT') throw er
var actual = archy(archyize(d)).trim()
t.matchSnapshot(actual, 'linkedroot tree')
t.end()
})
})
test('deeproot', function (t) {
var dir = path.resolve(fixtures, 'deeproot/root')
rpt(dir, function (er, d) {
if (er && er.code !== 'ENOENT') throw er
var actual = archy(archyize(d)).trim()
t.matchSnapshot(actual, 'deeproot tree')
t.end()
})
})
test('filterWith', t =>
rpt(
path.join(fixtures, 'root'),
(node, kid) => !node.parent,
true
).then(d => t.matchSnapshot(archy(archyize(d)).trim()), 'only 1 level deep')
)
test('looking outside of cwd', t => {
const cwd = process.cwd()
t.teardown(() => process.chdir(cwd))
process.chdir('test/fixtures/selflink')
return rpt('../root').then(d =>
t.matchSnapshot(archy(archyize(d)).trim()))
})
test('shake out Link target timing issue', t => {
process.env._TEST_RPT_SLOW_LINK_TARGET_ = '1'
const cwd = process.cwd()
t.teardown(() => process.env._TEST_RPT_SLOW_LINK_TARGET_ = '')
return rpt(path.resolve(fixtures, 'selflink')).then(d =>
t.matchSnapshot(archy(archyize(d)).trim()))
})
test('broken json', function (t) {
rpt(path.resolve(fixtures, 'bad'), function (er, d) {
t.ok(d.error, 'Got an error object')
t.equal(d.error && d.error.code, 'EJSONPARSE')
t.ok(d, 'Got a tree')
t.end()
})
})
test('missing json does not obscure deeper errors', function (t) {
rpt(path.resolve(fixtures, 'empty'), function (er, d) {
var error = d.error
t.ok(error, 'Error reading json of top level')
t.equal(error && error.code, 'ENOENT')
var childError = d.children.length===1 && d.children[0].error
t.ok(childError, 'Error parsing JSON of child node')
t.equal(childError && childError.code, 'EJSONPARSE')
t.end()
})
})
test('missing folder', function (t) {
rpt(path.resolve(fixtures, 'does-not-exist'), function (er, d) {
t.ok(er, 'Got an error object')
t.equal(er && er.code, 'ENOENT')
t.ok(!d, 'No tree on top level error')
t.end()
})
})
test('missing symlinks', function (t) {
rpt(path.resolve(fixtures, 'badlink'), function (er, d) {
if (er && er.code !== 'ENOENT') throw er
t.is(d.children.length, 2, 'both broken children are included')
d.children.forEach(function (child) {
t.ok(child.error, 'Child node has an error')
})
t.end()
})
})
function archyize (d, seen) {
seen = seen || {}
var path = d.path
if (d.target) {
path = d.target.path
}
var label = !Object.keys(d.package).length ? '' :
d.package._id ? d.package._id + ' ' :
d.name ? d.name + (d.package.version ? '@' + d.package.version : '') + ' ' :
d.package.name ? d.package.name + (d.package.version ? '@' + d.package.version : '') + ' ' :
''
label += path.substr(cwd.length + 1)
if (d . target) {
return { label: label + ' (symlink)', nodes: [] }
}
return {
label: label,
nodes: d.children.map(function (kid) {
return archyize(kid, seen)
})
}
}
test('realpath gutchecks', t => {
const d = path.resolve(cwd, 'test/fixtures')
const realpath = require('../realpath.js')
const {realpathSync} = fs
Object.keys(symlinks).map(link => t.test(link, t =>
realpath(
path.resolve(d, link),
new Map(),
new Map(),
0
).then(
real => t.equal(real, realpathSync(path.resolve(d, link))),
er => t.throws(()=> realpathSync(path.resolve(d, link)))
)))
t.end()
})
test('cleanup', function (t) {
cleanup()
t.end()
})