|
| 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 | +}) |
0 commit comments