Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #57 from yzen/bug-1132055
Browse files Browse the repository at this point in the history
Bug 1132055 - added support for --marionette-capabilities for marionette...
  • Loading branch information
lightsofapollo committed Feb 11, 2015
2 parents 3f95c18 + 4e209f7 commit 13608a6
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 17 deletions.
20 changes: 20 additions & 0 deletions bin/marionette-mocha
Expand Up @@ -31,6 +31,18 @@ function requireReporter(path) {
}
}

/**
* Helper to import the marionette desired capabilities
*/
function importDesiredCapabilities(path) {
try {
return require(path);
} catch (e) {
// Can not parse marionette desired capabilities file.
return;
}
}

/**
Convert this host log argument into a stream (not done as a type to avoid
destructive operations when calling --help)
Expand Down Expand Up @@ -108,6 +120,12 @@ parser.addArgument(['--mocha-flags'], {
action: 'storeTrue'
});

parser.addArgument(['--marionette-capabilities'], {
type: importDesiredCapabilities,
dest: 'desiredCapabilities',
help: 'Path tp marionette desired capabilities file (*.json)'
});

var argv = ['test/', 'tests/', '.'].reduce(function(argv, path) {
var optsFile = fsPath.join(path, OPTS_FILE);
if (!fs.existsSync(optsFile)) {
Expand Down Expand Up @@ -192,6 +210,8 @@ function main(argv) {

profileBase: args[0].profileBase,

desiredCapabilities: args[0].desiredCapabilities,

hostLog: hostLogStream(args[0].hostLog),

verbose: args[0].verbose
Expand Down
23 changes: 19 additions & 4 deletions lib/childrunner.js
Expand Up @@ -56,6 +56,9 @@ function ChildRunner(options) {
// base details used in every profile
this.profileBase = options.profileBase;

// optional marionette desired capabilities
this.desiredCapabilities = options.desiredCapabilities;

this._nextRemoteId = 1;
this._verbose = !!options.verbose;
this.remotes = {};
Expand Down Expand Up @@ -414,6 +417,15 @@ ChildRunner.prototype = {
process.nextTick(next);
},

/**
* Encode the data in base64 + json
* @param {JSON} data Data to be encoded.
* @return {String} base64 encoded string
*/
_encode: function(data) {
return new Buffer(JSON.stringify(data)).toString('base64');
},

/**
* Spawn the process for the mocha child runner.
*/
Expand All @@ -423,10 +435,8 @@ ChildRunner.prototype = {
['--reporter', PROXY_BINARY]
);

// encode the metadata in base64 + json
var metadata = new Buffer(
JSON.stringify(this.host.constructor.metadata)
).toString('base64');
// encode the metadata
var metadata = this._encode(this.host.constructor.metadata);

// pass all environment variables to the child...
var env = {};
Expand All @@ -444,6 +454,11 @@ ChildRunner.prototype = {
// turn on the fork options so we get ipc messages.
options.env[reporter.FORK_ENV] = 1;

if (this.desiredCapabilities) {
// Encode and pass desired capabilities to the child
options.env.DESIRED_CAPABILITIES = this._encode(this.desiredCapabilities);
}

this.process = fork(MOCHA_BINARY, argv, options);
this.runner = new Consumer(this.process);

Expand Down
3 changes: 2 additions & 1 deletion lib/parentrunner.js
Expand Up @@ -65,7 +65,8 @@ ParentRunner.prototype = {
profileBuilder: options.profileBuilder,
profileBase: options.profileBase,
verbose: options.verbose,
hostLog: options.hostLog
hostLog: options.hostLog,
desiredCapabilities: options.desiredCapabilities
});

// keep track of all children- mostly for future use.
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/hostmanager.js
Expand Up @@ -42,6 +42,7 @@ HostManager.prototype = {
createHost: function(profile, driver, desiredCapabilities) {
profile = profile || {};
driver = driver || Marionette.Drivers.TcpSync;
desiredCapabilities = desiredCapabilities || this.desiredCapabilities;

debug('create host', profile);

Expand Down
23 changes: 11 additions & 12 deletions lib/ui.js
Expand Up @@ -14,30 +14,28 @@ var Suite = require('mocha/lib/suite'),
utils = require('mocha/lib/utils');

/**
* Internal method designed to attempt to find the metadata for this child
* process based on the environment variable CHILD_METADATA.
* Internal method designed to attempt to find the env data for this child
* process based on the environment variable.
*
* The environment variable is expected to be a base64 encoded string which can
* be parsed as JSON.
*
* @param {String} name environment variable name.
*
* @private
* @return {Object}
*/
function findMetadata() {
if (!process.env.CHILD_METADATA)
function decode(name) {
var data = process.env[name];
if (!data) {
return {};

}
try {
return JSON.parse(
new Buffer(process.env.CHILD_METADATA, 'base64').toString()
);
return JSON.parse(new Buffer(data, 'base64').toString());
} catch (e) {
console.error('could not parse CHILD_METADATA');
console.error('could not parse ' + name);
return {};
}

return result;
}

/**
Expand Down Expand Up @@ -101,9 +99,10 @@ module.exports = function(suite) {
*
* @type {Object}
*/
context.marionette.metadata = findMetadata();
context.marionette.metadata = decode('CHILD_METADATA');

// Setup global state manager for the marionette runtime.
manager.desiredCapabilities = decode('DESIRED_CAPABILITIES');
context.marionette._manager = manager;
context.marionette.client = manager.createHost.bind(manager);
context.marionette.plugin = manager.addPlugin.bind(manager);
Expand Down
3 changes: 3 additions & 0 deletions test/bin/fixtures/capabilities.json
@@ -0,0 +1,3 @@
{
"desiredCapability": true
}
20 changes: 20 additions & 0 deletions test/bin/marionette-mocha.js
Expand Up @@ -143,6 +143,26 @@ suite('mocha integration', function() {
});
});

suite('--marionette-capabilities', function() {
var result,
capabilitiesPath = __dirname + '/fixtures/capabilities.json',
expected = JSON.stringify({ capabilities: require(capabilitiesPath) }),
argv = [
'--marionette-capabilities', capabilitiesPath,
'--host-log', 'stdout',
__dirname + '/fixtures/marionettetest'
];

setup(function(done) {
var proc = spawnMarionette(argv);
result = waitForProcess(proc, done);
});

test('desired capabilities are set', function() {
assert.ok(result.stdoutRaw.indexOf(expected) !== -1);
});
});

suite('--host-log=stdout', function() {
var result,
argv = [
Expand Down

0 comments on commit 13608a6

Please sign in to comment.