This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
152 lines (128 loc) · 4.2 KB
/
cmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /usr/bin/env node
var Reporter = require('test-report')
, path = require('path')
, fs = require('fs')
, d = require('d-utils')
, opts = d.merge({}, require('optimist').argv)
, tests = opts._
, ls = require('ls-r')
delete opts._
//
// usage
// [cmd] test/*.js
//
// --reportFile write report to file
// --isolate run each test in separate process]
// --timeout MS stop a test if it's taking longer that MS milliseconds]
// timeout must be user with --isolate
//
//
// TODO:
// * ignore files (defaults to fixtures, helpers)
// * function to run tests (adapter, tests, opts, callback) (which can run all the tests in isolation, etc)
//
function run (file, loader, adapter, reporter) {
var shutdown = function () {}
, failed = false
, tests
try {
tests = loader (path.resolve(file))
} catch (error) {
failed = true
reporter.error(error)
}
if (adapter && !failed)
shutdown = adapter.run(tests, reporter, function () {})
return shutdown
}
function go(adapter) {
//parse arguments, load files, run command
var reportFile = opts.reportFile || process.env.NODETEST_reportFile
, isolate = opts.isolate || process.env.NODETEST_isolate
, reporter = new Reporter(tests.length > 1 ? process.cwd() : tests[0])
, shutdowns = []
, isShutdown = false
function runShutdown () {
if(isShutdown) return
isShutdown = true
shutdowns.forEach(function (stop) {stop()})
if(reportFile)
fs.writeFileSync(reportFile, JSON.stringify(reporter.report))
else
console.log(require('test-report-view').view(reporter.report))
return reporter.report.failureCount
}
process.on('SIGINT', function () {
reporter.error(new Error("test manualy stopped"))
process.exit()
})
process.on('SIGTSTP', function () {
reporter.error(new Error("recieved stop signal due to timeout"))
process.exit()
})
process.on('SIGTERM', function () {
reporter.error(new Error("recieved stop signal due to timeout"))
process.exit()
})
process.on('exit', function (code) {
// return error count.
// check if the code is correct. if it is not, call exit(code)
// take care to not cause a stackOverflow
if(!isShutdown)
process.exit(runShutdown())
})
var lsOpts = {
strict: true
, prune: function (file) {
return /\.git|node_modules/.exec(path.basename(file.path))
}
, recursive: opts.recursive || false
}
// tests = d.map(tests, path.resolve)
// a way to pass in regular expressions to exclude, and sensible defaults, like exclude hidden files, fixtures, node_modules
if(!tests.length)
tests = ['./']
console.log('pre', tests, process.cwd())
ls(tests, lsOpts, function (err, tests) {
tests = d.filter(tests, /\.js$/)
console.log('POST',tests, process.cwd())
if(isolate && tests.length > 1) {
//run same node command again, but with only one test
var _cmd = process.argv[1]
, ctrl = require('ctrlflow')
, started = {}
ctrl.parallel.map(function (test, callback) {
started[test] = true
var child = require('./runner').runCP(_cmd, test, opts, function (err, report) {
started[test] = false
report.name = test
reporter.test(report)
callback(err, report)
})
console.log('isolating', test, "in", child.pid)
})(tests, function (err) {
if(err) reporter.error(err, 'strange')
// the process will exit when the event loop empties
// which should be right after this!
// if it isn't, something has been left dangling open
// or there is a still running timout or interval.
})
shutdowns = [function () {
d.map(started, function (notFinished, test) {
if(notFinished)
reporter.test(test, 'was started but did not finish')
})
}]
} else {
shutdowns =
tests.map(function (file) {
return run(file, function loader(file) {
return require(file)
}, adapter, tests.length > 1 ? reporter.subreport(file) : reporter)
})
}
})
}
exports.run = run
exports.go = go
if(!module.parent) go()