Skip to content

Commit 379485e

Browse files
committed
test: move sample lockfiles into fixtures/ directory + tidy lockfiles tests
1 parent 6ec62ac commit 379485e

9 files changed

Lines changed: 174 additions & 139 deletions

File tree

File renamed without changes.

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,6 @@ if (import.meta.main) {
951951
run()
952952
}
953953

954-
export { run }
955954
export type { VersionsSet }
956955
export {
957956
diffDependencySets,
@@ -963,6 +962,7 @@ export {
963962
parsePnpmLock,
964963
parseYarnBerryLock,
965964
parseYarnV1Lock,
965+
run,
966966
yarnBerrySpecifierToName,
967967
yarnV1SpecifierToName,
968968
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"keywords": [],
1010
"main": "lib/index.ts",
1111
"scripts": {
12-
"test": "node --test",
12+
"test": "node --test --experimental-test-coverage",
1313
"typecheck": "tsc --noEmit",
1414
"lint": "eslint .",
1515
"prepare": "simple-git-hooks",

test/fixtures/npm/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/pnpm/pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/yarn-berry/yarn.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 8
6+
cacheKey: 10c0
7+
8+
"lodash@npm:^4.17.21":
9+
version: 4.17.21
10+
resolution: "lodash@npm:4.17.21"
11+
12+
"@scope/name@npm:^2.3.4":
13+
version: 2.3.4
14+
15+

test/fixtures/yarn-v1/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
lodash@^4.17.21:
6+
version "4.17.21"
7+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz"
8+
9+
"@scope/name@^2.3.4":
10+
version "2.3.4"
11+
12+

test/lockfiles.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import type { VersionsSet } from '../lib/index.ts'
2+
import { strict as assert } from 'node:assert'
3+
import { readFileSync } from 'node:fs'
4+
import { test } from 'node:test'
5+
import { diffDependencySets, findLockfileLine, parseLockfile, parseNpmLock, parsePnpmLock, parseYarnBerryLock, parseYarnV1Lock, yarnBerrySpecifierToName, yarnV1SpecifierToName } from '../lib/index.ts'
6+
7+
function toSorted(obj: VersionsSet): Record<string, string[]> {
8+
const out: Record<string, string[]> = {}
9+
for (const [k, v] of obj) out[k] = Array.from(v).sort()
10+
return Object.fromEntries(Object.entries(out).sort(([a], [b]) => a.localeCompare(b)))
11+
}
12+
13+
const packageLockJson = readFileSync(new URL('./fixtures/npm/package-lock.json', import.meta.url), 'utf8')
14+
const pnpmLock = readFileSync(new URL('./fixtures/pnpm/pnpm-lock.yaml', import.meta.url), 'utf8')
15+
const yarnV1Lock = readFileSync(new URL('./fixtures/yarn-v1/yarn.lock', import.meta.url), 'utf8')
16+
const yarnBerryLock = readFileSync(new URL('./fixtures/yarn-berry/yarn.lock', import.meta.url), 'utf8')
17+
18+
test('parseLockfile', async (t) => {
19+
await t.test('dispatches npm', () => {
20+
const npm = parseLockfile('package-lock.json', '{"packages":{}}')
21+
assert.ok(npm instanceof Map)
22+
})
23+
assert.deepEqual(toSorted(parseNpmLock(packageLockJson)), {
24+
'@scope/name': ['2.3.4'],
25+
'lodash': ['4.17.21'],
26+
})
27+
28+
await t.test('dispatches pnpm', () => {
29+
const pnpm = parseLockfile('pnpm-lock.yaml', `packages:\n`)
30+
assert.ok(pnpm instanceof Map)
31+
})
32+
assert.deepEqual(toSorted(parsePnpmLock(pnpmLock)), {
33+
'@scope/name': ['2.3.4'],
34+
'lodash': ['4.17.21'],
35+
})
36+
37+
await t.test('dispatches yarn v1', () => {
38+
const yarnV1 = parseLockfile('yarn.lock', `pkg@^1:\n version "1.0.0"`)
39+
assert.ok(yarnV1 instanceof Map)
40+
})
41+
assert.deepEqual(toSorted(parseYarnV1Lock(yarnV1Lock)), {
42+
'@scope/name': ['2.3.4'],
43+
'lodash': ['4.17.21'],
44+
})
45+
46+
await t.test('dispatches yarn berry', () => {
47+
const yarnBerry = parseLockfile('yarn.lock', `"pkg@npm:^1":\n version: 1.0.0`)
48+
assert.ok(yarnBerry instanceof Map)
49+
})
50+
assert.deepEqual(toSorted(parseYarnBerryLock(yarnBerryLock)), {
51+
'@scope/name': ['2.3.4'],
52+
'lodash': ['4.17.21'],
53+
})
54+
})
55+
56+
test('yarnV1SpecifierToName', () => {
57+
assert.equal(yarnV1SpecifierToName('lodash@^4.17.21'), 'lodash')
58+
assert.equal(yarnV1SpecifierToName('@scope/name@^1.0.0'), '@scope/name')
59+
assert.equal(yarnV1SpecifierToName('name@npm:^1.0.0'), 'name')
60+
})
61+
62+
test('yarnBerrySpecifierToName', () => {
63+
assert.equal(yarnBerrySpecifierToName('lodash@npm:^4.17.21'), 'lodash')
64+
assert.equal(yarnBerrySpecifierToName('@scope/name@npm:^1.0.0'), '@scope/name')
65+
assert.equal(yarnBerrySpecifierToName('name@npm:^1.0.0'), 'name')
66+
})
67+
68+
test('diffDependencySets', () => {
69+
const a: VersionsSet = new Map([['lodash', new Set(['4.17.21'])]])
70+
const b: VersionsSet = new Map([['lodash', new Set(['4.17.21', '4.17.22'])]])
71+
const diff = diffDependencySets(a, b)
72+
assert.equal(diff.length, 1)
73+
assert.equal(diff[0].name, 'lodash')
74+
})
75+
76+
test('findLockfileLine', async (t) => {
77+
await t.test('finds in npm lockfile', () => {
78+
assert.ok(
79+
findLockfileLine('package-lock.json', packageLockJson, 'lodash', '4.17.21'),
80+
)
81+
})
82+
await t.test('finds in pnpm lockfile', () => {
83+
assert.ok(
84+
findLockfileLine('pnpm-lock.yaml', pnpmLock, 'lodash', '4.17.21'),
85+
)
86+
})
87+
await t.test('finds in yarn v1 lockfile', () => {
88+
assert.ok(
89+
findLockfileLine('yarn.lock', yarnV1Lock, 'lodash', '4.17.21'),
90+
)
91+
})
92+
await t.test('finds in yarn berry lockfile', () => {
93+
assert.ok(
94+
findLockfileLine('yarn.lock', yarnBerryLock, 'lodash', '4.17.21'),
95+
)
96+
})
97+
98+
await t.test('pnpm tolerates peer suffix and quotes', () => {
99+
const pnpm = `packages:\n\n "/name@1.0.0(peer@x)":\n resolution: {integrity: sha512-...}\n`
100+
const line = findLockfileLine('pnpm-lock.yaml', pnpm, 'name', '1.0.0')
101+
assert.ok(line && line >= 1)
102+
})
103+
104+
await t.test('yarn v1 matches within multi-spec header', () => {
105+
const y1 = `# yarn lockfile v1\nname@^1.0.0, name@~1.0.0:\n version "1.0.1"\n`
106+
const line = findLockfileLine('yarn.lock', y1, 'name', '1.0.1')
107+
assert.equal(line, 3)
108+
})
109+
110+
await t.test('yarn berry parses version in multiple formats', () => {
111+
const yb = `"name@npm:^1":\n version: 1.2.3\n\n"name@npm:^2":\n version: '2.3.4'\n\n"name@npm:^3":\n version: 3.2.1 # comment\n`
112+
assert.equal(findLockfileLine('yarn.lock', yb, 'name', '1.2.3'), 2)
113+
assert.equal(findLockfileLine('yarn.lock', yb, 'name', '2.3.4'), 5)
114+
assert.equal(findLockfileLine('yarn.lock', yb, 'name', '3.2.1'), 8)
115+
})
116+
})

test/parsers.test.ts

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)