Debian Codegraph @

smoke-tests/test/index.js

raw
1 const { join } = require('node:path')
2 const t = require('tap')
3 const setup = require('./fixtures/setup.js')
4
5 t.test('basic', async t => {
6 const { registry, npm, npmPath, npmLocal, readFile, paths } = await setup(t, {
7 testdir: {
8 packages: {
9 'abbrev-1.0.4': {
10 'package.json': { name: 'abbrev', version: '1.0.4' },
11 'index.js': 'module.exports = "1.0.4"',
12 },
13 'abbrev-1.1.1': {
14 'package.json': { name: 'abbrev', version: '1.1.1' },
15 'index.js': 'module.exports = "1.1.1"',
16 },
17 'promise-all-reject-late': {
18 'package.json': { name: 'promise-all-reject-late', version: '5.0.0' },
19 'index.js': 'module.exports = null',
20 },
21 'exec-test-1.0.0': {
22 'package.json': { name: 'exec-test', version: '1.0.0', bin: { 'exec-test': 'run.sh' } },
23 'index.js': 'module.exports = "1.0.0"',
24 'run.sh': 'echo 1.0.0',
25 },
26 'exec-test-1.0.1': {
27 'package.json': { name: 'exec-test', version: '1.0.1', bin: { 'exec-test': 'run.sh' } },
28 'index.js': 'module.exports = "1.0.1"',
29 'run.sh': 'echo 1.0.1',
30 },
31 },
32 },
33 })
34
35 const abbrevManifest = () => registry.manifest({ name: 'abbrev', versions: ['1.0.4', '1.1.1'] })
36
37 await t.test('npm init', async t => {
38 const cmdRes = await npm('init', '-y')
39
40 t.matchSnapshot(cmdRes.stdout, 'should have successful npm init result')
41 const pkg = await readFile('package.json')
42 t.equal(pkg.name, 'project', 'should have expected generated name')
43 t.equal(pkg.version, '1.0.0', 'should have expected generated version')
44 })
45
46 await t.test('npm root', async t => {
47 const npmRoot = await npm('help').then(setup.getNpmRoot)
48
49 if (setup.SMOKE_PUBLISH) {
50 const globalNpmRoot = await npmPath('help').then(setup.getNpmRoot)
51 t.rejects(npmLocal('help'), 'npm local rejects during smoke publish')
52 t.not(npmRoot, setup.CLI_ROOT, 'npm root is not the local source dir')
53 t.equal(
54 npmRoot,
55 globalNpmRoot,
56 'during smoke publish, npm root and global root are equal'
57 )
58 } else {
59 t.equal(
60 await npmLocal('help').then(setup.getNpmRoot),
61 setup.CLI_ROOT,
62 'npm local root is the local source dir'
63 )
64 t.equal(npmRoot, setup.CLI_ROOT, 'npm root is the local source dir')
65 }
66 })
67
68 await t.test('npm --version', async t => {
69 const v = await npm('--version').then(r => r.stdout.trim())
70
71 if (setup.SMOKE_PUBLISH) {
72 t.match(v, /-[0-9a-f]{40}\.\d$/, 'must have a git version')
73 } else {
74 t.match(v, /^\d+\.\d+\.\d+/, 'has a version')
75 }
76 })
77
78 await t.test('npm (no args)', async t => {
79 const err = await npm('--loglevel=notice').catch(e => e)
80
81 t.equal(err.code, 1, 'should exit with error code')
82 t.equal(err.stderr, '', 'should have no stderr output')
83 t.matchSnapshot(err.stdout, 'should have expected no args output')
84 })
85
86 await t.test('npm install prodDep@version', async t => {
87 const manifest = abbrevManifest()
88 await registry.package({
89 manifest: manifest,
90 tarballs: { '1.0.4': join(paths.root, 'packages', 'abbrev-1.0.4') },
91 })
92
93 const cmdRes = await npm('install', '[email protected]')
94
95 t.matchSnapshot(cmdRes.stdout, 'should have expected install reify output')
96 t.resolveMatchSnapshot(readFile('package.json'), 'should have expected package.json result')
97 t.resolveMatchSnapshot(readFile('package-lock.json'), 'should have expected lockfile result')
98 })
99
100 await t.test('npm install dev dep', async t => {
101 const manifest = registry.manifest({
102 name: 'promise-all-reject-late',
103 packuments: [{ version: '5.0.0', funding: 'https://github.com/sponsors' }],
104 })
105 await registry.package({
106 manifest: manifest,
107 tarballs: { '5.0.0': join(paths.root, 'packages', 'promise-all-reject-late') },
108 })
109
110 const cmdRes = await npm('install', 'promise-all-reject-late', '-D')
111
112 t.matchSnapshot(cmdRes.stdout, 'should have expected dev dep added reify output')
113 t.resolveMatchSnapshot(
114 readFile('package.json'),
115 'should have expected dev dep added package.json result'
116 )
117 t.resolveMatchSnapshot(
118 readFile('package-lock.json'),
119 'should have expected dev dep added lockfile result'
120 )
121 })
122
123 await t.test('npm ls', async t => {
124 const cmdRes = await npm('ls')
125
126 t.matchSnapshot(cmdRes.stdout, 'should have expected ls output')
127 })
128
129 await t.test('npm fund', async t => {
130 const cmdRes = await npm('fund')
131
132 t.matchSnapshot(cmdRes.stdout, 'should have expected fund output')
133 })
134
135 await t.test('npm explain', async t => {
136 const cmdRes = await npm('explain', 'abbrev')
137
138 t.matchSnapshot(cmdRes.stdout, 'should have expected explain output')
139 })
140
141 await t.test('npm diff', async t => {
142 const manifest = abbrevManifest()
143 await registry.package({
144 manifest: manifest,
145 tarballs: { '1.0.4': join(paths.root, 'packages', 'abbrev-1.0.4') },
146 })
147 await registry.package({
148 manifest: manifest,
149 tarballs: { '1.1.1': join(paths.root, 'packages', 'abbrev-1.1.1') },
150 })
151
152 const cmdRes = await npm('diff', '[email protected]', '[email protected]')
153
154 t.matchSnapshot(cmdRes.stdout, 'should have expected diff output')
155 })
156
157 await t.test('npm outdated', async t => {
158 await registry.package({
159 manifest: registry.manifest({
160 name: 'promise-all-reject-late',
161 versions: ['5.0.0'],
162 }),
163 })
164 await registry.package({
165 manifest: abbrevManifest(),
166 })
167
168 const outdated = await npm('outdated').catch(e => e)
169
170 t.equal(outdated.code, 1, 'should exit with error code')
171 t.not(outdated.stderr, '', 'should have stderr output')
172 t.matchSnapshot(outdated.stdout, 'should have expected outdated output')
173 })
174
175 await t.test('npm pkg set scripts', async t => {
176 const cmdRes = await npm('pkg', 'set', 'scripts.hello=echo Hello')
177
178 t.matchSnapshot(cmdRes.stdout, 'should have expected set-script output')
179 t.resolveMatchSnapshot(
180 readFile('package.json'),
181 'should have expected script added package.json result'
182 )
183 })
184
185 await t.test('npm run', async t => {
186 const cmdRes = await npm('run-script', 'hello')
187
188 t.matchSnapshot(cmdRes.stdout, 'should have expected run output')
189 })
190
191 await t.test('npm prefix', async t => {
192 const cmdRes = await npm('prefix')
193
194 t.matchSnapshot(cmdRes.stdout, 'should have expected prefix output')
195 })
196
197 await t.test('npm view', async t => {
198 await registry.package({
199 manifest: abbrevManifest(),
200 })
201 const cmdRes = await npm('view', '[email protected]')
202
203 t.matchSnapshot(cmdRes.stdout, 'should have expected view output')
204 })
205
206 await t.test('npm update dep', async t => {
207 const manifest = abbrevManifest()
208 await registry.package({
209 manifest: manifest,
210 tarballs: {
211 '1.1.1': join(paths.root, 'packages', 'abbrev-1.1.1'),
212 },
213 })
214
215 const cmdRes = await npm('update', 'abbrev')
216
217 t.matchSnapshot(cmdRes.stdout, 'should have expected update reify output')
218 t.resolveMatchSnapshot(readFile('package.json'),
219 'should have expected update package.json result')
220 t.resolveMatchSnapshot(readFile('package-lock.json'),
221 'should have expected update lockfile result')
222 })
223
224 await t.test('npm uninstall', async t => {
225 const cmdRes = await npm('uninstall', 'promise-all-reject-late')
226
227 t.matchSnapshot(cmdRes.stdout, 'should have expected uninstall reify output')
228 t.resolveMatchSnapshot(readFile('package.json'),
229 'should have expected uninstall package.json result')
230 t.resolveMatchSnapshot(readFile('package-lock.json'),
231 'should have expected uninstall lockfile result')
232 })
233
234 await t.test('npm pkg', async t => {
235 let cmdRes = await npm('pkg', 'get', 'license')
236 t.matchSnapshot(cmdRes.stdout, 'should have expected pkg get output')
237
238 cmdRes = await npm('pkg', 'set', 'tap[test-env][0]=LC_ALL=sk')
239 t.matchSnapshot(cmdRes.stdout, 'should have expected pkg set output')
240
241 t.resolveMatchSnapshot(
242 readFile('package.json'),
243 'should have expected npm pkg set modified package.json result'
244 )
245
246 cmdRes = await npm('pkg', 'get')
247 t.matchSnapshot(cmdRes.stdout, 'should print package.json contents')
248
249 cmdRes = await npm('pkg', 'delete', 'tap')
250 t.matchSnapshot(cmdRes.stdout, 'should have expected pkg delete output')
251
252 t.resolveMatchSnapshot(
253 readFile('package.json'),
254 'should have expected npm pkg delete modified package.json result'
255 )
256 })
257
258 await t.test('npm update --no-save --no-package-lock', async t => {
259 const manifest = abbrevManifest()
260 await registry.package({
261 manifest: manifest,
262 tarballs: {
263 '1.0.4': join(paths.root, 'packages', 'abbrev-1.0.4'),
264 },
265 })
266
267 // setup, manually reset dep value
268 await npm('pkg', 'set', 'dependencies.abbrev==1.0.4')
269 await npm('install')
270
271 await registry.package({
272 manifest: manifest,
273 tarballs: {
274 '1.1.1': join(paths.root, 'packages', 'abbrev-1.1.1'),
275 },
276 })
277
278 await npm('pkg', 'set', 'dependencies.abbrev=^1.0.4')
279 await npm('update', '--no-save', '--no-package-lock')
280
281 t.equal(
282 (await readFile('package.json')).dependencies.abbrev,
283 '^1.0.4',
284 'should have expected update --no-save --no-package-lock package.json result'
285 )
286 t.equal(
287 (await readFile('package-lock.json')).packages['node_modules/abbrev'].version,
288 '1.0.4',
289 'should have expected update --no-save --no-package-lock lockfile result'
290 )
291 t.equal(
292 (await readFile('node_modules/abbrev/package.json')).version,
293 '1.1.1',
294 'actual installed version is 1.1.1'
295 )
296 })
297
298 await t.test('npm update --no-save', async t => {
299 const manifest = abbrevManifest()
300 await registry.package({
301 manifest: manifest,
302 })
303
304 await npm('update', '--no-save')
305
306 t.equal(
307 (await readFile('package.json')).dependencies.abbrev,
308 '^1.0.4',
309 'should have expected update --no-save package.json result'
310 )
311 t.equal(
312 (await readFile('package-lock.json')).packages['node_modules/abbrev'].version,
313 '1.1.1',
314 'should have expected update --no-save lockfile result'
315 )
316 })
317
318 await t.test('npm update --save', async t => {
319 const manifest = abbrevManifest()
320 await registry.package({
321 manifest: manifest,
322 })
323
324 await npm('update', '--save')
325
326 t.equal(
327 (await readFile('package.json')).dependencies.abbrev,
328 '^1.1.1',
329 'should have expected update --save package.json result'
330 )
331 t.equal(
332 (await readFile('package-lock.json')).packages['node_modules/abbrev'].version,
333 '1.1.1',
334 'should have expected update --save lockfile result'
335 )
336 })
337
338 await t.test('npm ci', async t => {
339 await npm('uninstall', 'abbrev')
340
341 const manifest = abbrevManifest()
342 await registry.package({
343 manifest: manifest,
344 tarballs: {
345 '1.0.4': join(paths.root, 'packages', 'abbrev-1.0.4'),
346 },
347 })
348
349 await npm('install', '[email protected]', '--save-exact')
350
351 t.equal(
352 (await readFile('package-lock.json')).packages['node_modules/abbrev'].version,
353 '1.0.4',
354 'should have stored exact installed version'
355 )
356
357 await npm('pkg', 'set', 'dependencies.abbrev=^1.1.1')
358
359 await registry.package({
360 manifest,
361 })
362
363 const err = await npm('ci', '--loglevel=error').catch(e => e)
364 t.equal(err.code, 1)
365 t.matchSnapshot(err.stderr, 'should throw mismatch deps in lock file error')
366 })
367
368 await t.test('npm exec', async t => {
369 if (process.platform === 'win32') {
370 t.skip()
371 return
372 }
373 // First run finds package
374 {
375 const packument = registry.packument({
376 name: 'exec-test', version: '1.0.0', bin: { 'exec-test': 'run.sh' },
377 })
378 const manifest = registry.manifest({ name: 'exec-test', packuments: [packument] })
379 await registry.package({
380 times: 2,
381 manifest,
382 tarballs: {
383 '1.0.0': join(paths.root, 'packages', 'exec-test-1.0.0'),
384 },
385 })
386
387 const o = await npm('exec', 'exec-test')
388 t.match(o.stdout.trim(), '1.0.0')
389 }
390 // Second run finds newer version
391 {
392 const packument = registry.packument({
393 name: 'exec-test', version: '1.0.1', bin: { 'exec-test': 'run.sh' },
394 })
395 const manifest = registry.manifest({ name: 'exec-test', packuments: [packument] })
396 await registry.package({
397 times: 2,
398 manifest,
399 tarballs: {
400 '1.0.1': join(paths.root, 'packages', 'exec-test-1.0.1'),
401 },
402 })
403 const o = await npm('exec', 'exec-test')
404 t.match(o.stdout.trim(), '1.0.1')
405 }
406 })
407 })