Debian Codegraph @

workspaces/arborist/test/spec-from-lock.js

raw
1 const sfl = require('../lib/spec-from-lock.js')
2 const t = require('tap')
3 const cwd = process.cwd()
4 const dirname = require('node:path').dirname(cwd)
5
6 const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')
7 const isHGIFn = (key, val) =>
8 typeof val === 'function' &&
9 (/template$/.test(key) || ['extract', 'hashformat'].includes(key))
10
11 const normalizePaths = obj => {
12 for (const key in obj) {
13 if (['where', 'fetchSpec', 'saveSpec'].includes(key) && obj[key]) {
14 obj[key] = normalizePath(obj[key])
15 } else if (isHGIFn(key, obj[key])) {
16 obj[key] = `function ${key}`
17 } else if (typeof obj[key] === 'object' && obj[key] !== null) {
18 obj[key] = normalizePaths(obj[key])
19 }
20 }
21 return obj
22 }
23
24 t.formatSnapshot = obj => normalizePaths(obj)
25 t.cleanSnapshot = s => s
26 .split(/\s+"pathmatch": .*/g).join('\n{pathmatch regexp},')
27 .split(normalizePath(cwd)).join('{CWD}')
28 .split(normalizePath(dirname)).join('{..}')
29
30 t.matchSnapshot(sfl('x', {
31 version: '1.2.3',
32 integrity: 'integral',
33 }), 'version and integrity')
34
35 t.matchSnapshot(sfl('gitthing', {
36 version: 'git+ssh://[email protected]/isaacs/abbrev-js#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb',
37 from: 'github:isaacs/abbrev-js#some-ref',
38 }), 'git repo with resolved value')
39
40 t.matchSnapshot(sfl('legacy', {
41 from: '1.2.3',
42 }), 'legacy metadata with "from" and no integrity')
43
44 t.matchSnapshot(sfl('x', {
45 version: 'foo.tgz',
46 integrity: 'integral',
47 }), 'version (file) and integrity set')
48
49 t.matchSnapshot(sfl('x', {
50 version: '1.2.3',
51 from: '^1.2.0',
52 shasum: 'deadbeef0cafebad',
53 resolved: 'https://registry.npmjs.org/x/-/x-1.2.3.tgz',
54 }), 'version and range, no integrity')
55
56 t.matchSnapshot(sfl('x', {
57 version: 'file:x-1.2.3.tgz',
58 from: 'x-1.2.3.tgz',
59 }), 'file with from, no integrity')
60
61 t.matchSnapshot(sfl('x', {
62 version: 'file:x-1.2.3.tgz',
63 resolved: '/path/to/x-1.2.3.tgz',
64 }), 'file with resolved, no integrity')
65
66 t.matchSnapshot(sfl('x', {
67 version: 'file:x-1.2.3.tgz',
68 from: 'file:x-1.2.3.tgz',
69 resolved: '/path/to/x-1.2.3.tgz',
70 }), 'file with resolved and from')
71
72 t.matchSnapshot(sfl('x', {
73 version: 'file:../some/path',
74 }), 'directory symlink')
75
76 t.matchSnapshot(sfl('really bad and invalid', {
77 version: 'url:// not even close to a ! valid @ npm @ specifier',
78 resolved: 'this: is: also: not: valid!',
79 }), 'completely invalid, return empty object')