Skip to content

Commit

Permalink
refactoring and code organization
Browse files Browse the repository at this point in the history
Nudging slightly towards having a single 'main' method for a child test.
Seriously considering making spawn test children, in-process subtest,
and stdin subtests all inherit from some root class, just to get a
consistent interface.

Once the 'run the subtest' and 'collect the results' logic is fully
separated, it'll be possible to have two run at once and collect them
both up later, making parallel jobs possible.

Having an enormous amount of tests makes this work even possible.
Another helpful step may be to push for 100% coverage (or as close as is
feasible.)
  • Loading branch information
isaacs committed Jan 9, 2017
1 parent 642fada commit da59c8e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
62 changes: 37 additions & 25 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ function Test (options) {

this._timer = null
this._timeout = 0
if (options.timeout !== Infinity &&
!isNaN(options.timeout) &&
options.timeout > 0) {
this.setTimeout(options.timeout)
}

Readable.apply(this, options)

Expand Down Expand Up @@ -194,9 +189,10 @@ Test.prototype._queueFail = function () {
var extra = { at: this._calledAt }

switch (what) {
case 'test':
extra = q[2]
msg = 'child test left in queue: ' + (q[1] || '(unnamed)')
case '_runChild':
extra = q[3]
name = q[2] || '(unnamed)'
msg = 'child test left in queue: ' + name
break

case 'printResult':
Expand Down Expand Up @@ -467,34 +463,41 @@ Test.prototype.test = function test (name, extra, cb, deferred) {
extra.at = stack.at(test)
}

if (this._currentChild) {
this._queue.push(['test', name, extra, cb, deferred])
return deferred.promise
}
var child = this._createChild(name, extra, deferred)

var child = new Test(extra)
if (!extra.buffered) {
this.comment('Subtest: ' + name)
}
this._runChild(child, name, extra, cb, deferred)
return deferred.promise
}

Test.prototype._createChild = function (name, extra, deferred) {
var self = this
var child = new Test(extra)
child._name = name
child._parent = this
if (!Object.prototype.hasOwnProperty.call(extra, 'bail')) {
child._bail = this._bail
}
child._deferred = deferred
return child
}

Test.prototype._runChild = function (child, name, extra, cb, deferred) {
if (this._currentChild) {
this._queue.push(['_runChild', child, name, extra, cb, deferred])
return
}
if (!child._buffered) {
this.comment('Subtest: ' + name)
}
this._currentChild = child

var self = this
if (!extra.buffered) {
childStream(self, child)
if (!child._buffered) {
childStream(this, child)
}
self._level = child
child._deferred = deferred
this._level = child
var self = this
this._runBeforeEach(child, function () {
self._runChild(child, name, extra, cb)
self._childMain(child, name, extra, cb)
})
return deferred.promise
}

Test.prototype._runBeforeEach = function (who, cb) {
Expand Down Expand Up @@ -562,10 +565,19 @@ function childExtra (extra, results) {
delete extra.stack
}

Test.prototype._runChild = function runChild (child, name, extra, cb) {
Test.prototype._childMain = function childMain (child, name, extra, cb) {
var self = this
var results
var ended = false

child._startTime = process.hrtime()
if (extra.timeout !== Infinity &&
!isNaN(extra.timeout) &&
extra.timeout > 0) {

child.setTimeout(extra.timeout)
}

child.on('complete', function (res) {
results = pruneFailures(res)
})
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"regen-fixtures": "node scripts/generate-test-test.js test/test/*.js",
"test": "node bin/run.js test/*.* --coverage -t3600",
"smoke": "node test/test.js \"*.js\" | node bin/run.js -",
"posttest": "standard lib test"
"posttest": "standard lib test",
"t": "node bin/run.js test/*.* -sfails.txt"
},
"devDependencies": {
"mkdirp": "^0.5.1",
Expand Down
9 changes: 9 additions & 0 deletions test/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ tap.test('t.setTimeout()', function (t) {
' not ok 2 - timeout!\n' +
' ---\n' +
' timeout: 1\n' +
' at:\n' +
' line: [\\d]+\n' +
' column: [\\d]+\n' +
' file: test.timeout\\.js\n' +
'( type: global\n)?' +
' source: |\n' +
' tt = new Test\\([^\\)]+\\)\n' +
' \\.\\.\\.\n' +
' timeout: 1\n' +
' \\.\\.\\.\n' +
'\\s*\n' +
' 1..2\n' +
Expand Down

0 comments on commit da59c8e

Please sign in to comment.