Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1939 from Gozala/gcli-wrapper
Browse files Browse the repository at this point in the history
Bug 1152126 - Implement a wrapper for gcli. r=@jsantell
  • Loading branch information
Gozala committed Apr 22, 2015
2 parents 26182ea + dd37c3d commit 0ad61f1
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/dev/gcli.js
@@ -0,0 +1,55 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { Cu } = require("chrome");
const { CommandUtils } = require("resource:///modules/devtools/DeveloperToolbar.jsm");
const { devtools } = require("resource://gre/modules/devtools/Loader.jsm");
const { gcli } = require("resource://gre/modules/devtools/gcli.jsm");

const { getActiveTab } = require("sdk/tabs/utils");
const { getMostRecentBrowserWindow } = require("sdk/window/utils");
const { Conversion, Status } = devtools.require("gcli/types/types");

const targetFor = ({target, tab, window}) =>
target ? target :
tab ? devtools.TargetFactory.forTab(tab) :
window ? devtools.TargetFactory.forTab(getActiveTab(window)) :
devtools.TargetFactory.forTab(getActiveTab(getMostRecentBrowserWindow()))
exports.targetFor = targetFor

const environmentFor = options =>
options.environment ? options.environment :
CommandUtils.createEnvironment({target: targetFor(options)})
exports.environmentFor = environmentFor


const run = (command, options={}) => {
const environment = environmentFor(options)
return CommandUtils.createRequisition(environment).then(requisition => {
return requisition.updateExec(command).then(({error, data}) => {
if (error) {
throw data
}
return data
})
})
}
exports.run = run

const isUpperCase = text => text.toUpperCase() === text

const install = (...items) => {
gcli.addItems(items.map(item => isUpperCase(item.name.charAt(0)) ?
Object.assign(item, {item: "type"}) :
item))
}
exports.install = install

const uninstall = (...items) => gcli.removeItems(items)
exports.uninstall = uninstall

exports.Conversion = Conversion
exports.Status = Status
68 changes: 68 additions & 0 deletions test/test-gcli.js
@@ -0,0 +1,68 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const {run, install, uninstall, Conversion, Status} = require("dev/gcli");

exports["test gcli run"] = function*(assert) {
const result = yield run("help");
assert.ok(Array.isArray(result.commands),
"help returns array of commands");
};

exports["test gcli failed run"] = function*(assert) {
try {
const result = yield run("hlep");
assert.fail("should have rejected promise");
} catch (error) {
assert.ok(error);
assert.equal(error.message, "Invalid Command");
}
};

exports["test install / uninstall command"] = function*(assert) {
const command = {
name: "echo",
description: "Returns same text back",
params: [{name: "text",
type: "string",
description: "text to echo"}],
exec({text}) {
return text;
}
};

install(command);

const r1 = yield run("help echo");

assert.ok(r1.command, "returns command");
assert.equal(r1.command.description, command.description);
assert.equal(r1.command.name, command.name);
assert.equal(r1.command.params.length, command.params.length);
assert.equal(r1.command.params[0].name, command.params[0].name);
assert.equal(r1.command.params[0].description, command.params[0].description);

try {
const r2 = yield run("echo");
assert.fail("should have errored");
} catch(error) {
assert.equal(error.message, "Value required for 'text'.");
}

const r3 = yield run("echo Hi");

assert.equal(r3, "Hi")

uninstall(command);

try {
const r4 = yield run("echo Bye");
assert.fail("should have errored");
} catch(error) {
assert.equal(error.message, "Invalid Command");
}
};

require("sdk/test").run(exports);

0 comments on commit 0ad61f1

Please sign in to comment.