Debian Codegraph @

debian/tests/test_modules/node-zap/bin/zap

raw
1 #!/usr/bin/env node
2 // vim: set ft=javascript :
3
4 var fs = require('fs')
5 , path = require('path')
6 , spawn = require('child_process').spawn
7 , util = require('util')
8
9 var coffee
10 try {
11 coffee = require('coffee-script/register')
12 } catch (e0) {
13 try {
14 coffee = require('coffee-script')
15 } catch (e1) {}
16 }
17
18 if (process.argv[2] === '--one') {
19 // run a particular test
20 var test = {
21 done: function () {
22 test._finished++
23 },
24 finish: function () { this.done() },
25 _finished: 0,
26 fail: function (message) {
27 console.log("Test failed: ", message, "\n")
28 process.exit(1)
29 },
30 }
31 process.on('exit', function (code) {
32 if (suite && suite.teardown) {
33 suite.teardown.call(context, test)
34 }
35 if (code > 0) { return }
36 if (test._finished == 1) {
37 process.reallyExit(0)
38 } else {
39 if (test._finished == 0) {
40 console.log("Test didn't call done().")
41 } else {
42 console.log("Test called done()", test._finished, "times.")
43 }
44 process.reallyExit(1)
45 }
46 })
47 var suite = require(path.resolve(process.argv[3]))
48 var context = {}
49 if (suite.setup) {
50 suite.setup.call(context, test)
51 }
52 suite[process.argv[4]].call(context, test)
53 } else {
54 // run a suite
55 var test_targets = []
56 if (process.argv.length <= 2) {
57 if (!fs.existsSync('./test')) {
58 console.error(
59 "Test directory (./test) not found. zap suggests:\n\n" +
60 "\x1b[36m$\x1b[0m mkdir ./test\n" +
61 "\x1b[36m$\x1b[0m cat > ./test/example.test.js\n" +
62 "var assert = require('assert')\n" +
63 "module.exports = {\n" +
64 " 'example test': function (test) {\n" +
65 " assert.ok(true, 'test passed!');\n" +
66 " test.finish()\n" +
67 " },\n" +
68 "}\n" +
69 "\x1b[36m$\x1b[0m zap\n" +
70 "\x1b[33;1m\u26a1 \u26a1 \u26a1\x1b[0m")
71 process.exit(1)
72 }
73 if (!fs.statSync('./test').isDirectory()) {
74 console.error("zap: ./test is not a directory")
75 process.exit(1)
76 }
77 test_targets.push('./test')
78 } else {
79 for (var i = 2; i < process.argv.length; i++) {
80 test_targets.push(process.argv[i])
81 }
82 }
83 var test_files = []
84 test_targets.forEach(function (target) {
85 test_files = test_files.concat(allTestFiles(target))
86 })
87
88 function allTestFiles(pth, match) {
89 var stats = fs.statSync(pth)
90 if (stats.isFile()) {
91 if (!match || pth.match(match)) {
92 return [path.resolve(pth)]
93 } else {
94 return []
95 }
96 } else if (stats.isDirectory()) {
97 var paths = []
98 fs.readdirSync(pth).forEach(function (file) {
99 var rfile
100 if (coffee)
101 rfile = /\.test\.(js|coffee)$/
102 else
103 rfile = /\.test\.js$/
104 paths = paths.concat(allTestFiles(path.join(pth, file), rfile))
105 })
106 return paths
107 } else {
108 return []
109 }
110 }
111
112 var tests = []
113 test_files.forEach(function (tf) {
114 Object.keys(require(tf)).forEach(function (test) {
115 if (['setup','teardown'].indexOf(test) >= 0) { return }
116 tests.push({ file: tf, test: test })
117 })
118 })
119 if (tests.length === 0) {
120 console.info("All of 0 tests passed. Yeah!")
121 process.exit(0)
122 }
123
124 function name(t) {
125 return "\x1b[35m" + path.basename(t.file).match(/^(.+?)(\.test)?\.(js|coffee)$/)[1] + "\x1b[0m" + '/' + t.test
126 }
127
128 function runOne(t, cb) {
129 var output = ''
130 var runner = spawn(process.execPath, [__filename, '--one', t.file, t.test])
131 runner.on('exit', function (code) {
132 if (code == 0) {
133 cb()
134 } else {
135 cb(output)
136 }
137 })
138 runner.stdout.on('data', function (data) { output += data })
139 runner.stderr.on('data', function (data) { output += data })
140 }
141
142 var numFailures = 0
143 function runPar() {
144 var ordered = { upTo: 0 }
145 function inOrder(n, cb) {
146 if (!ordered[n]) { ordered[n] = [] }
147 if (ordered.upTo == n) {
148 cb()
149 } else {
150 ordered[n].push(cb)
151 }
152 }
153 function doneOrder(n) {
154 ordered[n].done = true
155 while (ordered[ordered.upTo] && ordered[ordered.upTo].done) {
156 ordered[ordered.upTo].forEach(function (cb) { cb() })
157 ordered.upTo++
158 }
159 if (ordered[ordered.upTo]) {
160 ordered[ordered.upTo].forEach(function (cb) { cb() })
161 }
162 ordered[ordered.upTo] = []
163 }
164 var testOrder = 0
165 function runNext() {
166 if (tests.length <= 0) { return }
167 var t = tests.shift()
168 var o = testOrder++
169 inOrder(o, function () {
170 process.stdout.write(
171 util.format("%s... ", name(t)))
172 })
173 runOne(t, function (err) {
174 if (err) {
175 numFailures++
176 inOrder(o, function () {
177 console.log("\x1b[1;31mfailed\x1b[0m")
178 console.log(err)
179 })
180 } else {
181 inOrder(o, function () {
182 console.log("passed")
183 })
184 }
185 doneOrder(o)
186 runNext()
187 })
188 }
189 var numCPUs = require('os').cpus().length
190 for (var i = 0; i < numCPUs + 1; i++) {
191 runNext()
192 }
193 }
194
195 process.on('exit', function() {
196 process.reallyExit(numFailures ? 1 : 0)
197 })
198 runPar()
199 }