1 // Everything in this file uses child processes, because we're
2 // testing a command line utility.
4 var chain = require("slide").chain
5 var child_process = require("child_process")
6 var path = require("path")
8 , fs = require("graceful-fs")
9 , npmpkg = path.dirname(testdir)
10 , npmcli = path.resolve(npmpkg, "bin", "npm-cli.js")
12 var temp = process.env.TMPDIR
15 || ( process.platform === "win32"
19 temp = path.resolve(temp, "npm-test-" + process.pid)
21 var root = path.resolve(temp, "root")
22 , cache = path.resolve(temp, "npm_cache")
25 , mkdir = require("mkdirp")
26 , rimraf = require("rimraf")
28 var pathEnvSplit = process.platform === "win32" ? ";" : ":"
29 , pathEnv = process.env.PATH.split(pathEnvSplit)
30 , npmPath = process.platform === "win32" ? root : path.join(root, "bin")
32 pathEnv.unshift(npmPath, path.join(root, "node_modules", ".bin"))
34 // lastly, make sure that we get the same node that is being used to do
35 // run this script. That's very important, especially when running this
36 // test file from in the node source folder.
37 pathEnv.unshift(path.dirname(process.execPath))
39 // the env for all the test installs etc.
41 Object.keys(process.env).forEach(function (i) {
42 env[i] = process.env[i]
44 env.npm_config_prefix = root
45 env.npm_config_color = "always"
46 env.npm_config_global = "true"
47 // have to set this to false, or it'll try to test itself forever
48 env.npm_config_npat = "false"
49 env.PATH = pathEnv.join(pathEnvSplit)
50 env.NODE_PATH = path.join(root, "node_modules")
51 env.npm_config_cache = cache
55 function cleanup (cb) {
56 if (failures !== 0) return
57 rimraf(root, function (er) {
63 function prefix (content, pref) {
64 return pref + (content.trim().split(/\r?\n/).join("\n" + pref))
68 function exec (cmd, cwd, shouldFail, cb) {
69 if (typeof shouldFail === "function") {
70 cb = shouldFail, shouldFail = false
72 console.error("\n+"+cmd + (shouldFail ? " (expect failure)" : ""))
74 // special: replace 'node' with the current execPath,
75 // and 'npm' with the thing we installed.
77 var npmReplace = path.resolve(npmPath, "npm")
78 var nodeReplace = process.execPath
79 if (process.platform === "win32") {
80 npmReplace = '"' + npmReplace + '"'
81 nodeReplace = '"' + nodeReplace + '"'
83 cmd = cmd.replace(/^npm /, npmReplace + " ")
84 cmd = cmd.replace(/^node /, nodeReplace + " ")
86 console.error("$$$$$$ cd %s; PATH=%s %s", cwd, env.PATH, cmd)
88 child_process.exec(cmd, {cwd: cwd, env: env}, function (er, stdout, stderr) {
89 console.error("$$$$$$ after command", cmd, cwd)
91 console.error(prefix(stdout, " 1> "))
94 console.error(prefix(stderr, " 2> "))
98 if (!shouldFail && !er || shouldFail && er) {
99 // stdout = (""+stdout).trim()
100 console.log("ok " + execCount + " " + cmdShow)
103 console.log("not ok " + execCount + " " + cmdShow)
104 cb(new Error("failed "+cmdShow))
109 function execChain (cmds, cb) {
110 chain(cmds.map(function (args) {
111 return [exec].concat(args)
115 function flatten (arr) {
116 return arr.reduce(function (l, r) {
121 function setup (cb) {
122 cleanup(function (er) {
123 if (er) return cb(er)
124 exec("node \""+npmcli+"\" install \""+npmpkg+"\"", root, false, cb)
129 console.log("# testing in %s", temp)
130 console.log("# global prefix = %s", root)
136 process.chdir(testdir)
137 var base = path.resolve(root, path.join("lib", "node_modules"))
139 // get the list of packages
140 var packages = fs.readdirSync(path.resolve(testdir, "packages"))
141 packages = packages.filter(function (p) {
142 return p && !p.match(/^\./)
145 installAllThenTestAll()
147 function installAllThenTestAll () {
148 var packagesToRm = packages.slice(0)
149 if (process.platform !== "win32") {
150 // Windows can't handle npm rm npm due to file-in-use issues.
151 packagesToRm.push("npm")
156 , [ exec, "npm install "+npmpkg, testdir ]
157 , [ execChain, packages.map(function (p) {
158 return [ "npm install packages/"+p, testdir ]
160 , [ execChain, packages.map(function (p) {
161 return [ "npm test -ddd", path.resolve(base, p) ]
163 , [ execChain, packagesToRm.map(function (p) {
164 return [ "npm rm "+p, root ]
172 function installAndTestEach (cb) {
173 var thingsToChain = [
175 , [ execChain, flatten(packages.map(function (p) {
176 return [ [ "npm install packages/"+p, testdir ]
177 , [ "npm test", path.resolve(base, p) ]
178 , [ "npm rm "+p, root ] ]
181 if (process.platform !== "win32") {
182 // Windows can't handle npm rm npm due to file-in-use issues.
183 thingsToChain.push([exec, "npm rm npm", testdir])
186 chain(thingsToChain, cb)
191 console.log("1.." + execCount)