• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2const fs = require('fs')
3const path = require('path')
4const test = require('tap').test
5const mr = require('npm-registry-mock')
6const Tacks = require('tacks')
7const File = Tacks.File
8const Dir = Tacks.Dir
9const common = require('../common-tap.js')
10
11const basedir = common.pkg
12const testdir = path.join(basedir, 'testdir')
13const cachedir = common.cache
14const globaldir = path.join(basedir, 'global')
15const tmpdir = path.join(basedir, 'tmp')
16
17const pkgPath = path.join(testdir, 'package.json')
18const pkgLockPath = path.join(testdir, 'package-lock.json')
19const shrinkwrapPath = path.join(testdir, 'npm-shrinkwrap.json')
20const CRLFreg = /\r\n|\r|\n/
21
22const env = common.newEnv().extend({
23  npm_config_cache: cachedir,
24  npm_config_tmp: tmpdir,
25  npm_config_prefix: globaldir,
26  npm_config_registry: common.registry,
27  npm_config_loglevel: 'warn',
28  npm_config_format_package_lock: false
29})
30
31var server
32var fixture = new Tacks(Dir({
33  cache: Dir(),
34  global: Dir(),
35  tmp: Dir(),
36  testdir: Dir({
37    'package.json': File({
38      name: 'install-package-lock-only',
39      version: '1.0.0',
40      dependencies: {
41        mkdirp: '^0.3.4'
42      }
43    })
44  })
45}))
46
47function setup () {
48  cleanup()
49  fixture.create(basedir)
50}
51
52function cleanup () {
53  fixture.remove(basedir)
54}
55
56test('setup', function (t) {
57  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
58    if (err) throw err
59    server = s
60    t.done()
61  })
62})
63
64test('package-lock.json unformatted, package.json formatted when config has `format-package-lock: false`', function (t) {
65  setup()
66  common.npm(['install'], {cwd: testdir, env}).spread((code, stdout, stderr) => {
67    t.is(code, 0, 'ok')
68    t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
69    const pkgLockUtf8 = fs.readFileSync(pkgLockPath, 'utf-8')
70    t.equal(pkgLockUtf8.split(CRLFreg).length, 2, 'package-lock.json is unformatted')
71    const pkgUtf8 = fs.readFileSync(pkgPath, 'utf-8')
72    t.notEqual(pkgUtf8.split(CRLFreg).length, 2, 'package.json is formatted')
73    t.done()
74  })
75})
76
77test('npm-shrinkwrap.json unformatted when config has `format-package-lock: false`', function (t) {
78  setup()
79  common.npm(['shrinkwrap'], {cwd: testdir, env}).spread((code, stdout, stderr) => {
80    t.is(code, 0, 'ok')
81    t.ok(fs.existsSync(shrinkwrapPath), 'ensure that npm-shrinkwrap.json was created')
82    const shrinkwrapUtf8 = fs.readFileSync(shrinkwrapPath, 'utf-8')
83    t.equal(shrinkwrapUtf8.split(CRLFreg).length, 2, 'npm-shrinkwrap.json is unformatted')
84    t.done()
85  })
86})
87
88test('package-lock.json and package.json formatted when config has `format-package-lock: true`', function (t) {
89  setup()
90  common.npm(['install'], {cwd: testdir}).spread((code, stdout, stderr) => {
91    t.is(code, 0, 'ok')
92    t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
93    const pkgLockUtf8 = fs.readFileSync(pkgLockPath, 'utf-8')
94    t.notEqual(pkgLockUtf8.split(CRLFreg).length, 2, 'package-lock.json is formatted')
95    const pkgUtf8 = fs.readFileSync(pkgPath, 'utf-8')
96    t.notEqual(pkgUtf8.split(CRLFreg).length, 2, 'package.json is formatted')
97    t.done()
98  })
99})
100
101test('npm-shrinkwrap.json formatted when config has `format-package-lock: true`', function (t) {
102  setup()
103  common.npm(['shrinkwrap'], {cwd: testdir}).spread((code, stdout, stderr) => {
104    t.is(code, 0, 'ok')
105    t.ok(fs.existsSync(shrinkwrapPath), 'ensure that npm-shrinkwrap.json was created')
106    const shrinkwrapUtf8 = fs.readFileSync(shrinkwrapPath, 'utf-8')
107    t.notEqual(shrinkwrapUtf8.split(CRLFreg).length, 2, 'npm-shrinkwrap.json is unformatted')
108    t.done()
109  })
110})
111
112test('cleanup', function (t) {
113  server.close()
114  cleanup()
115  t.done()
116})
117