Skip to content

Commit

Permalink
test: Add various tests for prune --production
Browse files Browse the repository at this point in the history
PR-URL: npm/npm#16637
Credit: @exogen
Reviewed-By: @iarna
  • Loading branch information
exogen authored and zkat committed Jul 5, 2017
1 parent c3b586a commit 656544c
Show file tree
Hide file tree
Showing 3 changed files with 325 additions and 0 deletions.
87 changes: 87 additions & 0 deletions test/tap/prune-dev-dep-cycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict'
var fs = require('fs')
var path = require('path')
var test = require('tap').test
var Tacks = require('tacks')
var File = Tacks.File
var Dir = Tacks.Dir
var common = require('../common-tap.js')
var testdir = path.join(__dirname, path.basename(__filename, '.js'))

var fixture = new Tacks(
Dir({
node_modules: Dir({
'a': Dir({
'package.json': File({
_requested: {
rawSpec: 'file:///mods/a'
},
dependencies: {
'b': 'file:///mods/b'
},
name: 'a',
version: '1.0.0'
})
}),
'b': Dir({
'package.json': File({
_requested: {
rawSpec: 'file:///mods/b'
},
dependencies: {
'a': 'file:///mods/a'
},
name: 'b',
version: '1.0.0'
})
})
}),
'package.json': File({
name: 'test',
version: '1.0.0',
devDependencies: {
'a': 'file:///mods/a'
}
})
})
)

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

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

test('setup', function (t) {
setup()
t.end()
})


function readdir (dir) {
try {
return fs.readdirSync(dir)
} catch (ex) {
if (ex.code === 'ENOENT') return []
throw ex
}
}

test('prune cycle in dev deps', function (t) {
common.npm(['prune', '--production', '--json'], {cwd: testdir}, function (err, code, stdout, stderr) {
if (err) throw err
t.is(code, 0, 'prune finished successfully')
t.like(JSON.parse(stdout), {removed: [{name: 'a'}, {name: 'b'}]}, 'removed the right modules')
var dirs = readdir(testdir + '/node_modules').sort()
t.same(dirs, [])
t.end()
})
})

test('cleanup', function (t) {
cleanup()
t.end()
})
114 changes: 114 additions & 0 deletions test/tap/prune-with-dev-dep-duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
var fs = require('fs')
var path = require('path')

var mkdirp = require('mkdirp')
var mr = require('npm-registry-mock')
var osenv = require('osenv')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap')
var server

var pkg = path.resolve(__dirname, 'prune')
var cache = path.resolve(pkg, 'cache')

var json = {
name: 'prune-with-dev-dep-duplicate',
description: 'fixture',
version: '0.0.1',
main: 'index.js',
dependencies: {
'test-package': '0.0.0'
},
devDependencies: {
'test-package': '0.0.0'
}
}

var EXEC_OPTS = {
cwd: pkg,
npm_config_depth: 'Infinity'
}

test('setup', function (t) {
cleanup()
mkdirp.sync(cache)
fs.writeFileSync(
path.join(pkg, 'package.json'),
JSON.stringify(json, null, 2)
)
mr({ port: common.port }, function (er, s) {
server = s
t.end()
})
})

function readdir (dir) {
try {
return fs.readdirSync(dir)
} catch (ex) {
if (ex.code === 'ENOENT') return []
throw ex
}
}

test('npm install also=dev', function (t) {
common.npm([
'install',
'--cache', cache,
'--registry', common.registry,
'--loglevel', 'silent',
'--also=dev'
], EXEC_OPTS, function (err, code, stdout, stderr) {
if (err) throw err
t.notOk(code, 'exit ok')
t.notOk(stderr, 'Should not get data on stderr: ' + stderr)
var dirs = readdir(pkg + '/node_modules').sort()
t.same(dirs, [ 'test-package' ].sort())
t.end()
})
})

test('npm prune also=dev', function (t) {
common.npm([
'prune',
'--loglevel', 'silent',
'--also=dev'
], EXEC_OPTS, function (err, code, stdout, stderr) {
if (err) throw err
t.notOk(code, 'exit ok')
t.notOk(stderr, 'Should not get data on stderr: ' + stderr)
var dirs = readdir(pkg + '/node_modules').sort()
t.same(dirs, [ 'test-package' ])
t.end()
})
})

test('npm prune only=prod', function (t) {
common.npm([
'prune',
'--loglevel', 'silent',
'--only=prod',
'--json'
], EXEC_OPTS, function (err, code, stdout, stderr) {
if (err) throw err
t.notOk(code, 'exit ok')
t.isDeeply(JSON.parse(stdout).removed, [], 'removed nothing')
var dirs = readdir(pkg + '/node_modules').sort()
t.same(dirs, [ 'test-package' ])
t.end()
})
})

test('cleanup', function (t) {
server.close()
cleanup()
t.pass('cleaned up')
t.end()
})

function cleanup () {
process.chdir(osenv.tmpdir())
rimraf.sync(pkg)
}
124 changes: 124 additions & 0 deletions test/tap/prune-with-only-dev-deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
var fs = require('fs')
var path = require('path')

var mkdirp = require('mkdirp')
var mr = require('npm-registry-mock')
var osenv = require('osenv')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap')
var server

var pkg = path.resolve(__dirname, 'prune')
var cache = path.resolve(pkg, 'cache')

var json = {
name: 'prune-with-only-dev-deps',
description: 'fixture',
version: '0.0.1',
main: 'index.js',
devDependencies: {
'test-package-with-one-dep': '0.0.0',
'test-package': '0.0.0'
}
}

var EXEC_OPTS = {
cwd: pkg,
npm_config_depth: 'Infinity'
}

test('setup', function (t) {
cleanup()
mkdirp.sync(cache)
fs.writeFileSync(
path.join(pkg, 'package.json'),
JSON.stringify(json, null, 2)
)
mr({ port: common.port }, function (er, s) {
server = s
t.end()
})
})

test('npm install', function (t) {
common.npm([
'install',
'--cache', cache,
'--registry', common.registry,
'--loglevel', 'silent',
'--production', 'false'
], EXEC_OPTS, function (err, code, stdout, stderr) {
t.ifErr(err, 'install finished successfully')
t.notOk(code, 'exit ok')
t.notOk(stderr, 'Should not get data on stderr: ' + stderr)
t.end()
})
})

function readdir (dir) {
try {
return fs.readdirSync(dir)
} catch (ex) {
if (ex.code === 'ENOENT') return []
throw ex
}
}

test('verify installs', function (t) {
var dirs = readdir(pkg + '/node_modules').sort()
t.same(dirs, [ 'test-package', 'test-package-with-one-dep' ].sort())
t.end()
})

test('npm prune', function (t) {
common.npm([
'prune',
'--loglevel', 'silent',
'--production', 'false'
], EXEC_OPTS, function (err, code, stdout, stderr) {
t.ifErr(err, 'prune finished successfully')
t.notOk(code, 'exit ok')
t.notOk(stderr, 'Should not get data on stderr: ' + stderr)
t.end()
})
})

test('verify installs', function (t) {
var dirs = readdir(pkg + '/node_modules').sort()
t.same(dirs, [ 'test-package', 'test-package-with-one-dep' ])
t.end()
})

test('npm prune', function (t) {
common.npm([
'prune',
'--loglevel', 'silent',
'--production',
'--json'
], EXEC_OPTS, function (err, code, stdout, stderr) {
t.ifErr(err, 'prune finished successfully')
t.notOk(code, 'exit ok')
t.like(JSON.parse(stdout), {removed: [{name: 'test-package'}, {name: 'test-package-with-one-dep'}]})
t.end()
})
})

test('verify installs', function (t) {
var dirs = readdir(pkg + '/node_modules').sort()
t.same(dirs, [])
t.end()
})

test('cleanup', function (t) {
server.close()
cleanup()
t.pass('cleaned up')
t.end()
})

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

0 comments on commit 656544c

Please sign in to comment.