summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/format-package-lock.js
blob: ddf40915d9bd3f6a39393ed7f7b48b3d82e04a5a (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict'
const fs = require('fs')
const path = require('path')
const test = require('tap').test
const mr = require('npm-registry-mock')
const Tacks = require('tacks')
const File = Tacks.File
const Dir = Tacks.Dir
const common = require('../common-tap.js')

const basedir = common.pkg
const testdir = path.join(basedir, 'testdir')
const cachedir = common.cache
const globaldir = path.join(basedir, 'global')
const tmpdir = path.join(basedir, 'tmp')

const pkgPath = path.join(testdir, 'package.json')
const pkgLockPath = path.join(testdir, 'package-lock.json')
const shrinkwrapPath = path.join(testdir, 'npm-shrinkwrap.json')
const CRLFreg = /\r\n|\r|\n/

const env = common.newEnv().extend({
  npm_config_cache: cachedir,
  npm_config_tmp: tmpdir,
  npm_config_prefix: globaldir,
  npm_config_registry: common.registry,
  npm_config_loglevel: 'warn',
  npm_config_format_package_lock: false
})

var server
var fixture = new Tacks(Dir({
  cache: Dir(),
  global: Dir(),
  tmp: Dir(),
  testdir: Dir({
    'package.json': File({
      name: 'install-package-lock-only',
      version: '1.0.0',
      dependencies: {
        mkdirp: '^0.3.4'
      }
    })
  })
}))

function setup () {
  cleanup()
  fixture.create(basedir)
}

function cleanup () {
  fixture.remove(basedir)
}

test('setup', function (t) {
  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
    if (err) throw err
    server = s
    t.done()
  })
})

test('package-lock.json unformatted, package.json formatted when config has `format-package-lock: false`', function (t) {
  setup()
  common.npm(['install'], {cwd: testdir, env}).spread((code, stdout, stderr) => {
    t.is(code, 0, 'ok')
    t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
    const pkgLockUtf8 = fs.readFileSync(pkgLockPath, 'utf-8')
    t.equal(pkgLockUtf8.split(CRLFreg).length, 2, 'package-lock.json is unformatted')
    const pkgUtf8 = fs.readFileSync(pkgPath, 'utf-8')
    t.notEqual(pkgUtf8.split(CRLFreg).length, 2, 'package.json is formatted')
    t.done()
  })
})

test('npm-shrinkwrap.json unformatted when config has `format-package-lock: false`', function (t) {
  setup()
  common.npm(['shrinkwrap'], {cwd: testdir, env}).spread((code, stdout, stderr) => {
    t.is(code, 0, 'ok')
    t.ok(fs.existsSync(shrinkwrapPath), 'ensure that npm-shrinkwrap.json was created')
    const shrinkwrapUtf8 = fs.readFileSync(shrinkwrapPath, 'utf-8')
    t.equal(shrinkwrapUtf8.split(CRLFreg).length, 2, 'npm-shrinkwrap.json is unformatted')
    t.done()
  })
})

test('package-lock.json and package.json formatted when config has `format-package-lock: true`', function (t) {
  setup()
  common.npm(['install'], {cwd: testdir}).spread((code, stdout, stderr) => {
    t.is(code, 0, 'ok')
    t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
    const pkgLockUtf8 = fs.readFileSync(pkgLockPath, 'utf-8')
    t.notEqual(pkgLockUtf8.split(CRLFreg).length, 2, 'package-lock.json is formatted')
    const pkgUtf8 = fs.readFileSync(pkgPath, 'utf-8')
    t.notEqual(pkgUtf8.split(CRLFreg).length, 2, 'package.json is formatted')
    t.done()
  })
})

test('npm-shrinkwrap.json formatted when config has `format-package-lock: true`', function (t) {
  setup()
  common.npm(['shrinkwrap'], {cwd: testdir}).spread((code, stdout, stderr) => {
    t.is(code, 0, 'ok')
    t.ok(fs.existsSync(shrinkwrapPath), 'ensure that npm-shrinkwrap.json was created')
    const shrinkwrapUtf8 = fs.readFileSync(shrinkwrapPath, 'utf-8')
    t.notEqual(shrinkwrapUtf8.split(CRLFreg).length, 2, 'npm-shrinkwrap.json is unformatted')
    t.done()
  })
})

test('cleanup', function (t) {
  server.close()
  cleanup()
  t.done()
})