Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
chore(launcher): reorganize launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
hankduan committed Oct 31, 2014
1 parent 86ead2c commit 14e9e1d
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 329 deletions.
122 changes: 122 additions & 0 deletions lib/RunnerFork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var child = require('child_process');
var q = require('q');
var TaskLogger = require('./TaskLogger.js');
var EventEmitter = require('events').EventEmitter;
var util = require('util');


/**
* A fork of a runner for running a specified task. The RunnerFork will
* start a new process that calls on '/runFromLauncher.js'.
*
* @constructor
* @param {object} task Task to run.
*/
var RunnerFork = function(configFile, additionalConfig, task, runInFork) {
this.configFile = configFile;
this.additionalConfig = additionalConfig;
this.task = task;
this.runInFork = runInFork;
};
util.inherits(RunnerFork, EventEmitter);

/**
* Sends the run command.
*/
RunnerFork.prototype.run = function() {
var self = this;

var runResults = {
taskId: this.task.taskId,
specs: this.task.specs,
capabilities: this.task.capabilities,
// The following are populated while running the test:
failedCount: 0,
exitCode: -1,
specResults: []
}

if (this.runInFork) {
var deferred = q.defer();

var childProcess = child.fork(
__dirname + '/runFromLauncher.js',
process.argv.slice(2), {
cwd: process.cwd(),
silent: true
}
);
var taskLogger = new TaskLogger(this.task, childProcess.pid);

// stdout pipe
childProcess.stdout.on('data', function(data) {
taskLogger.log(data);
});

// stderr pipe
childProcess.stderr.on('data', function(data) {
taskLogger.log(data);
});

childProcess.on('message', function(m) {
switch (m.event) {
case 'testPass':
process.stdout.write('.');
break;
case 'testFail':
process.stdout.write('F');
break;
case 'testsDone':
runResults.failedCount = m.results.failedCount;
runResults.specResults = m.results.specResults;
break;
}
})
.on('error', function(err) {
taskLogger.flush();
deferred.reject(err);
})
.on('exit', function(code) {
taskLogger.flush();
runResults.exitCode = code;
deferred.resolve(runResults);
});

childProcess.send({
command: 'run',
configFile: this.configFile,
additionalConfig: this.additionalConfig,
capabilities: this.task.capabilities,
specs: this.task.specs
});

return deferred.promise;
} else {
var ConfigParser = require('./configParser');
var configParser = new ConfigParser();
if (this.configFile) {
configParser.addFileConfig(this.configFile);
}
if (this.additionalConfig) {
configParser.addConfig(this.additionalConfig);
}
var config = configParser.getConfig();
config.capabilities = this.task.capabilities;
config.specs = this.task.specs;

var Runner = require('./runner');
var runner = new Runner(config);

runner.on('testsDone', function(results) {
runResults.failedCount = results.failedCount;
runResults.specResults = results.specResults;
})

return runner.run().then(function(exitCode) {
runResults.exitCode = exitCode;
return runResults;
});
}
};

module.exports = RunnerFork;
88 changes: 88 additions & 0 deletions lib/TaskLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var EOL = require('os').EOL;

/**
* Log output such that metadata are appended.
* Calling log(data) will not flush to console until you call flush()
*
* @constructor
* @param {object} task Task that is being reported.
* @param {number} pid PID of process running the task.
*/
var TaskLogger = function(task, pid) {
this.task = task;
this.pid = pid;
this.buffer = '';
this.insertTag = true;

this.logHeader_();
};

/**
* Report the header for the current task including information such as
* PID, browser name/version, task Id, specs being run.
*
* @private
*/
TaskLogger.prototype.logHeader_ = function() {
var output = 'PID: ' + this.pid + EOL;
if (this.task.specs.length === 1) {
output += 'Specs: '+ this.task.specs.toString() + EOL + EOL;
}
this.log(output);
};


/**
* Flushes the buffer to stdout.
*/
TaskLogger.prototype.flush = function() {
if (this.buffer) {
// Flush buffer if nonempty
process.stdout.write(EOL + '------------------------------------' + EOL);
process.stdout.write(this.buffer);
process.stdout.write(EOL);
this.buffer = '';
}
};

/**
* Log the data in the argument. The data will be saved to a buffer
* until flush() is called.
*
* @param {string} data
*/
TaskLogger.prototype.log = function(data) {
var tag = '[';
var capabilities = this.task.capabilities;
tag += (capabilities.browserName) ?
capabilities.browserName : '';
tag += (capabilities.version) ?
(' ' + capabilities.version) : '';
tag += (capabilities.platform) ?
(' ' + capabilities.platform) : '';
tag += (' #' + this.task.taskId);
tag += '] ';

data = data.toString();
for ( var i = 0; i < data.length; i++ ) {
if (this.insertTag) {
this.insertTag = false;
// This ensures that the '\x1B[0m' appears before the tag, so that
// data remains correct when color is not processed.
// See https://github.com/angular/protractor/pull/1216
if (data[i] === '\x1B' && data.substring(i, i+4) === '\x1B[0m' ) {
this.buffer += ('\x1B[0m' + tag);
i += 3;
continue;
}

this.buffer += tag;
}
if (data[i] === '\n') {
this.insertTag = true;
}
this.buffer += data[i];
}
};

module.exports = TaskLogger;
1 change: 1 addition & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var optimist = require('optimist').
describe('stackTrace', 'Print stack trace on error').
describe('params', 'Param object to be passed to the tests').
describe('framework', 'Test framework to use: jasmine, cucumber or mocha').
describe('testResultOutput', 'Path to save JSON test result').
alias('browser', 'capabilities.browserName').
alias('name', 'capabilities.name').
alias('platform', 'capabilities.platform').
Expand Down
24 changes: 22 additions & 2 deletions lib/frameworks/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@ exports.run = function(runner, specs) {
require('jasminewd');
/* global jasmine */

testResult = [];

var RunnerReporter = function(emitter) {
this.emitter = emitter;
};

RunnerReporter.prototype.reportRunnerStarting = function() {};
RunnerReporter.prototype.reportRunnerResults = function() {};
RunnerReporter.prototype.reportSuiteResults = function() {};
RunnerReporter.prototype.reportSpecStarting = function() {};
RunnerReporter.prototype.reportSpecStarting = function() {
this.startTime = new Date();
};
RunnerReporter.prototype.reportSpecResults = function(spec) {
if (spec.results().passed()) {
this.emitter.emit('testPass');
} else {
this.emitter.emit('testFail');
}

var entry = {
result: [],
duration: new Date().getTime() - this.startTime.getTime()
};
spec.results().getItems().forEach(function(item) {
entry.result.push({
passed: item.passed(),
errorMsg: item.message,
stacktrace: item.trace.stack
});
});
testResult.push(entry);
};
RunnerReporter.prototype.log = function() {};

Expand All @@ -47,7 +64,10 @@ exports.run = function(runner, specs) {
if (originalOnComplete) {
originalOnComplete(jasmineRunner, log);
}
resolve(jasmineRunner.results());
resolve({
failedCount: jasmineRunner.results().failedCount,
specResults: [] //TODO
});
} catch(err) {
reject(err);
}
Expand Down
Loading

0 comments on commit 14e9e1d

Please sign in to comment.