This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1939 from Gozala/gcli-wrapper
Bug 1152126 - Implement a wrapper for gcli. r=@jsantell
- Loading branch information
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |