| 1 |
const YarnLock = require('../lib/yarn-lock.js') |
| 2 |
const Arborist = require('../lib/arborist') |
| 3 |
const Node = require('../lib/node.js') |
| 4 |
|
| 5 |
const t = require('tap') |
| 6 |
const { resolve, basename } = require('node:path') |
| 7 |
const fixtures = [ |
| 8 |
resolve(__dirname, 'fixtures/tap-with-yarn-lock'), |
| 9 |
resolve(__dirname, 'fixtures/yarn-stuff'), |
| 10 |
] |
| 11 |
const { readFileSync } = require('node:fs') |
| 12 |
|
| 13 |
fixtures.forEach(f => t.test(basename(f), t => { |
| 14 |
const lockdata = readFileSync(f + '/yarn.lock') |
| 15 |
const yarnLock = new YarnLock() |
| 16 |
// parse the data |
| 17 |
yarnLock.parse(lockdata) |
| 18 |
// then turn it into output |
| 19 |
const lockOutput = yarnLock.toString() |
| 20 |
// snapshot the result |
| 21 |
t.matchSnapshot(lockOutput, 'generated output from input') |
| 22 |
const yarnLock2 = new YarnLock() |
| 23 |
yarnLock2.parse(lockOutput) |
| 24 |
t.strictSame(yarnLock2, yarnLock, 'same parsed result from string output') |
| 25 |
t.end() |
| 26 |
})) |
| 27 |
|
| 28 |
t.test('invalid yarn lockfile data throws', t => { |
| 29 |
t.throws(() => YarnLock.parse(` |
| 30 |
asdf@foo: |
| 31 |
this !is not vlid |
| 32 |
i mean |
| 33 |
what even is it?? |
| 34 |
not yarn lock, that's for sure |
| 35 |
{"maybe":"json"}? |
| 36 |
- or: even |
| 37 |
- yaml? |
| 38 |
- NO |
| 39 |
`), { content: ' this !is not vlid\n', line: 3, position: 11 }, 'just garbage') |
| 40 |
|
| 41 |
t.throws(() => YarnLock.parse(` |
| 42 |
asdf@foo: |
| 43 |
dependencies: |
| 44 |
foo bar baz blork |
| 45 |
`), { content: ' foo bar baz blork\n', line: 4 }, 'invalid subkey') |
| 46 |
|
| 47 |
t.end() |
| 48 |
}) |
| 49 |
|
| 50 |
t.test('omits empty dependency list on toString output', t => { |
| 51 |
const y = new YarnLock() |
| 52 |
y.parse(` |
| 53 |
foo@bar: |
| 54 |
version "1.2.3" |
| 55 |
resolved "https://registry.local/foo/-/foo-1.2.3.tgz" |
| 56 |
dependencies: |
| 57 |
|
| 58 |
# Note: do not require a \\n at the end of the file, just add it if missing |
| 59 |
# Also: comments are not preserved. |
| 60 |
|
| 61 |
bar@foo: |
| 62 |
version "1.2.3"`) |
| 63 |
t.equal(y.toString(), `# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. |
| 64 |
# yarn lockfile v1 |
| 65 |
|
| 66 |
|
| 67 |
bar@foo: |
| 68 |
version "1.2.3" |
| 69 |
|
| 70 |
foo@bar: |
| 71 |
version "1.2.3" |
| 72 |
resolved "https://registry.local/foo/-/foo-1.2.3.tgz" |
| 73 |
`) |
| 74 |
t.end() |
| 75 |
}) |
| 76 |
|
| 77 |
t.test('exports YarnLockEntry class', t => { |
| 78 |
t.type(YarnLock.Entry, 'function') |
| 79 |
t.end() |
| 80 |
}) |
| 81 |
|
| 82 |
t.test('load a yarn lock from an actual tree', t => { |
| 83 |
// make sure the symlinks are loaded |
| 84 |
require('./fixtures/index.js') |
| 85 |
const fixtures = [ |
| 86 |
resolve(__dirname, 'fixtures/install-types'), |
| 87 |
resolve(__dirname, 'fixtures/links-all-over'), |
| 88 |
] |
| 89 |
for (const fixture of fixtures) { |
| 90 |
t.test(basename(fixture), async t => { |
| 91 |
const tree = await new Arborist({ path: fixture }).loadActual() |
| 92 |
const y = YarnLock.fromTree(tree) |
| 93 |
t.matchSnapshot(y.toString(), 'yarn.lock from a package tree') |
| 94 |
}) |
| 95 |
} |
| 96 |
t.end() |
| 97 |
}) |
| 98 |
|
| 99 |
t.test('yarn lock with dedupes yarn wouldnt do', async t => { |
| 100 |
const tree = new Node({ |
| 101 |
path: '/my/project', |
| 102 |
pkg: { dependencies: { x: '1.x', y: '1.x', z: '1.x' } }, |
| 103 |
children: [ |
| 104 |
{ pkg: { name: 'x', version: '1.2.0' } }, |
| 105 |
{ pkg: { name: 'y', version: '1.0.0', dependencies: { x: '1.1', z: '2.x' } }, |
| 106 |
children: [ |
| 107 |
{ pkg: { name: 'x', version: '1.1.0' } }, |
| 108 |
{ pkg: { name: 'z', version: '2.0.0', dependencies: { x: '1.x' } } }, |
| 109 |
// note: yarn nests another [email protected] here, because it locks |
| 110 |
// the 1.x resolution to 1.2.0 even when unnecessary |
| 111 |
], |
| 112 |
}, |
| 113 |
{ pkg: { name: 'z', version: '1.0.0' } }, |
| 114 |
], |
| 115 |
}) |
| 116 |
|
| 117 |
const y = YarnLock.fromTree(tree) |
| 118 |
t.matchSnapshot(y.toString(), 'yarn.lock from deduped tree') |
| 119 |
}) |
| 120 |
|
| 121 |
t.test('deduped prior entries that do not match one another', async t => { |
| 122 |
const tree = new Node({ |
| 123 |
path: '/my/project', |
| 124 |
pkg: { dependencies: { a: '', b: '' } }, |
| 125 |
children: [ |
| 126 |
{ pkg: { name: 'a', dependencies: { i: '', x: '1.x', y: '1.x', z: '1.x' } }, |
| 127 |
children: [ |
| 128 |
{ pkg: { name: 'i', version: '1.0.0', dependencies: { x: '1.2.0' } } }, |
| 129 |
{ pkg: { name: 'x', version: '1.2.0' }, integrity: 'x120' }, |
| 130 |
{ pkg: { name: 'y', version: '1.0.0', dependencies: { x: '1.1', z: '2.x' } }, |
| 131 |
children: [ |
| 132 |
{ pkg: { name: 'x', version: '1.1.0' } }, |
| 133 |
{ pkg: { name: 'z', version: '2.0.0', dependencies: { x: '1.x' } } }, |
| 134 |
], |
| 135 |
}, |
| 136 |
{ pkg: { name: 'z', version: '1.0.0' } }, |
| 137 |
], |
| 138 |
}, |
| 139 |
{ pkg: { name: 'b', dependencies: { j: '', x: '1.x', y: '1.x', z: '1.x' } }, |
| 140 |
children: [ |
| 141 |
{ pkg: { name: 'j', version: '1.0.0', dependencies: { x: '1.3.0' } } }, |
| 142 |
{ pkg: { name: 'x', version: '1.3.0' }, integrity: 'x130' }, |
| 143 |
{ pkg: { name: 'y', version: '1.0.0', dependencies: { x: '1.1', z: '2.x' } }, |
| 144 |
children: [ |
| 145 |
{ pkg: { name: 'x', version: '1.1.0' } }, |
| 146 |
{ pkg: { name: 'z', version: '2.0.0', dependencies: { x: '1.x' } } }, |
| 147 |
], |
| 148 |
}, |
| 149 |
{ pkg: { name: 'z', version: '1.0.0' } }, |
| 150 |
], |
| 151 |
}, |
| 152 |
], |
| 153 |
}) |
| 154 |
|
| 155 |
const y = YarnLock.fromTree(tree) |
| 156 |
t.matchSnapshot(y.toString(), 'yarn.lock with mismatching previous resolutions') |
| 157 |
}) |
| 158 |
|
| 159 |
t.test('more nesting tree complications', async t => { |
| 160 |
const tree = new Node({ |
| 161 |
path: '/my/project', |
| 162 |
pkg: { dependencies: { a: '', b: '', c: '', c2: '', d3: '' } }, |
| 163 |
children: [ |
| 164 |
{ pkg: { name: 'a', dependencies: { x: '', g: '', h: '', i: '', j: '', k: '' } }, |
| 165 |
children: [ |
| 166 |
{ pkg: { name: 'x', version: '1.0.0' }, resolved: 'https://x100.xyz' }, |
| 167 |
{ pkg: { name: 'g', version: '1.0.0', dependencies: { x: '^1.0.0' } } }, |
| 168 |
{ pkg: { name: 'h', version: '1.0.0', dependencies: { x: '1.0.0' } } }, |
| 169 |
{ pkg: { name: 'i', version: '1.0.0', dependencies: { x: '1.0' } } }, |
| 170 |
{ pkg: { name: 'j', version: '1.0.0', dependencies: { x: '1' } } }, |
| 171 |
{ pkg: { name: 'k', version: '1.0.0', dependencies: { x: '' } } }, |
| 172 |
], |
| 173 |
}, |
| 174 |
// previously-seen specs that don't match |
| 175 |
{ pkg: { name: 'b', dependencies: { x: '', l: '', m: '', n: '', n2: '', o: '', p: '' } }, |
| 176 |
children: [ |
| 177 |
{ pkg: { name: 'x', version: '1.0.1' } }, |
| 178 |
{ pkg: { name: 'l', version: '1.0.0', dependencies: { x: '^1.0.1' } } }, |
| 179 |
{ pkg: { name: 'm', version: '1.0.0', dependencies: { x: '1.0.1' } } }, |
| 180 |
{ pkg: { name: 'n', version: '1.0.0', dependencies: { x: '1.0' } } }, |
| 181 |
{ pkg: { name: 'n2', version: '1.0.0', dependencies: { x: '>=1.0' } } }, |
| 182 |
{ pkg: { name: 'o', version: '1.0.0', dependencies: { x: '1' } } }, |
| 183 |
{ pkg: { name: 'p', version: '1.0.0', dependencies: { x: '' } } }, |
| 184 |
], |
| 185 |
}, |
| 186 |
// new specs that get added right away to the entry |
| 187 |
{ pkg: { name: 'c', dependencies: { d: '', e: '', f: '' } }, |
| 188 |
children: [ |
| 189 |
{ pkg: { name: 'x', version: '1.0.1' }, integrity: 'x101', resolved: 'https://x101.xyz' }, |
| 190 |
{ pkg: { name: 'd', version: '1.0.0', dependencies: { x: '^1.0.1' } } }, |
| 191 |
{ pkg: { name: 'e', version: '1.0.0', dependencies: { x: '>=1.0.1 <2' } } }, |
| 192 |
{ pkg: { name: 'f', version: '1.0.0', dependencies: { x: '>=1.0' } } }, |
| 193 |
], |
| 194 |
}, |
| 195 |
// new specs that later match |
| 196 |
{ pkg: { name: 'c2', dependencies: { d2: '', e2: '', f2: '' } }, |
| 197 |
children: [ |
| 198 |
{ pkg: { name: 'x', version: '1.0.1' }, resolved: 'https://x101.xyz' }, |
| 199 |
{ pkg: { name: 'd2', version: '1.0.0', dependencies: { x: '^1.0.0-x' } } }, |
| 200 |
{ pkg: { name: 'e2', version: '1.0.0', dependencies: { x: '>=1.0.0' } } }, |
| 201 |
{ pkg: { name: 'f2', version: '1.0.0', dependencies: { x: '1.0.1' } } }, |
| 202 |
], |
| 203 |
}, |
| 204 |
// no version, resolved, or integrity, we assume a match |
| 205 |
{ pkg: { name: 'd3', dependencies: { x: '' } }, |
| 206 |
children: [{ pkg: { name: 'x' } }], |
| 207 |
}, |
| 208 |
], |
| 209 |
}) |
| 210 |
|
| 211 |
const y = YarnLock.fromTree(tree) |
| 212 |
t.matchSnapshot(y.toString(), 'yarn.lock with mismatching previous resolutions') |
| 213 |
}) |