|
| 1 | +'use strict' |
| 2 | +const fs = require('fs') |
| 3 | +const path = require('path') |
| 4 | +const test = require('tap').test |
| 5 | +const mr = require('npm-registry-mock') |
| 6 | +const Tacks = require('tacks') |
| 7 | +const File = Tacks.File |
| 8 | +const Dir = Tacks.Dir |
| 9 | +const common = require('../common-tap.js') |
| 10 | + |
| 11 | +const basedir = common.pkg |
| 12 | +const testdir = path.join(basedir, 'testdir') |
| 13 | +const cachedir = common.cache |
| 14 | +const globaldir = path.join(basedir, 'global') |
| 15 | +const tmpdir = path.join(basedir, 'tmp') |
| 16 | + |
| 17 | +const pkgPath = path.join(testdir, 'package.json') |
| 18 | +const pkgLockPath = path.join(testdir, 'package-lock.json') |
| 19 | +const shrinkwrapPath = path.join(testdir, 'npm-shrinkwrap.json') |
| 20 | +const CRLFreg = /\r\n|\r|\n/ |
| 21 | + |
| 22 | +const 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 | + |
| 31 | +var server |
| 32 | +var 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 | + |
| 47 | +function setup () { |
| 48 | + cleanup() |
| 49 | + fixture.create(basedir) |
| 50 | +} |
| 51 | + |
| 52 | +function cleanup () { |
| 53 | + fixture.remove(basedir) |
| 54 | +} |
| 55 | + |
| 56 | +test('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 | + |
| 64 | +test('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 | + |
| 77 | +test('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 | + |
| 88 | +test('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 | + |
| 101 | +test('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 | + |
| 112 | +test('cleanup', function (t) { |
| 113 | + server.close() |
| 114 | + cleanup() |
| 115 | + t.done() |
| 116 | +}) |
0 commit comments