summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/shrinkwrap-package-integrity.js
blob: 6333757d7f3a3c51b3530dc06bba8029eb5e2ef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict'

const pkgsri = require('../../lib/utils/package-integrity.js')
const ssri = require('ssri')
const test = require('tap').test

test('generates integrity according to spec', (t) => {
  const pkgJson = {
    'name': 'foo',
    'version': '1.0.0',
    'dependencies': {
      'x': '1.0.0'
    },
    'devDependencies': {
      'y': '1.0.0'
    },
    'optionalDependencies': {
      'z': '1.0.0'
    }
  }
  const integrity = pkgsri.hash(pkgJson)
  t.ok(integrity && integrity.toString(), 'hash returned')
  t.equal(
    ssri.parse(integrity).toString(),
    integrity,
    'hash is a valid ssri string'
  )
  t.ok(pkgsri.check(pkgJson, integrity), 'same-data integrity check succeeds')
  t.done()
})

test('updates if anything changes in package.json', (t) => {
  const pkgJson = {
    'name': 'foo',
    'version': '1.0.0',
    'dependencies': {
      'x': '1.0.0'
    },
    'devDependencies': {
      'y': '1.0.0'
    },
    'optionalDependencies': {
      'z': '1.0.0'
    }
  }
  const sri = pkgsri.hash(pkgJson)
  pkgJson.version = '1.2.3'
  t.equal(pkgsri.check(pkgJson, sri), false, 'no match after pkgJson change')
  t.done()
})