All files / lib batch.js

91.71% Statements 166/181
83.33% Branches 40/48
89.47% Functions 17/19
91.71% Lines 166/181

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                                        4x 4x 4x   4x 4x 4x       68x 68x 68x   68x 68x   68x 68x 68x 68x   68x 209x 209x     209x 68x 68x   68x 141x 6x 6x   6x 135x 116x 116x 19x 19x 19x           68x   68x     68x 68x   68x 6x   62x         66x 66x   66x   66x   66x   66x   66x           66x 66x   66x 37x 37x 25x 25x 25x 25x     12x 12x       66x   66x 66x 66x 65x   1x 1x 1x     65x   65x 37x 28x 3x 3x   3x 3x 3x 3x   3x           25x 25x     65x       66x 66x 66x 66x   66x   66x 66x   66x 66x 66x   66x 4x 4x 4x 62x 3x 3x   59x 59x 59x         59x   59x         64x         66x   66x 6x   6x 6x 6x 4x 2x 2x 2x   2x                           60x   60x         66x 66x 66x 66x   66x   66x       66x 2x         66x 66x   66x         66x   66x 66x   66x   66x 114x 114x 114x   114x   114x 114x 102x 102x 102x   102x   12x   12x 12x 12x 12x             59x   59x 59x 59x   59x   59x 44x   15x   15x   15x 17x 17x         17x 17x 17x         15x 15x       15x 15x             4x  
// batch.js -- A batch of tests for vows
//
// Copyright 2016 fuzzy.ai <evan@fuzzy.ai>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
/* jshint esversion: 6 */
 
'use strict'
 
const _ = require('lodash')
const async = require('async')
const debug = require('debug')('vows:batch')
 
const assert = require('./assert')
const atMostOnce = require('./at-most-once')
const Report = require('./report')
 
class Batch {
  constructor (title, definition) {
    this.title = title
    assert.isString(this.title, 'Batch title must be a string')
    assert.isObject(definition, 'Batch definition must be an object')
 
    debug(`Batch constructor with title '${this.title}'`)
    debug(`Definition for '${this.title}' has ${_.keys(definition).length} keys`)
 
    this.topic = null
    this.teardown = null
    this.tests = {}
    this.batches = {}
 
    for (const name in definition) {
      const value = definition[name]
      assert.isString(name,
        `keys of definition must be strings, not '${name}'`)
 
      if (name === 'topic') {
        debug(`Adding topic to '${this.title}'`)
        assert.isFunction(value,
          `'topic' of '${this.title}' must be a function`)
        this.topic = value
      } else if (name === 'teardown') {
        debug(`Adding teardown to '${this.title}'`)
        assert.isFunction(value,
          `'teardown' of '${this.title}' must be a function`)
        this.teardown = value
      } else if (_.isFunction(value)) {
        debug(`Adding test '${name}' to '${this.title}'`)
        this.tests[name] = value
      } else Eif (_.isObject(value)) {
        debug(`Adding batch '${name}' to '${this.title}'`)
        this.batches[name] = new Batch(`${this.title} ${name}`, value)
      } else {
        throw new Error(`Unexpected value '${value}' for key '${name}'`)
      }
    }
 
    assert.isFunction(this.topic, `Batch '${this.title}' has no topic`)
 
    assert(_.size(this.tests) > 0 || _.size(this.batches) > 0,
      `Batch '${this.title}' has no sub-batches or tests`)
 
    debug(`Batch '${this.title}' has ${_.size(this.tests)} tests`)
    debug(`Batch '${this.title}' has ${_.size(this.batches)} batches`)
 
    if (this.teardown !== null) {
      debug(`Batch '${this.title}' has a teardown`)
    } else {
      debug(`Batch '${this.title}' has no teardown`)
    }
  }
 
  run (args, callback) {
    assert.isArray(args, 'Args to Batch::run() must be an array')
    assert.isFunction(callback, 'Callback for Batch::run() must be function')
 
    debug(`Beginning run of batch '${this.title}'`)
 
    this.report = new Report(this.title)
 
    debug('Creating callback')
 
    const next = this.onTopicComplete(args, callback)
 
    let results = null
 
    // It's weird to call this.callback() synchronously, but we
    // allow it. If it's called while the topic is running,
    // we just call setImmediate() to do it "later".
 
    let sync = null
    const title = this.title
 
    const thisCallback = function () {
      const args = Array.prototype.slice.call(arguments)
      if (sync) {
        debug(`this.callback called synchronously from topic '${title}'`)
        setImmediate(() => {
          debug(`this.callback of topic '${title}' activated after setImmediate()`)
          next.apply(null, args)
        })
      } else {
        debug(`this.callback called asynchronously from topic '${title}'`)
        next.apply(null, args)
      }
    }
 
    debug(`Beginning topic of batch '${this.title}'`)
 
    try {
      sync = true
      results = this.topic.apply({callback: thisCallback}, args)
      sync = false
    } catch (err) {
      sync = false
      debug(`Error running topic of batch '${this.title}'`)
      return next(err)
    }
 
    debug(`Completed topic of batch '${this.title}'`)
 
    if (_.isUndefined(results)) {
      debug(`Results of topic of batch '${this.title}' undefined; running async`)
    } else if (!_.isUndefined(Promise) && (results instanceof Promise)) {
      debug(`Results of topic of batch '${this.title}' defined and is a Promise; resolving`)
      results
        .then(function () {
          debug(arguments.length)
          const args = Array.prototype.slice.call(arguments)
          debug(`Resolved Promise returned by topic with ${args.length} arguments`)
          debug(args)
          // We pass along a null in zero position for the error value
          next.apply(null, _.concat([null], args))
        })
        .catch((err) => {
          next(err)
        })
    } else {
      debug(`Results of topic of batch '${this.title}' defined and not a Promise; running sync`)
      next(null, results)
    }
 
    return undefined
  }
 
  onTopicComplete (args, callback) {
    return atMostOnce(function () {
      const calledWith = Array.prototype.slice.call(arguments)
      const err = calledWith[0]
      const results = calledWith.slice(1)
 
      debug(`Results for topic of '${this.title}': ${err}, ${results.join(', ')}`)
 
      this.runTests(err, results)
      this.report.successes = _.keys(this.tests).length - this.report.failures
 
      assert.ok(_.isFinite(this.report.failures), `failures must be a finite number, not ${this.report.failures}`)
      assert.ok(_.isFinite(this.report.successes), `successes must be a finite number, not ${this.report.successes}`)
      assert.equal(this.report.successes + this.report.failures, _.keys(this.tests).length, `failures + successes != number of tests`)
 
      if (err) {
        this.report.broken = 1
        debug(`Error ${err} from topic; skipping batches for '${this.title}'`)
        this.runTeardown(results, callback)
      } else if (this.report.failures > 0) {
        debug(`${this.report.failures} failures; skipping batches for '${this.title}'`)
        this.runTeardown(results, callback)
      } else {
        debug(`${this.report.failures} failures; running batches for '${this.title}'`)
        this.runSubBatches(args, results, (err) => {
          Iif (err) {
            debug(`runSubBatches for '${this.title}' returned error '${err}'`)
 
            this.runTeardown(results, callback)
          } else {
            debug(`runSubBatches for '${this.title}' complete`)
 
            this.runTeardown(results, callback)
          }
        })
      }
 
      return undefined
    }.bind(this))
  }
 
  runTeardown (topicResults, callback) {
    const next = this.onTeardownComplete(callback)
 
    if (this.teardown !== null) {
      debug(`Running teardown for '${this.title}'`)
 
      try {
        const tdres = this.teardown.apply({callback: next}, topicResults)
        if (_.isUndefined(tdres)) {
          return debug(`Results of teardown for '${this.title}' are undef; running async`)
        } else Eif (!_.isUndefined(Promise) && tdres instanceof Promise) {
          debug(`Results of teardown for '${this.title}' are defined and a Promise; resolving`)
          tdres
            .then((realTdres) => {
              next(null, realTdres)
            })
            .catch((err) => {
              next(err)
            })
        } else {
          debug(`Results of teardown for '${this.title}' are defined and not a Promise; running sync`)
          return next(null, tdres)
        }
      } catch (err) {
        debug(`Error thrown on teardown for '${this.title}': '${err}'`)
        return next(err)
      }
    } else {
      debug(`No teardown for '${this.title}'`)
 
      return next(null)
    }
  }
 
  onTeardownComplete (callback) {
    return atMostOnce(function () {
      const calledWith = Array.prototype.slice.call(arguments)
      const err = calledWith[0]
      const results = calledWith.slice(1)
 
      debug(`Teardown for '${this.title}' is complete`)
 
      Iif (err !== null) {
        debug(`Teardown for '${this.title}' called with err '${err}'`)
      }
 
      if ((results !== null) && results.length > 0) {
        debug(`Teardown for '${this.title}' called with results '${results}'`)
      }
 
      // Clear the report
 
      const report = this.report
      this.report = null
 
      return callback(null, report)
    }.bind(this))
  }
 
  runTests (err, results) {
    debug(`Running ${_.size(this.tests)} tests for batch '${this.title}'`)
 
    Eif (_.size(this.tests) > 0) {
      const args = _.concat([err], results)
 
      debug(`Passing args '${args.join(', ')}' to tests for batch '${this.title}'`)
 
      for (const name in this.tests) {
        const test = this.tests[name]
        assert.isString(name, `Name of test must be a string; '${name}'`)
        assert.isFunction(test, 'Test is not a function')
 
        debug(`Running test '${name}' for '${this.title}'`)
 
        try {
          test.apply(null, args)
          assert.ok(_.isObject(this.report))
          assert.ok(_.isObject(this.report.tests))
          this.report.tests[name] = true
 
          debug(`Finished running test '${name}' for '${this.title}'`)
        } catch (caught) {
          debug(`Error running test '${name}' for '${this.title}'`)
 
          assert.ok(_.isObject(this.report))
          assert.ok(_.isObject(this.report.tests))
          this.report.tests[name] = `${caught.name}: ${caught.message}`
          this.report.failures += 1
        }
      }
    }
  }
 
  runSubBatches (args, results, callback) {
    const batch = this
 
    assert.isArray(args, 'args for runSubBatches() must be an array')
    assert.isArray(results, 'results for runSubBatches() must be an array')
    assert.isFunction(callback, 'callback for runSubBatches() must be an array')
 
    debug(`Running ${_.size(this.batches)} batches for batch '${this.title}'`)
 
    if (_.size(this.batches) === 0) {
      return callback(null)
    } else {
      const batchArgs = _.concat(_.clone(results), args)
 
      debug(`Passing args '${batchArgs.join(', ')}' to batches for '${this.title}'`)
 
      const runBatch = (sub, callback) => {
        sub.run(batchArgs, (err, report) => {
          Iif (err) {
            debug(`Running sub-batch ${sub.title} resulted in an error`)
            batch.report.addSub(sub.title, report)
            callback(err)
          } else {
            debug(`Running sub-batch ${sub.title} succeeded`)
            batch.report.addSub(sub.title, report)
            callback(null)
          }
        })
      }
 
      return async.each(this.batches, runBatch, err => {
        Iif (err) {
          debug(`Running batches for '${this.title}' resulted in ${err}`)
          callback(err)
        } else {
          debug(`Running batches for '${this.title}' succeeded`)
          callback(null)
        }
      })
    }
  }
}
 
module.exports = Batch