]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/run.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / run.js
1 // Everything in this file uses child processes, because we're
2 // testing a command line utility.
3
4 var chain = require("slide").chain
5 var child_process = require("child_process")
6 var path = require("path")
7   , testdir = __dirname
8   , fs = require("graceful-fs")
9   , npmpkg = path.dirname(testdir)
10   , npmcli = path.resolve(npmpkg, "bin", "npm-cli.js")
11
12 var temp = process.env.TMPDIR
13          || process.env.TMP
14          || process.env.TEMP
15          || ( process.platform === "win32"
16             ? "c:\\windows\\temp"
17             : "/tmp" )
18
19 temp = path.resolve(temp, "npm-test-" + process.pid)
20
21 var root = path.resolve(temp, "root")
22   , cache = path.resolve(temp, "npm_cache")
23
24 var failures = 0
25   , mkdir = require("mkdirp")
26   , rimraf = require("rimraf")
27
28 var pathEnvSplit = process.platform === "win32" ? ";" : ":"
29   , pathEnv = process.env.PATH.split(pathEnvSplit)
30   , npmPath = process.platform === "win32" ? root : path.join(root, "bin")
31
32 pathEnv.unshift(npmPath, path.join(root, "node_modules", ".bin"))
33
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))
38
39 // the env for all the test installs etc.
40 var env = {}
41 Object.keys(process.env).forEach(function (i) {
42   env[i] = process.env[i]
43 })
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
52
53
54
55 function cleanup (cb) {
56   if (failures !== 0) return
57   rimraf(root, function (er) {
58     if (er) cb(er)
59     mkdir(root, 0755, cb)
60   })
61 }
62
63 function prefix (content, pref) {
64   return pref + (content.trim().split(/\r?\n/).join("\n" + pref))
65 }
66
67 var execCount = 0
68 function exec (cmd, cwd, shouldFail, cb) {
69   if (typeof shouldFail === "function") {
70     cb = shouldFail, shouldFail = false
71   }
72   console.error("\n+"+cmd + (shouldFail ? " (expect failure)" : ""))
73
74   // special: replace 'node' with the current execPath,
75   // and 'npm' with the thing we installed.
76   var cmdShow = cmd
77   var npmReplace = path.resolve(npmPath, "npm")
78   var nodeReplace = process.execPath
79   if (process.platform === "win32") {
80     npmReplace = '"' + npmReplace + '"'
81     nodeReplace = '"' + nodeReplace + '"'
82   }
83   cmd = cmd.replace(/^npm /, npmReplace + " ")
84   cmd = cmd.replace(/^node /, nodeReplace + " ")
85
86   console.error("$$$$$$ cd %s; PATH=%s %s", cwd, env.PATH, cmd)
87
88   child_process.exec(cmd, {cwd: cwd, env: env}, function (er, stdout, stderr) {
89     console.error("$$$$$$ after command", cmd, cwd)
90     if (stdout) {
91       console.error(prefix(stdout, " 1> "))
92     }
93     if (stderr) {
94       console.error(prefix(stderr, " 2> "))
95     }
96
97     execCount ++
98     if (!shouldFail && !er || shouldFail && er) {
99       // stdout = (""+stdout).trim()
100       console.log("ok " + execCount + " " + cmdShow)
101       return cb()
102     } else {
103       console.log("not ok " + execCount + " " + cmdShow)
104       cb(new Error("failed "+cmdShow))
105     }
106   })
107 }
108
109 function execChain (cmds, cb) {
110   chain(cmds.map(function (args) {
111     return [exec].concat(args)
112   }), cb)
113 }
114
115 function flatten (arr) {
116   return arr.reduce(function (l, r) {
117     return l.concat(r)
118   }, [])
119 }
120
121 function setup (cb) {
122   cleanup(function (er) {
123     if (er) return cb(er)
124     exec("node \""+npmcli+"\" install \""+npmpkg+"\"", root, false, cb)
125   })
126 }
127
128 function main (cb) {
129   console.log("# testing in %s", temp)
130   console.log("# global prefix = %s", root)
131
132
133
134   failures = 0
135
136   process.chdir(testdir)
137   var base = path.resolve(root, path.join("lib", "node_modules"))
138
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(/^\./)
143   })
144
145   installAllThenTestAll()
146
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")
152     }
153
154     chain(
155         [ setup
156         , [ exec, "npm install "+npmpkg, testdir ]
157         , [ execChain, packages.map(function (p) {
158               return [ "npm install packages/"+p, testdir ]
159             }) ]
160         , [ execChain, packages.map(function (p) {
161               return [ "npm test -ddd", path.resolve(base, p) ]
162             }) ]
163         , [ execChain, packagesToRm.map(function (p) {
164               return [ "npm rm "+p, root ]
165             }) ]
166         , installAndTestEach
167         ]
168       , cb
169       )
170   }
171
172   function installAndTestEach (cb) {
173     var thingsToChain = [
174         setup
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 ] ]
179             })) ]
180       ]
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])
184     }
185
186     chain(thingsToChain, cb)
187   }
188 }
189
190 main(function (er) {
191   console.log("1.." + execCount)
192   if (er) throw er
193 })