Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit 8d6bfcb

Browse files
chrismeyersfsuothiym23
authored andcommitted
tests run with no system-wide side effects
All changes should be confined to the test repo.
1 parent f0bc711 commit 8d6bfcb

19 files changed

Lines changed: 248 additions & 256 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ npm-debug.log
66
/test/packages/npm-test-depends-on-spark/which-spark.log
77
/test/packages/test-package/random-data.txt
88
/test/root
9+
/test/npm_cache
910
/node_modules/marked-man
1011
/node_modules/tap
1112
/node_modules/nock

test/common-tap.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
var spawn = require("child_process").spawn
22

33
var port = exports.port = 1337
4-
exports.registry = "http://localhost:" + port
5-
process.env.npm_config_loglevel = "error"
4+
exports.registry = "http://localhost:" + port;
5+
process.env.npm_config_loglevel = "error";
6+
7+
var npm_config_cache = __dirname + '/npm_cache';
8+
exports.child_env = {
9+
npm_config_cache: npm_config_cache,
10+
};
11+
12+
var bin = exports.bin = require.resolve("../bin/npm-cli.js");
13+
var once = require("once");
614

7-
var bin = exports.bin = require.resolve("../bin/npm-cli.js")
8-
var once = require("once")
915
exports.npm = function (cmd, opts, cb) {
10-
cb = once(cb)
11-
cmd = [bin].concat(cmd)
12-
opts = opts || {}
16+
cb = once(cb);
17+
cmd = [bin].concat(cmd);
18+
opts = opts || {};
19+
20+
opts.env = opts.env ? opts.env : process.env;
21+
if (!opts.env.npm_config_cache) {
22+
opts.env.npm_config_cache = __dirname + "/npm_cache";
23+
}
1324

1425
var stdout = ""
1526
, stderr = ""
1627
, node = process.execPath
17-
, child = spawn(node, cmd, opts)
28+
, child = spawn(node, cmd, opts);
1829

1930
if (child.stderr) child.stderr.on("data", function (chunk) {
2031
stderr += chunk
@@ -29,4 +40,5 @@ exports.npm = function (cmd, opts, cb) {
2940
child.on("close", function (code) {
3041
cb(null, code, stdout, stderr)
3142
})
43+
return child;
3244
}

test/tap/00-verify-ls-ok.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
var common = require("../common-tap")
12
var test = require("tap").test
23
var node = process.execPath
34
var path = require("path")
45
var cwd = path.resolve(__dirname, "..", "..")
56
var npm = path.resolve(cwd, "cli.js")
67
var spawn = require("child_process").spawn
8+
var fs = require('fs')
79

810
test("npm ls in npm", function (t) {
11+
t.ok(fs.existsSync(cwd))
12+
913
var opt = { cwd: cwd, stdio: [ "ignore", "ignore", 2 ] }
10-
var child = spawn(node, [npm, "ls"], opt)
11-
child.on("close", function (code) {
12-
t.notOk(code)
14+
common.npm(['ls'], opt, function(err, code) {
15+
t.ok(code > 0)
1316
t.end()
1417
})
1518
})

test/tap/dedupe.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ var test = require("tap").test
77
, mr = require("npm-registry-mock")
88
, common = require('../common-tap.js')
99

10+
var EXEC_OPTS = { }
11+
1012
test("dedupe finds the common module and moves it up one level", function (t) {
1113
setup(function (s) {
12-
npm.install(".", function (err) {
13-
if (err) return t.fail(err)
14-
npm.dedupe(function(err) {
15-
if (err) return t.fail(err)
14+
common.npm(['install', '.', '--registry', common.registry], EXEC_OPTS, function(err, code) {
15+
t.equal(code, 0)
16+
common.npm(['dedupe'], EXEC_OPTS, function(err, code) {
17+
t.equal(code, 0)
1618
t.ok(existsSync(path.join(__dirname, "dedupe", "node_modules", "minimist")))
1719
t.ok(!existsSync(path.join(__dirname, "dedupe", "node_modules", "checker")))
1820
s.close() // shutdown mock registry.
@@ -25,10 +27,8 @@ test("dedupe finds the common module and moves it up one level", function (t) {
2527
function setup (cb) {
2628
process.chdir(path.join(__dirname, "dedupe"))
2729
mr(common.port, function (s) { // create mock registry.
28-
npm.load({registry: common.registry}, function() {
29-
rimraf.sync(path.join(__dirname, "dedupe", "node_modules"))
30-
fs.mkdirSync(path.join(__dirname, "dedupe", "node_modules"))
31-
cb(s)
32-
})
30+
rimraf.sync(path.join(__dirname, "dedupe", "node_modules"))
31+
fs.mkdirSync(path.join(__dirname, "dedupe", "node_modules"))
32+
cb(s)
3333
})
3434
}

test/tap/ignore-scripts.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
var common = require("../common-tap")
12
var test = require("tap").test
23
var npm = require.resolve("../../bin/npm-cli.js")
34

4-
var spawn = require("child_process").spawn
55
var node = process.execPath
66

77
// ignore-scripts/package.json has scripts that always exit with non-zero error
@@ -10,14 +10,14 @@ var node = process.execPath
1010
var pkg = __dirname + "/ignore-scripts"
1111

1212
test("ignore-scripts: install using the option", function(t) {
13-
createChild([npm, "install", "--ignore-scripts"]).on("close", function(code) {
13+
createChild(["install", "--ignore-scripts"], function(err, code) {
1414
t.equal(code, 0)
1515
t.end()
1616
})
1717
})
1818

1919
test("ignore-scripts: install NOT using the option", function(t) {
20-
createChild([npm, "install"]).on("close", function(code) {
20+
createChild(["install"], function(err, code) {
2121
t.notEqual(code, 0)
2222
t.end()
2323
})
@@ -36,24 +36,23 @@ var scripts = [
3636

3737
scripts.forEach(function(script) {
3838
test("ignore-scripts: run-script "+script+" using the option", function(t) {
39-
createChild([npm, "--ignore-scripts", "run-script", script])
40-
.on("close", function(code) {
41-
t.equal(code, 0)
42-
t.end()
43-
})
39+
createChild(["--ignore-scripts", "run-script", script], function(err, code) {
40+
t.equal(code, 0)
41+
t.end()
42+
})
4443
})
4544
})
4645

4746
scripts.forEach(function(script) {
4847
test("ignore-scripts: run-script "+script+" NOT using the option", function(t) {
49-
createChild([npm, "run-script", script]).on("close", function(code) {
48+
createChild(["run-script", script], function(err, code) {
5049
t.notEqual(code, 0)
5150
t.end()
5251
})
5352
})
5453
})
5554

56-
function createChild (args) {
55+
function createChild (args, cb) {
5756
var env = {
5857
HOME: process.env.HOME,
5958
Path: process.env.PATH,
@@ -64,9 +63,9 @@ function createChild (args) {
6463
if (process.platform === "win32")
6564
env.npm_config_cache = "%APPDATA%\\npm-cache"
6665

67-
return spawn(node, args, {
66+
return common.npm(args, {
6867
cwd: pkg,
6968
stdio: "inherit",
7069
env: env
71-
})
70+
}, cb)
7271
}

test/tap/install-at-locally.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var rimraf = require('rimraf')
77
var mkdirp = require('mkdirp')
88
var pkg = path.join(__dirname, 'install-at-locally')
99

10+
var EXEC_OPTS = { }
11+
1012
test("setup", function (t) {
1113
mkdirp.sync(pkg)
1214
mkdirp.sync(path.resolve(pkg, 'node_modules'))
@@ -15,22 +17,20 @@ test("setup", function (t) {
1517
})
1618

1719
test('"npm install ./package@1.2.3" should install local pkg', function(t) {
18-
npm.load(function() {
19-
npm.commands.install(['./package@1.2.3'], function(err) {
20-
var p = path.resolve(pkg, 'node_modules/install-at-locally/package.json')
21-
t.ok(JSON.parse(fs.readFileSync(p, 'utf8')))
22-
t.end()
23-
})
20+
common.npm(['install', './package@1.2.3'], EXEC_OPTS, function(err, code) {
21+
var p = path.resolve(pkg, 'node_modules/install-at-locally/package.json')
22+
t.equal(code, 0);
23+
t.ok(JSON.parse(fs.readFileSync(p, 'utf8')))
24+
t.end()
2425
})
2526
})
2627

2728
test('"npm install install/at/locally@./package@1.2.3" should install local pkg', function(t) {
28-
npm.load(function() {
29-
npm.commands.install(['./package@1.2.3'], function(err) {
30-
var p = path.resolve(pkg, 'node_modules/install-at-locally/package.json')
31-
t.ok(JSON.parse(fs.readFileSync(p, 'utf8')))
32-
t.end()
33-
})
29+
common.npm(['install', './package@1.2.3'], EXEC_OPTS, function(err, code) {
30+
var p = path.resolve(pkg, 'node_modules/install-at-locally/package.json')
31+
t.equal(code, 0);
32+
t.ok(JSON.parse(fs.readFileSync(p, 'utf8')))
33+
t.end()
3434
})
3535
})
3636

test/tap/install-cli-production.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ var rimraf = require('rimraf')
77
var mkdirp = require('mkdirp')
88
var pkg = path.join(__dirname, 'install-cli-production')
99

10+
var EXEC_OPTS = {
11+
cwd: pkg
12+
}
13+
1014
test("setup", function(t) {
1115
mkdirp.sync(pkg)
1216
mkdirp.sync(path.resolve(pkg, 'node_modules'))
@@ -15,26 +19,20 @@ test("setup", function(t) {
1519
})
1620

1721
test('"npm install --production" should install dependencies', function(t) {
18-
npm.load(function() {
19-
npm.config.set('production', true)
20-
npm.commands.install([], function(err) {
21-
if (err) return t.fail(err)
22-
var p = path.resolve(pkg, 'node_modules/dependency/package.json')
23-
t.ok(JSON.parse(fs.readFileSync(p, 'utf8')))
24-
t.end()
25-
})
22+
common.npm(['install', '--production'], EXEC_OPTS, function(err, code) {
23+
t.equal(code, 0)
24+
var p = path.resolve(pkg, 'node_modules/dependency/package.json')
25+
t.ok(JSON.parse(fs.readFileSync(p, 'utf8')))
26+
t.end()
2627
})
2728
})
2829

2930
test('"npm install --production" should not install dev dependencies', function(t) {
30-
npm.load(function() {
31-
npm.config.set('production', true)
32-
npm.commands.install([], function(err) {
33-
if (err) return t.fail(err)
34-
var p = path.resolve(pkg, 'node_modules/dev-dependency/package.json')
35-
t.ok(!fs.existsSync(p), '')
36-
t.end()
37-
})
31+
common.npm(['install', '--production'], EXEC_OPTS, function(err, code) {
32+
t.equal(code, 0)
33+
var p = path.resolve(pkg, 'node_modules/dev-dependency/package.json')
34+
t.ok(!fs.existsSync(p), '')
35+
t.end()
3836
})
3937
})
4038

test/tap/install-cli-unicode.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var test = require('tap').test
33
var npm = require('../../')
44
var mkdirp = require('mkdirp')
55
var mr = require('npm-registry-mock')
6-
var exec = require('child_process').exec
6+
var path = require("path")
77

88
var pkg = __dirname + '/install-cli'
99
var NPM_BIN = __dirname + '/../../bin/npm-cli.js'
@@ -12,12 +12,15 @@ function hasOnlyAscii (s) {
1212
return /^[\000-\177]*$/.test(s) ;
1313
}
1414

15+
var EXEC_OPTS = {
16+
cwd : pkg,
17+
}
18+
1519
test('does not use unicode with --unicode false', function (t) {
16-
t.plan(3)
20+
t.plan(4)
1721
mr(common.port, function (s) {
18-
exec('node ' + NPM_BIN + ' install --unicode false read', {
19-
cwd: pkg
20-
}, function(err, stdout) {
22+
common.npm(['install', '--unicode', 'false', 'read'], EXEC_OPTS, function(err, code, stdout, stderr) {
23+
t.equal(code, 0)
2124
t.ifError(err)
2225
t.ok(stdout, stdout.length)
2326
t.ok(hasOnlyAscii(stdout))
@@ -28,9 +31,8 @@ test('does not use unicode with --unicode false', function (t) {
2831

2932
test('cleanup', function (t) {
3033
mr(common.port, function (s) {
31-
exec('node ' + NPM_BIN + ' uninstall read', {
32-
cwd: pkg
33-
}, function(err, stdout) {
34+
common.npm(['uninstall', 'read'], EXEC_OPTS, function(err, code, stdout, stderr) {
35+
t.equal(code, 0)
3436
s.close()
3537
})
3638
})

test/tap/install-from-local.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1+
var common = require("../common-tap")
12
var test = require("tap").test
23
var npm = require("../../")
34
var path = require("path")
45
var fs = require("fs")
56
var rimraf = require("rimraf")
67
var pkg = path.join(__dirname, "install-from-local", "package-with-local-paths")
78

9+
var EXEC_OPTS = { }
10+
811
test("setup", function (t) {
912
process.chdir(pkg)
1013
t.end()
1114
})
1215

1316
test('"npm install" should install local packages', function (t) {
14-
npm.load({loglevel : "silent"}, function () {
15-
npm.commands.install(["."], function (err) {
16-
t.ifError(err, "local packages installed")
17-
var dependencyPackageJson = path.resolve(pkg, "node_modules/package-local-dependency/package.json")
18-
t.ok(
19-
JSON.parse(fs.readFileSync(dependencyPackageJson, "utf8")),
20-
"package with local dependency installed"
21-
)
17+
common.npm(["install", "."], EXEC_OPTS, function (err, code) {
18+
t.equal(code, 0)
19+
var dependencyPackageJson = path.resolve(pkg, "node_modules/package-local-dependency/package.json")
20+
t.ok(
21+
JSON.parse(fs.readFileSync(dependencyPackageJson, "utf8")),
22+
"package with local dependency installed"
23+
)
2224

23-
var devDependencyPackageJson = path.resolve(pkg, "node_modules/package-local-dev-dependency/package.json")
24-
t.ok(
25-
JSON.parse(fs.readFileSync(devDependencyPackageJson, "utf8")),
26-
"package with local dev dependency installed"
27-
)
25+
var devDependencyPackageJson = path.resolve(pkg, "node_modules/package-local-dev-dependency/package.json")
26+
t.ok(
27+
JSON.parse(fs.readFileSync(devDependencyPackageJson, "utf8")),
28+
"package with local dev dependency installed"
29+
)
2830

29-
t.end()
30-
})
31+
t.end()
3132
})
3233
})
3334

0 commit comments

Comments
 (0)