summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/shrinkwrap-extra-metadata.js
blob: c5f60e4c221f004bce06bca6b1707a3559c2d731 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'

const common = require('../common-tap.js')
const fs = require('fs')
const mkdirp = require('mkdirp')
const mr = require('npm-registry-mock')
const npm = require('../../lib/npm.js')
const osenv = require('osenv')
const path = require('path')
const pkgSri = require('../../lib/utils/package-integrity.js')
const rimraf = require('rimraf')
const test = require('tap').test

const pkg = path.join(__dirname, path.basename(__filename, '.js'))

const EXEC_OPTS = {
  cwd: pkg }

const json = {
  author: 'Rockbert',
  name: 'shrinkwrap-extra-metadata',
  version: '0.0.0'
}

test('setup', function (t) {
  cleanup()
  mkdirp.sync(pkg)
  fs.writeFileSync(
    path.join(pkg, 'package.json'),
    JSON.stringify(json, null, 2)
  )

  process.chdir(pkg)
  t.end()
})

test('adds additional metadata fields from the pkglock spec', function (t) {
  mr({ port: common.port }, function (er, s) {
    common.npm(
      [
        '--registry', common.registry,
        '--loglevel', 'silent',
        'shrinkwrap'
      ],
      { cwd: pkg, env: { NODE_PRESERVE_SYMLINKS: 'foo' } },
      function (err, code, stdout, stderr) {
        t.ifError(err, 'shrinkwrap ran without issue')
        t.notOk(code, 'shrinkwrap ran without raising error code')

        fs.readFile(path.resolve(pkg, 'npm-shrinkwrap.json'), function (err, desired) {
          t.ifError(err, 'read npm-shrinkwrap.json without issue')
          t.same(
            {
              'name': 'shrinkwrap-extra-metadata',
              'version': '0.0.0',
              'lockfileVersion': npm.lockfileVersion,
              'packageIntegrity': pkgSri.hash(json),
              'preserveSymlinks': 'foo'
            },
            JSON.parse(desired),
            'shrinkwrap wrote the expected metadata fields'
          )

          s.close()
          t.end()
        })
      }
    )
  })
})

test('cleanup', function (t) {
  cleanup()

  t.end()
})

function cleanup () {
  process.chdir(osenv.tmpdir())
  rimraf.sync(pkg)
}