Skip to content

Commit 523cf69

Browse files
committed
test: add lib/rebuild.js tests
Fixes: npm/statusboard#166 PR-URL: #2244 Credit: @ruyadorno Close: #2244 Reviewed-by: @darcyclarke
1 parent 9da972d commit 523cf69

File tree

2 files changed

+228
-3
lines changed

2 files changed

+228
-3
lines changed

lib/rebuild.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
'use strict'
2+
3+
const { resolve } = require('path')
14
const Arborist = require('@npmcli/arborist')
5+
const npa = require('npm-package-arg')
6+
const semver = require('semver')
7+
28
const npm = require('./npm.js')
39
const usageUtil = require('./utils/usage.js')
4-
const { resolve } = require('path')
510
const output = require('./utils/output.js')
6-
const npa = require('npm-package-arg')
7-
const semver = require('semver')
811

912
const cmd = (args, cb) => rebuild(args).then(() => cb()).catch(cb)
1013

test/lib/rebuild.js

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
const fs = require('fs')
2+
const { resolve } = require('path')
3+
const t = require('tap')
4+
const requireInject = require('require-inject')
5+
6+
let result = ''
7+
8+
const npm = {
9+
globalDir: '',
10+
flatOptions: {
11+
global: false,
12+
},
13+
prefix: '',
14+
}
15+
const mocks = {
16+
'../../lib/npm.js': npm,
17+
'../../lib/utils/output.js': (...msg) => {
18+
result += msg.join('\n')
19+
},
20+
'../../lib/utils/usage.js': () => 'usage instructions',
21+
}
22+
23+
const rebuild = requireInject('../../lib/rebuild.js', mocks)
24+
25+
t.afterEach(cb => {
26+
npm.prefix = ''
27+
npm.flatOptions.global = false
28+
npm.globalDir = ''
29+
result = ''
30+
cb()
31+
})
32+
33+
t.test('no args', t => {
34+
const path = t.testdir({
35+
node_modules: {
36+
a: {
37+
'package.json': JSON.stringify({
38+
name: 'a',
39+
version: '1.0.0',
40+
bin: 'cwd',
41+
scripts: {
42+
preinstall: `node -e 'require("fs").writeFileSync("cwd", "")'`,
43+
},
44+
}),
45+
},
46+
b: {
47+
'package.json': JSON.stringify({
48+
name: 'b',
49+
version: '1.0.0',
50+
bin: 'cwd',
51+
scripts: {
52+
preinstall: `node -e 'require("fs").writeFileSync("cwd", "")'`,
53+
},
54+
}),
55+
},
56+
},
57+
})
58+
59+
const aBuildFile = resolve(path, 'node_modules/a/cwd')
60+
const bBuildFile = resolve(path, 'node_modules/b/cwd')
61+
const aBinFile = resolve(path, 'node_modules/.bin/a')
62+
const bBinFile = resolve(path, 'node_modules/.bin/b')
63+
t.throws(() => fs.statSync(aBuildFile))
64+
t.throws(() => fs.statSync(bBuildFile))
65+
t.throws(() => fs.statSync(aBinFile))
66+
t.throws(() => fs.statSync(bBinFile))
67+
68+
npm.prefix = path
69+
70+
rebuild([], err => {
71+
if (err)
72+
throw err
73+
74+
t.ok(() => fs.statSync(aBuildFile))
75+
t.ok(() => fs.statSync(bBuildFile))
76+
t.ok(() => fs.statSync(aBinFile))
77+
t.ok(() => fs.statSync(bBinFile))
78+
79+
t.equal(
80+
result,
81+
'rebuilt dependencies successfully',
82+
'should output success msg'
83+
)
84+
85+
t.end()
86+
})
87+
})
88+
89+
t.test('filter by pkg name', t => {
90+
const path = t.testdir({
91+
node_modules: {
92+
a: {
93+
'index.js': '',
94+
'package.json': JSON.stringify({
95+
name: 'a',
96+
version: '1.0.0',
97+
bin: 'index.js',
98+
}),
99+
},
100+
b: {
101+
'index.js': '',
102+
'package.json': JSON.stringify({
103+
name: 'b',
104+
version: '1.0.0',
105+
bin: 'index.js',
106+
}),
107+
},
108+
},
109+
})
110+
111+
npm.prefix = path
112+
113+
const aBinFile = resolve(path, 'node_modules/.bin/a')
114+
const bBinFile = resolve(path, 'node_modules/.bin/b')
115+
t.throws(() => fs.statSync(aBinFile))
116+
t.throws(() => fs.statSync(bBinFile))
117+
118+
rebuild(['b'], err => {
119+
if (err)
120+
throw err
121+
122+
t.throws(() => fs.statSync(aBinFile), 'should not link a bin')
123+
t.ok(() => fs.statSync(bBinFile), 'should link filtered pkg bin')
124+
125+
t.end()
126+
})
127+
})
128+
129+
t.test('filter by pkg@<range>', t => {
130+
const path = t.testdir({
131+
node_modules: {
132+
a: {
133+
'index.js': '',
134+
'package.json': JSON.stringify({
135+
name: 'a',
136+
version: '1.0.0',
137+
bin: 'index.js',
138+
}),
139+
node_modules: {
140+
b: {
141+
'index.js': '',
142+
'package.json': JSON.stringify({
143+
name: 'b',
144+
version: '2.0.0',
145+
bin: 'index.js',
146+
}),
147+
},
148+
},
149+
},
150+
b: {
151+
'index.js': '',
152+
'package.json': JSON.stringify({
153+
name: 'b',
154+
version: '1.0.0',
155+
bin: 'index.js',
156+
}),
157+
},
158+
},
159+
})
160+
161+
npm.prefix = path
162+
163+
const bBinFile = resolve(path, 'node_modules/.bin/b')
164+
const nestedBinFile = resolve(path, 'node_modules/a/node_modules/.bin/b')
165+
166+
rebuild(['b@2'], err => {
167+
if (err)
168+
throw err
169+
170+
t.throws(() => fs.statSync(bBinFile), 'should not link b bin')
171+
t.ok(() => fs.statSync(nestedBinFile), 'should link filtered pkg bin')
172+
173+
t.end()
174+
})
175+
})
176+
177+
t.test('filter must be a semver version/range', t => {
178+
rebuild(['b:git+ssh://github.com/npm/arborist'], err => {
179+
t.match(
180+
err,
181+
/Error: `npm rebuild` only supports SemVer version\/range specifiers/,
182+
'should throw type error'
183+
)
184+
185+
t.end()
186+
})
187+
})
188+
189+
t.test('global prefix', t => {
190+
const globalPath = t.testdir({
191+
lib: {
192+
node_modules: {
193+
a: {
194+
'index.js': '',
195+
'package.json': JSON.stringify({
196+
name: 'a',
197+
version: '1.0.0',
198+
bin: 'index.js',
199+
}),
200+
},
201+
},
202+
},
203+
})
204+
205+
npm.flatOptions.global = true
206+
npm.globalDir = resolve(globalPath, 'lib', 'node_modules')
207+
208+
rebuild([], err => {
209+
if (err)
210+
throw err
211+
212+
t.ok(() => fs.statSync(resolve(globalPath, 'lib/node_modules/.bin/a')))
213+
214+
t.equal(
215+
result,
216+
'rebuilt dependencies successfully',
217+
'should output success msg'
218+
)
219+
220+
t.end()
221+
})
222+
})

0 commit comments

Comments
 (0)