Code coverage report for lib/cmdt.js

Statements: 100% (78 / 78)      Branches: 100% (18 / 18)      Functions: 100% (17 / 17)      Lines: 100% (78 / 78)      Ignored: none     

All files » lib/ » cmdt.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 153 154 155 156 157 158 159 160 161 162 163 164 165 1661 1 1 1 1 1 1 1 1 1 1 1 1 1         1   10 10 10 10 10               1 1                 1   4 4   4 4   1 6 5 5       1 6 6 1   5         1   7   7   7 1     6 6 6         4 4 2 2 1     4                       1   6   6 6   1   1 1   1             1   1 1   1 1 1   1 1 1       6                   1   3   3   2 2   2 1   1   2       1
var async    = require('async');
var checker  = require('./checker');
var child    = require('child_process');
var colors   = require('colors');
var fs       = require('fs');
var fsx      = require('fs-extra');
var mkdirp   = require('mkdirp');
var ncp      = require('ncp');
var os       = require('os');
var p        = require('path');
var reporter = require('./reporters/console');
var rimraf   = require('rimraf');
var test     = require('./test');
var util     = require('util');
 
/**
 * class Cmdt
 */
function Cmdt(opts) {
 
  this.opts         = opts || {};
  this.opts.baseDir = this.opts.baseDir || os.tmpdir();
  this.opts.debug   = this.opts.debug || false;
  this.runId        = 'cmdt-' + new Date().getTime() + '-' + process.pid;
  this._execData    = {};
}
 
/**
 * Create example cmdt test files.
 *
 * @param {Function} cb: standard cb(err, result) callback
 */
Cmdt.prototype.init = function (cb) {
  ncp.ncp(p.join(__dirname, '../examples'), '.', cb);
};
 
/**
 * Parse .cmdt test files and run all tests.
 *
 * @param {Array} files: test files to run
 * @param {Function} cb: standard cb(err, result) callback
 */
Cmdt.prototype.run = function (files, cb) {
 
  var runDir = p.join(this.opts.baseDir, this.runId);
  var self   = this;
 
  reporter.emit('dir', this.opts.debug, runDir);
  mkdirp.sync(runDir);
 
  function copyFixture(testDir) {
    return function (fixturePath, cb) {
      var destPath = fs.lstatSync(fixturePath).isDirectory() ? testDir : p.join(testDir, p.basename(fixturePath));
      fsx.copy(fixturePath, destPath, cb);
    };
  }
 
  function execTests(tests, testDir, cb) {
    return function (err) {
      if (err) {
        cb(err);
      } else {
        self._exec(tests, testDir, cb);
      }
    };
  }
 
  function iter(file, cb) {
 
    reporter.emit('segment', file);
    
    test.load(file, function (err, tests, fixtures) {
 
      if (err) {
        cb(err);
 
      } else {
        var testDir = p.join(self.opts.baseDir, self.runId, file);
        mkdirp.sync(testDir);
        async.each(fixtures, copyFixture(testDir), execTests(tests, testDir, cb));
      }
    });
  }
 
  async.eachSeries(files, iter, function (err) {
    if (!err) {
      reporter.emit('end', self.opts.debug);
      if (!self.opts.debug) {
        rimraf.sync(runDir);
      }
    }
    cb(err, { successes: reporter.successes, failures: reporter.failures });
  });
};
 
/**
 * Execute test command and validate output of each test.
 * Commands will be executed in a sandbox directory.
 *
 * @param {Array} tests: test settings
 * @param {String} testDir: test execution directory
 * @param {Function} cb: standard cb(err, result) callback
 */
Cmdt.prototype._exec = function (tests, testDir, cb) {
 
  this._execData = {};
 
  var self   = this;
  var testId = 0; // unique ID for each test within the file
 
  function processTest(test, cb) {
 
    test.dir    = testDir;
    test.execId = util.format('%d-%s', testId++, test.file);
 
    self._execData[test.execId] = {
      exitcode: undefined,
      output  : '',
      stdout  : '',
      stderr  : ''
    };
 
    var _exec = child.exec(test.command, { cwd: test.dir }, self._testCb(test, cb));
 
    _exec.on('exit', function (code) {
      self._execData[test.execId].exitcode = code;
    });
    _exec.stdout.on('data', function (data) {
      self._execData[test.execId].output += data;
      self._execData[test.execId].stdout += data;
    });
    _exec.stderr.on('data', function (data) {
      self._execData[test.execId].output += data;
      self._execData[test.execId].stderr += data;
    });
  }
  
  async.eachSeries(tests, processTest, cb);
};
 
/**
 * Create a callback function for processing each test.
 * This callback emits event to reporter depending on the existence of check error(s) .
 *
 * @param {Object} test: test setting
 * @param {Function} cb: standard cb(err, result) callback
 */
Cmdt.prototype._testCb = function (test, cb) {
 
  var self = this;
 
  return function (err) {
 
    var result = self._execData[test.execId];
    var errors = checker.check(result, test);
 
    if (errors.length === 0) {
      reporter.emit('success', test, result);
    } else {
      reporter.emit('failure', errors, test, result);
    }
    cb();
  };
};
 
module.exports = Cmdt;