Debian Codegraph @

workspaces/arborist/test/shrinkwrap.js

raw
1 const Shrinkwrap = require('../lib/shrinkwrap.js')
2 const Node = require('../lib/node.js')
3 const Link = require('../lib/link.js')
4 const calcDepFlags = require('../lib/calc-dep-flags.js')
5 const fs = require('node:fs')
6 const Arborist = require('../lib/arborist/index.js')
7
8 const t = require('tap')
9
10 const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')
11 t.cleanSnapshot = s => s.split(process.cwd()).join('{CWD}')
12
13 const { resolve } = require('node:path')
14 const fixture = resolve(__dirname, 'fixtures/install-types')
15 const swonlyFixture = resolve(__dirname, 'fixtures/install-types-sw-only')
16 const YarnLock = require('../lib/yarn-lock.js')
17 const yarnFixture = resolve(__dirname, 'fixtures/yarn-stuff')
18 const emptyFixture = resolve(__dirname, 'fixtures/empty')
19 const depTypesFixture = resolve(__dirname, 'fixtures/dev-deps')
20 const badJsonFixture = resolve(__dirname, 'fixtures/testing-peer-deps-bad-sw')
21 const hiddenLockfileFixture = resolve(__dirname, 'fixtures/hidden-lockfile')
22 const hidden = 'node_modules/.package-lock.json'
23 const saxFixture = resolve(__dirname, 'fixtures/sax')
24
25 // start out with the file being fresh
26 const later = Date.now() + 10000
27 fs.utimesSync(
28 resolve(hiddenLockfileFixture, hidden),
29 new Date(later),
30 new Date(later)
31 )
32
33 t.test('shrinkwrap key order', async t => t.matchSnapshot(Shrinkwrap.keyOrder))
34
35 t.test('path defaults to .', async t => {
36 const sw = new Shrinkwrap()
37 t.equal(sw.path, process.cwd())
38 })
39
40 t.test('load and change lockfileVersion', async t => {
41 const vDefault = await Shrinkwrap.load({ path: fixture })
42 const v1 = await Shrinkwrap.load({ path: fixture, lockfileVersion: 1 })
43 const v2 = await Shrinkwrap.load({ path: fixture, lockfileVersion: 2 })
44 const v3 = await Shrinkwrap.load({ path: fixture, lockfileVersion: 3 })
45
46 t.strictSame(vDefault, v3, 'default is same as version 3')
47 const v1Data = await v1.commit()
48 const v2Data = await v2.commit()
49 const v3Data = await v3.commit()
50 t.strictSame(v2Data, { ...v1Data, ...v3Data, lockfileVersion: 2 },
51 'v2 is superset of v1 and v3')
52 t.equal(v1Data.packages, undefined, 'v1 data does not have packages')
53 t.equal(v3Data.dependencies, undefined, 'v3 data does not have dependencies')
54 })
55
56 t.test('load and then reset gets empty lockfile', async t => {
57 const sw = await Shrinkwrap.load({ path: fixture })
58 sw.reset()
59 t.strictSame(sw.data, {
60 lockfileVersion: 3,
61 requires: true,
62 dependencies: {},
63 packages: {},
64 })
65 t.equal(sw.loadedFromDisk, true)
66 t.equal(sw.filename, resolve(fixture, 'package-lock.json'))
67 })
68
69 t.test('starting out with a reset lockfile is an empty lockfile', async t => {
70 const sw = await Shrinkwrap.reset({ path: fixture })
71 t.strictSame(sw.data, {
72 lockfileVersion: 3,
73 requires: true,
74 dependencies: {},
75 packages: {},
76 })
77 t.equal(sw.originalLockfileVersion, 3)
78 t.equal(sw.loadedFromDisk, true)
79 t.equal(sw.filename, resolve(fixture, 'package-lock.json'))
80 })
81
82 t.test('reset in a bad dir gets an empty lockfile with no lockfile version', async t => {
83 const nullLockDir = t.testdir({
84 'package-lock.json': JSON.stringify(null),
85 })
86
87 const [swMissingLock, swNullLock] = await Promise.all([
88 Shrinkwrap.reset({ path: 'path/which/does/not/exist' }),
89 Shrinkwrap.reset({ path: nullLockDir }),
90 ])
91
92 t.strictSame(swMissingLock.data, {
93 lockfileVersion: 3,
94 requires: true,
95 dependencies: {},
96 packages: {},
97 })
98 t.equal(swMissingLock.lockfileVersion, null)
99 t.equal(swMissingLock.loadedFromDisk, false)
100
101 t.strictSame(swNullLock.data, {
102 lockfileVersion: 3,
103 requires: true,
104 dependencies: {},
105 packages: {},
106 })
107 t.equal(swNullLock.lockfileVersion, null)
108 t.equal(swNullLock.loadedFromDisk, true)
109 })
110
111 t.test('loading in bad dir gets empty lockfile', async t => {
112 const sw = await Shrinkwrap.load({ path: 'path/which/does/not/exist' })
113 t.strictSame(sw.data, {
114 lockfileVersion: 3,
115 requires: true,
116 dependencies: {},
117 packages: {},
118 })
119 t.equal(sw.loadedFromDisk, false)
120 })
121
122 t.test('failure to parse json gets empty lockfile', async t => {
123 const sw = await Shrinkwrap.load({ path: badJsonFixture })
124 t.strictSame(sw.data, {
125 lockfileVersion: 3,
126 requires: true,
127 dependencies: {},
128 packages: {},
129 })
130 t.equal(sw.loadedFromDisk, false)
131 })
132
133 t.test('loading in empty dir gets empty lockfile', async t => {
134 const sw = await Shrinkwrap.load({ path: emptyFixture })
135 t.strictSame(sw.data, {
136 lockfileVersion: 3,
137 requires: true,
138 dependencies: {},
139 packages: {},
140 })
141 t.equal(sw.loadedFromDisk, false)
142 // update with an empty node, set name to node name, not package name
143 const root = new Node({
144 path: emptyFixture,
145 realpath: emptyFixture,
146 })
147 root.peer = false
148 root.dev = false
149 root.devOptional = false
150 root.optional = false
151 root.extraneous = false
152 sw.add(root)
153 t.strictSame(sw.commit(), {
154 name: 'empty',
155 lockfileVersion: 3,
156 requires: true,
157 packages: {},
158 })
159 })
160
161 t.test('look up from locks and such', async t => {
162 const m = await new Shrinkwrap({ path: fixture }).load()
163 t.strictSame(m.get(''), {
164 name: 'a',
165 version: '1.2.3',
166 dependencies: {
167 abbrev: '^1.1.1',
168 'full-git-url': 'git+https://github.com/isaacs/abbrev-js.git',
169 ghshort: 'github:isaacs/abbrev-js',
170 old: 'npm:abbrev@^1.0.3',
171 pinned: 'npm:abbrev@^1.1.1',
172 reg: 'npm:abbrev@^1.1.1',
173 remote: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
174 symlink: 'file:./abbrev-link-target',
175 tarball: 'file:abbrev-1.1.1.tgz',
176 bundler: '1.2.3',
177 },
178 }, 'root metadata')
179 t.match(m.data, {
180 lockfileVersion: 3,
181 requires: true,
182 dependencies: Object,
183 packages: Object,
184 })
185 t.equal(m.loadedFromDisk, true)
186 t.matchSnapshot(m.get('node_modules/abbrev'), 'basic package')
187 t.matchSnapshot(m.get(
188 'node_modules/abbrev/node_modules/@scope/name/node_modules/@otherscope/othername', 'scoped package'))
189 t.matchSnapshot(m.get('package/not/found'), 'not found')
190
191 t.matchSnapshot(m.get('node_modules/old/node_modules/notfound'), 'fall off the dep tree')
192
193 t.test('lockfile', t => {
194 const p = m.data.packages
195 m.data.packages = {}
196 t.matchSnapshot(m.get('node_modules/abbrev'), 'basic pkg, from lock')
197 t.matchSnapshot(m.data.packages, 'saved fetched metadata back to packages section')
198 t.matchSnapshot(m.get(
199 'node_modules/abbrev/node_modules/@scope/name/node_modules/@otherscope/othername', 'scoped package'))
200 t.matchSnapshot(m.get('package/not/found'), 'not found')
201 t.matchSnapshot(m.get('node_modules/full-git-url'), 'full git')
202 t.matchSnapshot(m.get('node_modules/symlink'), 'symlink')
203 t.matchSnapshot(m.get('node_modules/unhosted-git'), 'unhosted git')
204 m.data.packages = p
205 t.end()
206 })
207 })
208
209 t.test('load a shrinkwrap with some dev and optional flags', t =>
210 Shrinkwrap.load({ path: depTypesFixture }).then(m =>
211 t.matchSnapshot(m.data, 'got expected dependency types')))
212
213 t.test('load a legacy shrinkwrap without a package.json', t =>
214 Shrinkwrap.load({ path: swonlyFixture }).then(m =>
215 t.matchSnapshot(m.data, 'did our best with what we had')))
216
217 t.test('throws when attempting to access data before loading', t => {
218 t.throws(() =>
219 new Shrinkwrap().get(), Error('run load() before getting or setting data'))
220 t.throws(() =>
221 new Shrinkwrap().delete(), Error('run load() before getting or setting data'))
222 t.throws(() =>
223 new Shrinkwrap().add(), Error('run load() before getting or setting data'))
224 t.throws(() =>
225 new Shrinkwrap().toJSON(), Error('run load() before getting or setting data'))
226 t.throws(() =>
227 new Shrinkwrap().toString(), Error('run load() before getting or setting data'))
228 t.throws(() =>
229 new Shrinkwrap().save(), Error('run load() before saving data'))
230 t.end()
231 })
232
233 t.test('resolveOptions', async t => {
234 const url = 'https://private.registry.org/deadbeef/registry/-/registry-1.2.3.tgz'
235 const someOtherRegistry = 'https://someother.registry.org/registry/-/registry-1.2.3.tgz'
236 const getData = async (resolveOptions) => {
237 const dir = t.testdir()
238 const meta = await Shrinkwrap.load({
239 path: dir,
240 resolveOptions,
241 lockfileVersion: 2,
242 })
243
244 const root = new Node({
245 pkg: {
246 name: 'root',
247 dependencies: {
248 registry: '^1.0.0',
249 'some-other-registry': '^1.0.0',
250 '@scoped/some-other-registry': '^1.0.0',
251 tar: url,
252 },
253 },
254 path: dir,
255 realpath: dir,
256 meta,
257 })
258
259 const registry = new Node({
260 pkg: { name: 'registry', version: '1.2.3' },
261 resolved: url,
262 integrity: 'sha512-registry',
263 parent: root,
264 })
265
266 const otherRegistry = new Node({
267 pkg: { name: 'some-other-registry', version: '1.2.3' },
268 resolved: someOtherRegistry,
269 integrity: 'sha512-registry',
270 parent: root,
271 })
272
273 const scopedOtherRegistry = new Node({
274 pkg: { name: '@scope/some-other-registry', version: '1.2.3' },
275 resolved: someOtherRegistry,
276 integrity: 'sha512-registry',
277 parent: root,
278 })
279
280 const tar = new Node({
281 pkg: { name: 'tar', version: '1.2.3' },
282 resolved: url,
283 integrity: 'sha512-registry',
284 parent: root,
285 })
286
287 calcDepFlags(root)
288 meta.add(root)
289 return { data: meta.commit(), registry, tar, root, otherRegistry, scopedOtherRegistry }
290 }
291
292 await t.test('omitLockfileRegistryResolved', async t => {
293 const { data } = await getData({ omitLockfileRegistryResolved: true })
294 // registry dependencies in v2 packages and v1 dependencies should
295 // have resolved stripped.
296 t.strictSame(data.packages['node_modules/registry'].resolved, undefined)
297
298 // tar should have resolved because it is not a registry dep.
299 t.strictSame(data.packages['node_modules/tar'].resolved, url)
300 // v1 url dependencies never have resolved.
301 t.strictSame(data.dependencies.tar.resolved, undefined)
302 })
303
304 await t.test('omitLockfileRegistryResolved: false', async t => {
305 const { data } = await getData({ omitLockfileRegistryResolved: false })
306 t.strictSame(data.packages['node_modules/registry'].resolved, url)
307 t.strictSame(data.dependencies.registry.resolved, url)
308
309 t.strictSame(data.packages['node_modules/tar'].resolved, url)
310 // v1 url dependencies never have resolved.
311 t.strictSame(data.dependencies.tar.resolved, undefined)
312 })
313
314 t.test('metaFromNode default', async t => {
315 // test to cover options default.
316 const { registry } = await getData(undefined)
317 t.strictSame(Shrinkwrap.metaFromNode(registry, '').resolved, url)
318 })
319 })
320
321 t.test('construct metadata from node and package data', t => {
322 const meta = new Shrinkwrap({ path: '/home/user/projects/root' })
323 // fake load
324 meta.data = {
325 lockfileVersion: 2,
326 requires: true,
327 dependencies: {},
328 packages: {},
329 }
330
331 const root = new Node({
332 pkg: {
333 name: 'root',
334 dependencies: { a: '', link: '', link2: '', link3: '' },
335 devDependencies: { d: '', e: 'https://foo.com/e.tgz', devit: '' },
336 optionalDependencies: { optin: '' },
337 peerDependencies: { peer: '' },
338 scripts: {
339 install: 'true',
340 },
341 },
342 path: '/home/user/projects/root',
343 realpath: '/home/user/projects/root',
344 meta,
345 })
346
347 const peer = new Node({
348 pkg: {
349 name: 'peer',
350 version: '1.2.3',
351 dependencies: { peerdep: '' },
352 },
353 resolved: 'https://peer.com/peer.tgz',
354 integrity: 'sha512-peerpeerpeer',
355 parent: root,
356 })
357
358 const peerdep = new Node({
359 pkg: {
360 name: 'peerdep',
361 version: '1.2.3',
362 },
363 resolved: 'https://peer.com/peerdep.tgz',
364 integrity: 'sha512-peerdeppeerdep',
365 parent: peer,
366 })
367
368 const e = new Node({
369 pkg: {
370 name: 'e',
371 version: '1.2.3',
372 dependencies: {
373 tgz: '',
374 'tgz-pkg-resolved': '',
375 },
376 // expect to get "license": "blurb" in the metadata, not an object
377 license: {
378 type: 'blurb',
379 url: 'http://example.com/',
380 },
381 },
382 resolved: 'https://foo.com/e.tgz',
383 parent: root,
384 })
385
386 const tgz = new Node({
387 pkg: {
388 name: 'tgz',
389 version: '1.2.3',
390 funding: { url: 'https://example.com/' },
391 engines: {
392 node: '>=10',
393 },
394 os: ['any', '!win32'],
395 cpu: ['x64'],
396 libc: ['glibc'],
397 },
398 resolved: '/home/user/projects/root/archives/tarball.tgz',
399 parent: root,
400 })
401 const tgzPkgResolved = new Node({
402 pkg: {
403 name: 'tgz-pkg-resolved',
404 version: '1.2.3',
405 funding: { url: 'https://example.com/' },
406 _resolved: '/home/user/projects/root/archives/tarball-pkg-resolved.tgz',
407 _integrity: 'sha512-tarball/package/resolved/integrity',
408 },
409 parent: root,
410 })
411
412 const link = new Link({
413 path: '/home/user/projects/root/node_modules/link',
414 realpath: '/home/user/projects/root/target',
415 parent: root,
416 pkg: {
417 name: 'link',
418 version: '1.2.3',
419 funding: 'https://example.com/payme',
420 _resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
421 },
422 target: new Node({
423 realpath: '/home/user/projects/root/target',
424 path: '/home/user/projects/root/target',
425 root,
426 pkg: {
427 name: 'link',
428 version: '1.2.3',
429 funding: 'https://example.com/payme',
430 _resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
431 },
432 }),
433 })
434 // special case when link alias is the same as target package name
435 const link3 = new Link({
436 name: 'link3',
437 path: '/home/user/projects/root/node_modules/link3',
438 realpath: '/home/user/projects/root/realPkg',
439 parent: root,
440 pkg: {
441 name: 'link3',
442 version: '1.2.3',
443 funding: 'https://example.com/payme',
444 _resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
445 },
446 target: new Node({
447 name: 'link3',
448 realpath: '/home/user/projects/root/realPkg',
449 path: '/home/user/projects/root/realPkg',
450 root,
451 pkg: {
452 name: 'link3',
453 version: '1.2.3',
454 funding: 'https://example.com/payme',
455 _resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
456 },
457 }),
458 })
459
460 const a = new Node({
461 resolved: 'https://example.com/a.tgz',
462 integrity: 'sha512-helloyesthisisdog',
463 pkg: {
464 name: 'a',
465 version: '1.2.3',
466 },
467 parent: root,
468 })
469
470 const optin = new Node({
471 pkg: {
472 name: 'optin',
473 version: '1.2.3',
474 dependencies: { devo: '' },
475 },
476 parent: root,
477 })
478
479 const devit = new Node({
480 pkg: {
481 name: 'devit',
482 version: '1.2.3',
483 dependencies: { devo: '' },
484 },
485 parent: root,
486 })
487
488 const devo = new Node({
489 pkg: {
490 name: 'devo',
491 version: '1.2.3',
492 },
493 parent: root,
494 })
495
496 const d = new Node({
497 pkg: {
498 name: 'd',
499 version: '1.2.3',
500 _shasum: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
501 bundleDependencies: ['bundled'],
502 dependencies: {
503 e: '1.2.3',
504 bundled: 'npm:not-bundled-tho@*',
505 'git-dep': 'github:foo/bar',
506 },
507 },
508 parent: root,
509 })
510 const bundled = new Node({
511 pkg: {
512 name: 'not-bundled-tho',
513 version: '1.2.3',
514 },
515 path: d.path + '/node_modules/bundled',
516 name: 'bundled',
517 parent: d,
518 })
519 const git = new Node({
520 pkg: {
521 name: 'git-dep',
522 version: '1.2.3',
523 },
524 parent: d,
525 resolved: 'git+ssh://[email protected]/foo/bar.git#0000000000000000000000000000000000000000',
526 })
527
528 const nopkg = new Node({
529 path: root.path + '/node_modules/nopkg',
530 parent: root,
531 })
532
533 calcDepFlags(root)
534
535 t.matchSnapshot(meta.get(''), 'root metadata, no package version')
536 root.package.version = '1.2.3'
537 meta.add(root)
538 t.matchSnapshot(meta.get(''), 'root metadata, with package version')
539
540 t.matchSnapshot(meta.get(optin.location), 'meta for optional dep')
541 t.matchSnapshot(meta.get(devit.location), 'meta for dev dep')
542 t.matchSnapshot(meta.get(devo.location), 'meta for devOptional dep')
543
544 t.matchSnapshot(meta.get(tgz.location), 'metadata for tarball file pkg')
545 t.matchSnapshot(meta.get(tgzPkgResolved.location),
546 'metadata for tarball file pkg with _resolved value')
547 t.matchSnapshot(meta.get(link.path), 'link metadata')
548 t.matchSnapshot(meta.get(link.target.path), 'link target metadata')
549
550 t.matchSnapshot(meta.get(link3.path), 'link metadata with same pkg name as link target')
551 t.matchSnapshot(meta.get(link3.target.path), 'link target metadata with same pkg name as link target')
552
553 t.matchSnapshot(meta.get(a.location), 'dep a metadata')
554 t.matchSnapshot(meta.get(d.location), 'dep d metadata')
555 t.matchSnapshot(meta.get(e.location), 'dep e metadata')
556 t.matchSnapshot(meta.get(bundled.location), 'bundled pkg metadata')
557 t.matchSnapshot(meta.get(git.location), 'git dep metadata')
558 t.matchSnapshot(meta.get(nopkg.location), 'node without a package')
559 t.matchSnapshot(meta.get(peer.location), 'a peer dep')
560 t.matchSnapshot(meta.get(peerdep.location), 'a peer meta-dep')
561 t.matchSnapshot(meta.commit(), 'data calculated from nodes themselves')
562 t.end()
563 })
564
565 t.test('saving dependency-free shrinkwrap object', t => {
566 const dir = t.testdir({
567 'package.json': JSON.stringify({
568 name: 'badsave',
569 version: '1.0.0',
570 description: 'no node_modules/ will fail',
571 }),
572 })
573
574 t.test('save meta lockfile into node_modules directory', async t => {
575 const sw = await Shrinkwrap.load({ path: dir, hiddenLockfile: true })
576 t.equal(
577 sw.filename,
578 resolve(`${dir}/node_modules/.package-lock.json`),
579 'correct filepath on shrinkwrap instance'
580 )
581 // save does not throw, but doesn't write the file
582 await sw.save()
583 t.throws(() => fs.statSync(sw.filename))
584 })
585
586 t.test('if save fails, it does throw, if not a hidden lockfile', async t => {
587 const sw = await Shrinkwrap.load({ path: dir, hiddenLockfile: false })
588 sw.filename = dir + '/this/path/does/not/exist.json'
589 await t.rejects(sw.save(), { code: 'ENOENT' })
590 t.throws(() => fs.statSync(sw.filename))
591 })
592
593 t.test('save lockfile to root directory', async t => {
594 const sw = await Shrinkwrap.load({ path: dir })
595 t.equal(
596 sw.filename,
597 resolve(`${dir}/package-lock.json`),
598 'correct filepath on shrinkwrap instance'
599 )
600 await sw.save({ format: false })
601 fs.statSync(sw.filename)
602 t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'no indent json output')
603 })
604
605 t.test('load the unindented file, and generate expected contents', async t => {
606 const sw = await Shrinkwrap.load({ path: dir })
607 t.equal(
608 sw.filename,
609 resolve(`${dir}/package-lock.json`),
610 'correct filepath on shrinkwrap instance'
611 )
612 t.equal(sw.indent, '')
613 const json = await sw.toJSON()
614 t.matchSnapshot(json, 'indented json object output')
615
616 const jsonString = await sw.toString()
617 t.matchSnapshot(jsonString, 'indented json string output')
618 })
619
620 t.test('load the unindented file, and save it back default', async t => {
621 const sw = await Shrinkwrap.load({ path: dir })
622 t.equal(
623 sw.filename,
624 resolve(`${dir}/package-lock.json`),
625 'correct filepath on shrinkwrap instance'
626 )
627 t.equal(sw.indent, '')
628 await sw.save()
629 t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'indented json output')
630 })
631
632 t.test('load file, and save it with a custom format', async t => {
633 const sw = await Shrinkwrap.load({ path: dir })
634 t.equal(
635 sw.filename,
636 resolve(`${dir}/package-lock.json`),
637 'correct filepath on shrinkwrap instance'
638 )
639 await sw.save({ format: '\t' })
640 t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'custom indented json output')
641 })
642
643 t.end()
644 })
645
646 t.test('write the shrinkwrap back to disk', t => {
647 const dir = t.testdir({})
648 t.test('just read and write back', async t => {
649 const s = await Shrinkwrap.load({ path: fixture, lockfileVersion: 1 })
650 const fileData = require(s.filename)
651 s.filename = dir + '/test-shrinkwrap.json'
652 const shrinkwrapData = await s.commit()
653 t.strictSame(shrinkwrapData, fileData, 'saved json matches data')
654 })
655 t.test('write back with pending changes', async t => {
656 const s = await Shrinkwrap.load({ path: fixture, lockfileVersion: 2 })
657 const dir = t.testdir({})
658 s.filename = dir + '/test-shrinkwrap-with-changes.json'
659 const node = new Node({
660 path: fixture + '/node_modules/newthing',
661 pkg: {
662 name: 'newthing',
663 version: '1.2.3',
664 },
665 })
666 s.add(node)
667 const preCommit = JSON.parse(JSON.stringify(s.data))
668 const postCommit = s.commit()
669 t.notSame(postCommit, preCommit, 'committing changes the data')
670 // delete and re-add to put us back in the pre-commit state
671 s.delete(node.location)
672 s.add(node)
673 await s.save()
674 t.strictSame(s.data, postCommit, 'committed changes to data')
675 t.strictSame(require(s.filename), s.data, 'saved json matches data')
676 })
677 t.end()
678 })
679
680 t.test('load shrinkwrap if no package-lock.json present', async t => {
681 const dir = t.testdir({
682 'npm-shrinkwrap.json': JSON.stringify({
683 lockfileVersion: 1,
684 name: 'foo',
685 version: '1.2.3',
686 }),
687 })
688 let s
689 s = await Shrinkwrap.load({ path: dir, shrinkwrapOnly: true })
690 t.equal(s.type, 'npm-shrinkwrap.json', 'loaded with swonly')
691 s = await Shrinkwrap.reset({ path: dir, shrinkwrapOnly: true })
692 t.equal(s.type, 'npm-shrinkwrap.json', 'loaded fresh')
693 s = await Shrinkwrap.load({ path: dir })
694 t.equal(s.type, 'npm-shrinkwrap.json', 'loaded without swonly')
695 s = await Shrinkwrap.reset({ path: dir })
696 t.equal(s.type, 'npm-shrinkwrap.json', 'loaded fresh without swonly')
697 })
698
699 t.test('load yarn.lock file if present', async t => {
700 const s = await Shrinkwrap.load({ path: yarnFixture })
701 t.type(s.yarnLock, YarnLock, 'loaded a yarn lock file')
702 t.not(s.yarnLock.entries.size, 0, 'got some entries')
703 })
704
705 t.test('save yarn lock if loaded', async t => {
706 const s = await Shrinkwrap.load({ path: yarnFixture })
707 s.path = t.testdir()
708 s.filename = s.path + '/package-lock.json'
709 await s.save()
710 const ss = await Shrinkwrap.load({ path: s.path })
711 t.strictSame(s.yarnLock, ss.yarnLock)
712 })
713
714 t.test('ignore yarn lock file parse errors', async t => {
715 const dir = t.testdir({
716 'yarn.lock': 'this is not a yarn lock file!',
717 })
718 const s = await Shrinkwrap.load({ path: dir })
719 t.type(s.yarnLock, YarnLock, 'got a yarn lock object because a yarn lock exists')
720 t.equal(s.yarnLock.entries.size, 0, 'did not get any entries out of it')
721 })
722
723 t.test('load a resolution from yarn.lock if we do not have our own', async t => {
724 const path = t.testdir({
725 'yarn.lock': `
726 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
727 # yarn lockfile v1
728
729 mkdirp@^1.0.2:
730 version "1.0.2"
731 resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
732 integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
733
734 mcdork@^1.0.0:
735 version "1.0.2"
736 resolved "https://registry.npmjs.org/mcdork/-/mcdork-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
737 integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
738
739 otherreg@^1.0.0:
740 version "1.0.2"
741 resolved "https://otherreg.registry.npm/othrreg/-/othrreg-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
742 integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
743
744 brokenempty@^1.2.3:
745 version "1.2.3"
746
747 regalias@xyz:
748 version "1.0.2"
749 resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
750 integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
751 `,
752 'package.json': JSON.stringify({
753 dependencies: {
754 mkdirp: '^1.0.2',
755 },
756 }),
757 })
758 const s = await Shrinkwrap.load({ path })
759 {
760 const opt = {}
761 t.equal(s.checkYarnLock('mkdirp@^1.0.2', opt).raw, '[email protected]')
762 t.strictSame(opt, {
763 resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.2.tgz',
764 integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
765 })
766 }
767 {
768 const opt = {}
769 t.equal(s.checkYarnLock('mcdork@^1.0.0', opt).raw, '[email protected]')
770 t.strictSame(opt, {
771 resolved: 'https://registry.npmjs.org/mcdork/-/mcdork-1.0.2.tgz',
772 integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
773 })
774 }
775 {
776 const opt = {}
777 t.equal(s.checkYarnLock('notfound@^1.0.0', opt).raw, 'notfound@^1.0.0')
778 t.strictSame(opt, {})
779 }
780 {
781 const opt = {}
782 t.equal(s.checkYarnLock('otherreg@^1.0.0', opt).raw, 'otherreg@https://otherreg.registry.npm/othrreg/-/othrreg-1.0.2.tgz')
783 t.strictSame(opt, {
784 resolved: 'https://otherreg.registry.npm/othrreg/-/othrreg-1.0.2.tgz',
785 integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
786 })
787 }
788 {
789 const opt = {}
790 t.equal(s.checkYarnLock('brokenempty@^1.2.3', opt).raw, 'brokenempty@^1.2.3')
791 t.strictSame(opt, {})
792 }
793 {
794 const opt = {}
795 t.equal(s.checkYarnLock('regalias@xyz', opt).raw, 'regalias@npm:[email protected]')
796 t.strictSame(opt, {
797 resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.2.tgz',
798 integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
799 })
800 }
801
802 t.equal(s.checkYarnLock('mkdirp').raw, 'mkdirp')
803
804 {
805 s.loadedFromDisk = true
806 const opt = {}
807 t.equal(s.checkYarnLock('regalias@xyz', opt).raw, 'regalias@xyz')
808 t.strictSame(opt, {})
809 s.loadedFromDisk = false
810 }
811 {
812 const { yarnLock } = s
813 s.yarnLock = null
814 const opt = {}
815 t.equal(s.checkYarnLock('regalias@xyz', opt).raw, 'regalias@xyz')
816 t.strictSame(opt, {})
817 s.yarnLock = yarnLock
818 }
819 })
820
821 t.test('handle missing dependencies object without borking', t => {
822 const s = new Shrinkwrap({ path: '/path/to/root' })
823 s.data = {
824 packages: {
825 'node_modules/foo': {},
826 'node_modules/foo/node_modules/bar': {},
827 },
828 dependencies: {
829 foo: {
830 resolved: 'http://foo.com',
831 integrity: 'foo integrity',
832 // no dependencies object here!
833 },
834 },
835 }
836 s.delete('/path/to/root/node_modules/foo/node_modules/bar')
837 t.matchSnapshot(s.commit())
838 t.end()
839 })
840
841 t.test('load a hidden lockfile', async t => {
842 // ensure the hidden lockfile is newer than the contents
843 // otherwise this can fail on a fresh checkout.
844 fs.utimesSync(resolve(hiddenLockfileFixture, hidden), new Date(), new Date())
845 const s = await Shrinkwrap.load({
846 path: hiddenLockfileFixture,
847 hiddenLockfile: true,
848 })
849 t.matchSnapshot(s.data)
850 // make sure it does not add to the dependencies block when a new
851 // node is added.
852 s.data.dependencies = {}
853 s.add(new Node({
854 path: hiddenLockfileFixture + '/node_modules/foo',
855 pkg: {
856 name: 'foo',
857 version: '1.2.3',
858 _integrity: 'sha512-deadbeef',
859 _resolved: 'https://registry.npmjs.org/foo/-/foo-1.2.3.tgz',
860 },
861 }))
862 t.strictSame(s.data.dependencies, {}, 'did not add to legacy data')
863 const data = s.commit()
864 t.equal(data.packages[''], undefined, 'no root entry')
865 t.equal(data.dependencies, undefined, 'deleted legacy metadata')
866 })
867
868 t.test('skip inert nodes in commit', async t => {
869 // When a proxy registry returns 404 or incomplete manifests for
870 // platform-specific optional deps, #pruneFailedOptional marks them
871 // inert. commit() must skip inert nodes — otherwise the lockfile
872 // gets entries like {"optional": true} without version/resolved/integrity
873 // that cause "Invalid Version:" errors on subsequent npm ci.
874 const path = t.testdir({
875 'package.json': JSON.stringify({
876 name: 'proxy-registry-repro',
877 version: '1.0.0',
878 devDependencies: { esbuild: '^0.27.0' },
879 }),
880 })
881 const meta = new Shrinkwrap({ path })
882 meta.data = {
883 lockfileVersion: 3,
884 packages: {},
885 }
886
887 const root = new Node({
888 pkg: {
889 name: 'proxy-registry-repro',
890 version: '1.0.0',
891 devDependencies: { esbuild: '^0.27.0' },
892 },
893 path,
894 realpath: path,
895 })
896
897 // esbuild with full metadata (valid)
898 const esbuild = new Node({
899 pkg: {
900 name: 'esbuild',
901 version: '0.27.7',
902 optionalDependencies: {
903 '@esbuild/linux-x64': '0.27.7',
904 '@esbuild/aix-ppc64': '0.27.7',
905 },
906 },
907 name: 'esbuild',
908 parent: root,
909 })
910 esbuild.dev = true
911
912 // platform dep with full metadata (current platform — valid, NOT inert)
913 const validDep = new Node({
914 pkg: {
915 name: '@esbuild/linux-x64',
916 version: '0.27.7',
917 os: ['linux'],
918 cpu: ['x64'],
919 },
920 name: '@esbuild/linux-x64',
921 parent: root,
922 })
923 validDep.optional = true
924 validDep.dev = true
925
926 // platform dep marked inert (proxy registry 404'd or returned incomplete manifest)
927 // #pruneFailedOptional sets inert=true on these nodes
928 const brokenDep = new Node({
929 pkg: {
930 name: '@esbuild/aix-ppc64',
931 // no version — proxy registry returned 404 or incomplete manifest
932 },
933 name: '@esbuild/aix-ppc64',
934 parent: esbuild,
935 })
936 brokenDep.optional = true
937 brokenDep.dev = true
938 brokenDep.extraneous = false
939 brokenDep.inert = true
940
941 // file: optional dep WITHOUT version (legitimate — NOT inert, should be kept)
942 const fileDep = new Node({
943 pkg: {
944 name: 'my-local-optional',
945 // no version — but this is a file: dep, so it's legitimate
946 },
947 name: 'my-local-optional',
948 parent: root,
949 resolved: 'file:../my-local-optional',
950 })
951 fileDep.optional = true
952 fileDep.extraneous = false
953
954 // local optional dep WITHOUT version or resolved (NOT inert, should be kept)
955 const localDep = new Node({
956 pkg: {
957 name: 'my-disk-optional',
958 // no version, no resolved — loaded from local node_modules
959 },
960 name: 'my-disk-optional',
961 parent: root,
962 })
963 localDep.optional = true
964 localDep.extraneous = false
965
966 meta.tree = root
967 const committed = meta.commit()
968
969 // The valid platform dep should be in the lockfile
970 const validLoc = 'node_modules/@esbuild/linux-x64'
971 t.ok(
972 committed.packages[validLoc],
973 'valid optional dep is included'
974 )
975 t.equal(
976 committed.packages[validLoc].version,
977 '0.27.7',
978 'valid optional dep has version'
979 )
980
981 // The inert dep should NOT be in the lockfile
982 const brokenLoc = 'node_modules/esbuild/node_modules/@esbuild/aix-ppc64'
983 t.notOk(
984 committed.packages[brokenLoc],
985 'inert optional dep is excluded from lockfile'
986 )
987
988 // The file: optional dep WITHOUT version SHOULD be kept (not inert)
989 const fileLoc = 'node_modules/my-local-optional'
990 t.ok(
991 committed.packages[fileLoc],
992 'file: optional dep without version is preserved in lockfile'
993 )
994
995 // The local (resolved=null) optional dep WITHOUT version SHOULD be kept (not inert)
996 const localLoc = 'node_modules/my-disk-optional'
997 t.ok(
998 committed.packages[localLoc],
999 'local optional dep without version is preserved in lockfile'
1000 )
1001 })
1002
1003 t.test('load a fresh hidden lockfile', async t => {
1004 const sw = await Shrinkwrap.reset({
1005 path: hiddenLockfileFixture,
1006 hiddenLockfile: true,
1007 })
1008 t.strictSame(sw.data, {
1009 lockfileVersion: 3,
1010 requires: true,
1011 dependencies: {},
1012 packages: {},
1013 })
1014 t.equal(sw.loadedFromDisk, true)
1015 t.equal(sw.filename, resolve(hiddenLockfileFixture, hidden))
1016 })
1017
1018 t.test('hidden lockfile only used if up to date', async t => {
1019 const lockdata = require(resolve(hiddenLockfileFixture, hidden))
1020 const path = t.testdir({
1021 node_modules: {
1022 '.package-lock.json': JSON.stringify(lockdata),
1023 abbrev: {
1024 'package.json': JSON.stringify({ name: 'abbrev', version: '1.1.1' }),
1025 },
1026 },
1027 'package.json': JSON.stringify({ dependencies: { abbrev: '1.1.1' } }),
1028 })
1029
1030 // ensure that the lockfile is fresh to start
1031 {
1032 const later = Date.now() + 10000
1033 fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
1034 const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
1035 t.equal(s.loadedFromDisk, true, 'loading from fresh lockfile')
1036 }
1037
1038 // make the node_modules dir have a newer mtime by adding an entry
1039 // and setting the hidden lockfile back in time
1040 {
1041 fs.mkdirSync(resolve(path, 'node_modules/xyz'))
1042 const time = new Date('1999-12-31T23:59:59Z')
1043 fs.utimesSync(resolve(path, hidden), time, time)
1044 const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
1045 t.equal(s.loadedFromDisk, false, 'did not load from disk, updated nm')
1046 t.equal(s.loadingError.message, 'out of date, updated: node_modules')
1047 }
1048
1049 // make the lockfile newer, but that new entry is still a problem
1050 {
1051 const later = Date.now() + 10000
1052 fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
1053 const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
1054 t.equal(s.loadedFromDisk, false, 'did not load, new entry')
1055 t.equal(s.loadingError.message, 'missing from lockfile: node_modules/xyz')
1056 }
1057
1058 // make the lockfile newer, but missing a folder from node_modules
1059 {
1060 fs.rmSync(resolve(path, 'node_modules/abbrev'), { recursive: true, force: true })
1061 fs.rmSync(resolve(path, 'node_modules/xyz'), { recursive: true, force: true })
1062 const later = Date.now() + 10000
1063 fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
1064 const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
1065 t.equal(s.loadedFromDisk, false, 'did not load, missing entry')
1066 t.equal(s.loadingError.message, 'missing from node_modules: node_modules/abbrev')
1067 }
1068 })
1069
1070 t.test('hidden lockfile understands symlinks', async t => {
1071 const path = t.testdir({
1072 node_modules: {
1073 '.package-lock.json': JSON.stringify({
1074 name: 'hidden-lockfile-with-symlink',
1075 lockfileVersion: 2,
1076 requires: true,
1077 packages: {
1078 abbrev: {
1079 version: '1.1.1',
1080 },
1081 'node_modules/abbrev': {
1082 resolved: 'abbrev',
1083 link: true,
1084 },
1085 },
1086 }),
1087 abbrev: t.fixture('symlink', '../abbrev'),
1088
1089 // a symlink missing a target is not relevant
1090 // windows cannot handle missing symlink targets, though
1091 ...(process.platform === 'win32' ? {} : {
1092 missing: t.fixture('symlink', '../missing'),
1093 }),
1094 },
1095 abbrev: {
1096 'package.json': JSON.stringify({
1097 name: 'abbrev',
1098 version: '1.1.1',
1099 }),
1100 },
1101 'package.json': JSON.stringify({
1102 dependencies: {
1103 abbrev: 'file:abbrev',
1104 },
1105 }),
1106 })
1107
1108 // respect it if not newer, and the target included in shrinkwrap
1109 {
1110 const later = Date.now() + 10000
1111 fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
1112 const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
1113 t.equal(s.loadedFromDisk, true, 'loaded from disk')
1114 t.equal(s.filename, resolve(path, hidden))
1115 }
1116
1117 // don't respect if the target is newer than hidden shrinkwrap
1118 {
1119 const later = Date.now() + 20000
1120 fs.utimesSync(resolve(path, 'abbrev/package.json'), new Date(later), new Date(later))
1121 fs.utimesSync(resolve(path, 'abbrev'), new Date(later), new Date(later))
1122 const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
1123 t.equal(s.loadedFromDisk, false, 'not loaded from disk')
1124 t.equal(s.filename, resolve(path, hidden))
1125 }
1126
1127 // don't respect it if the target is not in hidden shrinkwrap
1128 {
1129 fs.mkdirSync(resolve(path, 'missing'))
1130 fs.writeFileSync(resolve(path, 'missing/package.json'), JSON.stringify({
1131 name: 'missing',
1132 version: '1.2.3',
1133 }))
1134 if (process.platform === 'win32') {
1135 fs.symlinkSync(resolve(path, 'missing'), resolve(path, 'node_modules/missing'), 'junction')
1136 }
1137 // even though it's newer, the 'missing' is not found in the lock
1138 const later = Date.now() + 30000
1139 fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
1140
1141 const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
1142 t.equal(s.loadedFromDisk, false, 'not loaded from disk')
1143 t.equal(s.filename, resolve(path, hidden))
1144 }
1145 })
1146
1147 t.test('a yarn.lock entry with version mismatch', async t => {
1148 const path = t.testdir({
1149 'yarn.lock': `
1150 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
1151 # yarn lockfile v1
1152
1153 mkdirp@^1.0.2:
1154 version "1.0.2"
1155 resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
1156 integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
1157 `,
1158 'package.json': JSON.stringify({
1159 name: 'root',
1160 version: '1.0.0',
1161 dependencies: { mkdirp: '^1.0.2' }, // spec matches
1162 }),
1163 node_modules: {
1164 mkdirp: {
1165 'package.json': JSON.stringify({
1166 name: 'mkdirp',
1167 version: '1.0.4', // version does not!
1168 _resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz',
1169 }),
1170 },
1171 },
1172 })
1173
1174 const arb = new Arborist({ path })
1175 const tree = await arb.loadActual()
1176 tree.meta.commit()
1177 t.matchSnapshot(tree.meta.data, 'lockfile')
1178 t.matchSnapshot(tree.meta.yarnLock.toString(), 'yarn.lock')
1179 })
1180
1181 t.test('a yarn.lock entry with integrity mismatch', async t => {
1182 const path = t.testdir({
1183 'yarn.lock': `
1184 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
1185 # yarn lockfile v1
1186
1187 mkdirp@^1.0.2:
1188 version "1.0.2"
1189 resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
1190 integrity "sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA=="
1191 `,
1192 'package.json': JSON.stringify({
1193 name: 'root',
1194 version: '1.0.0',
1195 dependencies: { mkdirp: '^1.0.2' }, // spec matches
1196 }),
1197 node_modules: {
1198 mkdirp: {
1199 'package.json': JSON.stringify({
1200 name: 'mkdirp',
1201 version: '1.0.2',
1202 _resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.2.tgz',
1203 // integrity mismatch
1204 _integrity: 'um, nope, not even close buddy',
1205 }),
1206 },
1207 },
1208 })
1209
1210 const arb = new Arborist({ path })
1211 const tree = await arb.loadActual()
1212 tree.meta.commit()
1213 t.matchSnapshot(tree.meta.data, 'lockfile')
1214 t.matchSnapshot(tree.meta.yarnLock.toString(), 'yarn.lock')
1215 })
1216
1217 t.test('a yarn.lock entry with no resolved', async t => {
1218 const path = t.testdir({
1219 'yarn.lock': `
1220 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
1221 # yarn lockfile v1
1222
1223 mkdirp@^1.0.2:
1224 version "1.0.2"
1225 integrity "sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA=="
1226 `,
1227 'package.json': JSON.stringify({
1228 name: 'root',
1229 version: '1.0.0',
1230 dependencies: { mkdirp: '^1.0.2' }, // spec matches
1231 }),
1232 node_modules: {
1233 mkdirp: {
1234 'package.json': JSON.stringify({
1235 name: 'mkdirp',
1236 version: '1.0.2',
1237 }),
1238 },
1239 },
1240 })
1241
1242 const arb = new Arborist({ path })
1243 const tree = await arb.loadActual()
1244 tree.meta.commit()
1245 t.matchSnapshot(tree.meta.data, 'lockfile')
1246 t.matchSnapshot(tree.meta.yarnLock.toString(), 'yarn.lock')
1247 })
1248
1249 t.test('a yarn.lock entry with file: resolved', async t => {
1250 const path = t.testdir({
1251 'yarn.lock': `
1252 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
1253 # yarn lockfile v1
1254
1255 mkdirp@file:mkdirp:
1256 version "1.0.2"
1257 integrity "sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA=="
1258 `,
1259 'package.json': JSON.stringify({
1260 name: 'root',
1261 version: '1.0.0',
1262 dependencies: { mkdirp: 'file:mkdirp' }, // spec matches
1263 }),
1264 mkdirp: {
1265 'package.json': JSON.stringify({
1266 name: 'mkdirp',
1267 version: '1.0.2',
1268 }),
1269 },
1270 node_modules: {
1271 mkdirp: t.fixture('symlink', '../mkdirp'),
1272 },
1273 })
1274
1275 const arb = new Arborist({ path })
1276 const tree = await arb.loadActual()
1277 tree.meta.commit()
1278 t.matchSnapshot(tree.meta.data, 'lockfile')
1279 t.matchSnapshot(tree.meta.yarnLock.toString(), 'yarn.lock')
1280 })
1281
1282 t.test('a yarn.lock entry with no integrity', async t => {
1283 const path = t.testdir({
1284 'yarn.lock': `
1285 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
1286 # yarn lockfile v1
1287
1288 mkdirp@^1.0.2:
1289 version "1.0.2"
1290 resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
1291 `,
1292 'package.json': JSON.stringify({
1293 name: 'root',
1294 version: '1.0.0',
1295 dependencies: { mkdirp: '^1.0.2' }, // spec matches
1296 }),
1297 node_modules: {
1298 mkdirp: {
1299 'package.json': JSON.stringify({
1300 name: 'mkdirp',
1301 version: '1.0.2',
1302 }),
1303 },
1304 },
1305 })
1306
1307 const arb = new Arborist({ path })
1308 const tree = await arb.loadActual()
1309 tree.meta.commit()
1310 t.matchSnapshot(tree.meta.data, 'lockfile')
1311 t.matchSnapshot(tree.meta.yarnLock.toString(), 'yarn.lock')
1312 })
1313
1314 t.test('a yarn.lock with no entries', async t => {
1315 const path = t.testdir({
1316 'yarn.lock': `
1317 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
1318 # yarn lockfile v1
1319 `,
1320 'package.json': JSON.stringify({
1321 name: 'root',
1322 version: '1.0.0',
1323 dependencies: { mkdirp: '^1.0.2' }, // spec matches
1324 }),
1325 node_modules: {
1326 mkdirp: {
1327 'package.json': JSON.stringify({
1328 name: 'mkdirp',
1329 version: '1.0.4', // version does not!
1330 _resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz',
1331 }),
1332 },
1333 },
1334 })
1335
1336 const arb = new Arborist({ path })
1337 const tree = await arb.loadActual()
1338 tree.meta.commit()
1339 t.matchSnapshot(tree.meta.data, 'lockfile')
1340 t.matchSnapshot(tree.meta.yarnLock.toString(), 'yarn.lock')
1341 })
1342
1343 // basically just reproduce the loadActual and loadVirtual tests with
1344 // a closer eye on the shrinkwrap results.
1345 t.test('loadActual tests', t => {
1346 const {
1347 fixtures,
1348 roots,
1349 } = require('./fixtures/index.js')
1350
1351 roots.push('tap-with-yarn-lock')
1352
1353 for (const root of roots) {
1354 const path = resolve(fixtures, root)
1355 t.test(root, async t => {
1356 const tree = await new Arborist({ path }).loadActual()
1357 const shrinkwrap = tree.meta.commit()
1358 t.matchSnapshot(shrinkwrap, 'shrinkwrap data')
1359 if (tree.meta.yarnLock) {
1360 const yarnLock = tree.meta.yarnLock.toString()
1361 t.matchSnapshot(yarnLock, 'yarn.lock data')
1362 }
1363 })
1364 }
1365 t.end()
1366 })
1367
1368 t.test('set integrity because location and resolved match', async t => {
1369 const s = new Shrinkwrap({ path: '/some/path' })
1370 s.reset()
1371 s.data = {
1372 packages: {
1373 '': {},
1374 'node_modules/foo': {
1375 resolved: 'https://registry.npmjs.org/foo/foo-1.2.3.tgz',
1376 version: '1.2.3',
1377 integrity: 'sha512-happyhappyjoyjoy',
1378 },
1379 },
1380 }
1381 const root = new Node({
1382 path: '/some/path',
1383 meta: s,
1384 })
1385 const foo = new Node({
1386 parent: root,
1387 name: 'foo',
1388 pkg: {
1389 version: '1.2.3',
1390 _resolved: 'https://registry.npmjs.org/foo/foo-1.2.3.tgz',
1391 },
1392 })
1393 t.equal(foo.integrity, 'sha512-happyhappyjoyjoy')
1394 })
1395
1396 t.test('set integrity because location matches and no resolved', async t => {
1397 const s = new Shrinkwrap({ path: '/some/path' })
1398 s.reset()
1399 s.data = {
1400 packages: {
1401 '': {},
1402 'node_modules/foo': {
1403 resolved: 'https://registry.npmjs.org/foo/foo-1.2.3.tgz',
1404 version: '1.2.3',
1405 integrity: 'sha512-happyhappyjoyjoy',
1406 },
1407 },
1408 }
1409 const root = new Node({
1410 path: '/some/path',
1411 meta: s,
1412 })
1413 const foo = new Node({
1414 parent: root,
1415 name: 'foo',
1416 pkg: {
1417 version: '1.2.3',
1418 },
1419 })
1420 t.equal(foo.resolved, 'https://registry.npmjs.org/foo/foo-1.2.3.tgz')
1421 t.equal(foo.integrity, 'sha512-happyhappyjoyjoy')
1422 })
1423
1424 t.test('set integrity but no resolved', async t => {
1425 const s = new Shrinkwrap({ path: '/some/path' })
1426 s.reset()
1427 s.data = {
1428 packages: {
1429 '': {},
1430 'node_modules/foo': {
1431 version: '1.2.3',
1432 integrity: 'sha512-happyhappyjoyjoy',
1433 },
1434 },
1435 }
1436 const root = new Node({
1437 path: '/some/path',
1438 meta: s,
1439 })
1440 const foo = new Node({
1441 parent: root,
1442 name: 'foo',
1443 pkg: {
1444 version: '1.2.3',
1445 },
1446 })
1447 t.equal(foo.resolved, null)
1448 t.equal(foo.integrity, 'sha512-happyhappyjoyjoy')
1449 })
1450
1451 t.test('get meta from yarn.lock', t => {
1452 const fooEntry = {
1453 integrity: 'sha512-freebeerisworththeprice',
1454 resolved: 'https://example.com/foo.tgz',
1455 version: '1.2.3',
1456 optionalDependencies: {
1457 bar: '2.x',
1458 },
1459 }
1460 const foo2Entry = {
1461 integrity: 'the second coming of fooo',
1462 resolved: 'https://example.com/foo-2.tgz',
1463 version: '2.3.4',
1464 }
1465 const barEntry = {
1466 integrity: 'sha512-integrity is allegiance to your truest self',
1467 resolved: 'file:bar-2.3.4.tgz',
1468 version: '2.3.4',
1469 }
1470 const yarnLock = {
1471 entries: new Map([
1472 ['[email protected]', fooEntry],
1473 ['[email protected]', fooEntry],
1474 ['[email protected]', barEntry],
1475 ['[email protected]', foo2Entry],
1476 ]),
1477 }
1478
1479 const root = '/path/to/root'
1480 const meta = new Shrinkwrap({ path: root })
1481 meta.data = {
1482 lockfileVersion: 2,
1483 packages: {},
1484 dependencies: {},
1485 requires: true,
1486 }
1487 meta.yarnLock = yarnLock
1488
1489 const tree = new Node({
1490 meta,
1491 path: root,
1492 realpath: root,
1493 pkg: {
1494 name: 'root',
1495 version: '4.5.6',
1496 dependencies: {
1497 foo: '1.x',
1498 },
1499 devDependencies: {
1500 bar: '2.x',
1501 },
1502 },
1503 })
1504
1505 const foo = new Node({
1506 name: 'foo',
1507 parent: tree,
1508 pkg: {
1509 name: 'foo',
1510 version: '1.2.3',
1511 optionalDependencies: { bar: '2.x' },
1512 },
1513 })
1514
1515 t.equal(foo.integrity, fooEntry.integrity, 'foo integrity from yarn.lock')
1516 t.equal(foo.resolved, fooEntry.resolved, 'foo resolved from yarn.lock')
1517
1518 const bar = new Node({
1519 name: 'bar',
1520 parent: tree,
1521 pkg: {
1522 name: 'bar',
1523 version: '2.3.4',
1524 },
1525 })
1526 t.equal(bar.integrity, barEntry.integrity, 'bar integrity from yarn.lock')
1527 t.equal(normalizePath(bar.resolved), 'file:/path/to/root/bar-2.3.4.tgz', 'bar resolved from yarn.lock')
1528
1529 bar.parent = null
1530
1531 const barDiffVersion = new Node({
1532 name: 'bar',
1533 parent: tree,
1534 pkg: {
1535 name: 'bar',
1536 description: 'witaf',
1537 version: '2.4.5',
1538 },
1539 })
1540
1541 t.equal(barDiffVersion.integrity, null, 'version mismatch, no integrity')
1542 t.equal(barDiffVersion.resolved, null, 'version mismatch, no resolved')
1543
1544 const barDiffIntegrity = new Node({
1545 integrity: 'sha512-a fundamental lack of commitment to ideals',
1546 name: 'bar',
1547 parent: tree,
1548 pkg: {
1549 name: 'bar',
1550 version: '2.3.4',
1551 },
1552 })
1553 t.equal(barDiffIntegrity.integrity,
1554 'sha512-a fundamental lack of commitment to ideals',
1555 'integrity not updated from yarn lock')
1556 t.equal(barDiffIntegrity.resolved, null, 'integrity mismatch, no resolved')
1557
1558 const barDiffResolved = new Node({
1559 resolved: 'https://x.com/b.tgz',
1560 name: 'bar',
1561 parent: tree,
1562 pkg: {
1563 name: 'bar',
1564 version: '2.3.4',
1565 },
1566 })
1567 t.equal(barDiffResolved.integrity, null, 'integrity not updated from yarn lock')
1568 t.equal(barDiffResolved.resolved, 'https://x.com/b.tgz',
1569 'resolved was not updated from yarn lock')
1570
1571 const barSameIntegrity = new Node({
1572 integrity: barEntry.integrity,
1573 name: 'bar',
1574 parent: tree,
1575 pkg: {
1576 name: 'bar',
1577 version: '2.3.4',
1578 },
1579 })
1580 t.equal(barSameIntegrity.integrity, barEntry.integrity, 'bar integrity still matches')
1581 t.equal(normalizePath(barSameIntegrity.resolved), 'file:/path/to/root/bar-2.3.4.tgz', 'bar resolved from yarn.lock')
1582
1583 const barSameResolved = new Node({
1584 resolved: 'file:/path/to/root/bar-2.3.4.tgz',
1585 name: 'bar',
1586 parent: tree,
1587 pkg: {
1588 name: 'bar',
1589 version: '2.3.4',
1590 },
1591 })
1592 t.equal(barSameResolved.integrity, barEntry.integrity, 'bar integrity from yarn.lock')
1593 t.equal(barSameResolved.resolved, 'file:/path/to/root/bar-2.3.4.tgz', 'bar resolved still matches')
1594
1595 // test that we sometimes might not get the resolved/integrity values
1596 barEntry.resolved = barEntry.integrity = null
1597 bar.package.description = 'new integrity, no resolved'
1598 bar.integrity = 'new integrity'
1599 bar.resolved = null
1600 bar.parent = tree
1601 t.equal(bar.integrity, 'new integrity', 'integrity unchanged by yarn lock')
1602 t.equal(bar.resolved, null, 'resolved set to null, not in yarn entry')
1603
1604 bar.package.description = 'new resolved, no integrity'
1605 bar.parent = null
1606 bar.integrity = null
1607 bar.resolved = 'new resolved'
1608 bar.parent = tree
1609 t.equal(bar.integrity, null, 'integrity set to null, not in yarn entry')
1610 t.equal(bar.resolved, 'new resolved', 'resolved unchanged by yarn lock')
1611
1612 const foo2 = new Node({
1613 name: 'foo',
1614 package: {
1615 name: 'foo',
1616 version: '2.3.4',
1617 },
1618 parent: tree,
1619 })
1620 t.equal(foo2.integrity, null, 'no integrity, entry invalid')
1621 t.equal(foo2.resolved, null, 'no resolved, entry invalid')
1622
1623 t.end()
1624 })
1625
1626 t.test('metadata that only has one of resolved/integrity', t => {
1627 const root = '/path/to/root'
1628 const meta = new Shrinkwrap({ path: root })
1629 meta.data = {
1630 name: 'root',
1631 version: '4.5.6',
1632 lockfileVersion: 2,
1633 packages: {
1634 'node_modules/integrity': {
1635 integrity: 'has integrity no resolved',
1636 },
1637 'node_modules/resolved': {
1638 resolved: 'file:has-resolved-no-integrity.tgz',
1639 },
1640 'node_modules/intalready': {
1641 integrity: 'superceded by node integrity value',
1642 },
1643 'node_modules/resalready': {
1644 resolved: 'superceded by node resolved value',
1645 },
1646 },
1647 dependencies: {},
1648 }
1649
1650 const tree = new Node({
1651 path: root,
1652 realpath: root,
1653 meta,
1654 })
1655 const integrity = new Node({
1656 name: 'integrity',
1657 parent: tree,
1658 })
1659 const resolved = new Node({
1660 name: 'resolved',
1661 parent: tree,
1662 })
1663 const intalready = new Node({
1664 name: 'intalready',
1665 parent: tree,
1666 integrity: 'pre-existing integrity',
1667 })
1668 const resalready = new Node({
1669 name: 'resalready',
1670 parent: tree,
1671 resolved: 'pre-existing resolved',
1672 })
1673
1674 t.equal(integrity.integrity, 'has integrity no resolved', 'integrity only')
1675 t.equal(integrity.resolved, null, 'integrity only')
1676
1677 t.equal(normalizePath(resolved.resolved), 'file:/path/to/root/has-resolved-no-integrity.tgz',
1678 'resolved only')
1679 t.equal(resolved.integrity, null, 'resolved only')
1680
1681 t.equal(intalready.resolved, null, 'integrity only, from node settings')
1682 t.equal(intalready.integrity, 'pre-existing integrity', 'integrity only, from node settings')
1683
1684 t.equal(resalready.resolved, 'pre-existing resolved', 'resolved only, from node settings')
1685 t.equal(resalready.integrity, null, 'resolved only, from node settings')
1686
1687 t.end()
1688 })
1689
1690 t.test('load an ancient lockfile', async t =>
1691 t.match(await Shrinkwrap.load({ path: saxFixture }), {
1692 ancientLockfile: true,
1693 }))
1694
1695 t.test('shrinkwrap where root is a link node', async t => {
1696 const meta = await Shrinkwrap.reset({ path: '/actual/project/path' })
1697 const root = new Link({
1698 path: '/some/link/path',
1699 realpath: '/actual/project/path',
1700 meta,
1701 })
1702
1703 new Node({
1704 root,
1705 path: '/actual/project/path',
1706 pkg: {
1707 name: 'path',
1708 version: '1.2.3',
1709 dependencies: { kid: '' },
1710 },
1711 children: [
1712 { pkg: { name: 'kid', version: '1.2.3' } },
1713 ],
1714 })
1715
1716 t.strictSame(root.meta.commit(), {
1717 lockfileVersion: 3,
1718 requires: true,
1719 packages: {
1720 '': {
1721 version: '1.2.3',
1722 dependencies: {
1723 kid: '',
1724 },
1725 extraneous: true,
1726 },
1727 'node_modules/kid': {
1728 version: '1.2.3',
1729 extraneous: true,
1730 },
1731 },
1732 name: 'path',
1733 version: '1.2.3',
1734 extraneous: true,
1735 })
1736 })
1737
1738 t.test('prioritize npm-shrinkwrap.json over package-lock.json', async t => {
1739 const path = t.testdir({
1740 'npm-shrinkwrap.json': '{}',
1741 'package-lock.json': '{}',
1742 'package.json': '{}',
1743 })
1744 const sw = await Shrinkwrap.load({ path })
1745 t.equal(sw.type, 'npm-shrinkwrap.json')
1746 })
1747
1748 t.test('do not add metadata if versions mismatch', async t => {
1749 const meta = new Shrinkwrap({ path: '/home/user/projects/root' })
1750 // fake load
1751 meta.data = {
1752 lockfileVersion: 2,
1753 requires: true,
1754 dependencies: {},
1755 packages: {},
1756 }
1757
1758 const root = new Node({
1759 path: '/home/usr/projects/root',
1760 meta,
1761 pkg: {
1762 name: 'root',
1763 version: '1.2.3',
1764 dependencies: {
1765 foo: '1',
1766 },
1767 },
1768 children: [
1769 {
1770 name: 'foo',
1771 pkg: {
1772 name: 'foo',
1773 version: '1.0.0',
1774 },
1775 resolved: 'https://registry.npmjs.org/foo-1.0.0.tgz',
1776 integrity: 'sha512-this is no sha of mine',
1777 },
1778 ],
1779 })
1780 const oldFoo = root.children.get('foo')
1781 const newFoo = new Node({
1782 path: oldFoo.path,
1783 root,
1784 pkg: {
1785 name: 'foo',
1786 version: '1.2.3',
1787 },
1788 })
1789 t.equal(newFoo.resolved, null)
1790 t.equal(newFoo.integrity, null)
1791 })
1792
1793 t.test('setting lockfileVersion from the file contents', async t => {
1794 const path = t.testdir({
1795 v1: {
1796 'package-lock.json': JSON.stringify({ lockfileVersion: 1 }),
1797 },
1798 v2: {
1799 'package-lock.json': JSON.stringify({ lockfileVersion: 2 }),
1800 },
1801 v3: {
1802 'package-lock.json': JSON.stringify({ lockfileVersion: 3 }),
1803 },
1804 })
1805
1806 const loadAndReset = (options) => Promise.all([
1807 Shrinkwrap.load(options).then((s) => s.lockfileVersion),
1808 Shrinkwrap.reset(options).then((s) => s.lockfileVersion),
1809 ])
1810
1811 t.test('default setting', async t => {
1812 const s1 = await loadAndReset({ path: `${path}/v1` })
1813 t.strictSame(s1, [3, null], 'will upgrade old lockfile')
1814 const s2 = await loadAndReset({ path: `${path}/v2` })
1815 t.strictSame(s2, [2, null], 'will keep v2 as v2')
1816 const s3 = await loadAndReset({ path: `${path}/v3` })
1817 t.strictSame(s3, [3, null], 'load will keep v3 as v3')
1818 })
1819 t.test('v1', async t => {
1820 const s1 = await loadAndReset({ path: `${path}/v1`, lockfileVersion: 1 })
1821 t.strictSame(s1, [1, 1], 'keep explicit v1 setting')
1822 const s2 = await loadAndReset({ path: `${path}/v2`, lockfileVersion: 1 })
1823 t.strictSame(s2, [1, 1], 'downgrade explicitly')
1824 const s3 = await loadAndReset({ path: `${path}/v3`, lockfileVersion: 1 })
1825 t.strictSame(s3, [1, 1], 'downgrade explicitly')
1826 })
1827 t.test('v2', async t => {
1828 const s1 = await loadAndReset({ path: `${path}/v1`, lockfileVersion: 2 })
1829 t.strictSame(s1, [2, 2], 'upgrade explicitly')
1830 const s2 = await loadAndReset({ path: `${path}/v2`, lockfileVersion: 2 })
1831 t.strictSame(s2, [2, 2], 'keep v2 setting')
1832 const s3 = await loadAndReset({ path: `${path}/v3`, lockfileVersion: 2 })
1833 t.strictSame(s3, [2, 2], 'downgrade explicitly')
1834 })
1835 t.test('v3', async t => {
1836 const s1 = await loadAndReset({ path: `${path}/v1`, lockfileVersion: 3 })
1837 t.strictSame(s1, [3, 3], 'upgrade explicitly')
1838 const s2 = await loadAndReset({ path: `${path}/v2`, lockfileVersion: 3 })
1839 t.strictSame(s2, [3, 3], 'upgrade explicitly')
1840 const s3 = await loadAndReset({ path: `${path}/v3`, lockfileVersion: 3 })
1841 t.strictSame(s3, [3, 3], 'keep v3 setting')
1842 })
1843
1844 t.equal(Shrinkwrap.defaultLockfileVersion, 3, 'default is 3')
1845
1846 t.test('load should return error correctly when it cant access folder',
1847 { skip: process.platform === 'win32' ? 'skip chmod in windows' : false },
1848 async t => {
1849 const dir = t.testdir({})
1850 try {
1851 fs.chmodSync(dir, '000')
1852 const res = await Shrinkwrap.load({ path: dir })
1853 t.ok(res.loadingError, 'loading error should exist')
1854 t.strictSame(res.loadingError.errno, -13)
1855 t.strictSame(res.loadingError.code, 'EACCES')
1856 } finally {
1857 fs.chmodSync(dir, '666')
1858 }
1859 })
1860 })