buster-ci-agent
Advanced tools
Comparing version 0.1.0 to 0.1.1
109
lib/agent.js
@@ -7,16 +7,6 @@ "use strict"; | ||
formatio = require("formatio"), | ||
logger = require("evented-logger"); | ||
logger = require("evented-logger"), | ||
closeWindows = require("./close-windows"); | ||
function createReporter(out, err) { | ||
return { | ||
log: out, | ||
info: out, | ||
debug: out, | ||
warn: err, | ||
error: err | ||
}; | ||
} | ||
function processLog(error, stdout, stderr) { | ||
@@ -40,3 +30,59 @@ /*jshint validthis: true */ | ||
function startBrowser(browserName, browser, url, id) { | ||
/*jshint validthis: true */ | ||
if (browser.prepareStart) { | ||
this._logger.info("prepare start"); | ||
var prepareStartCommand = browser.prepareStart; | ||
this._logger.debug(prepareStartCommand); | ||
cp.exec( | ||
prepareStartCommand, | ||
processLog.bind(this) | ||
); | ||
} | ||
var startCommand = browser.start; | ||
var startArgs = browser.startArgs; | ||
startArgs = startArgs ? startArgs.slice(0) : []; | ||
if (url) { | ||
url = id ? url + "?id=" + id : url; | ||
startArgs.push(url); | ||
} | ||
this._logger.info("start browser " + browserName); | ||
var process = cp.spawn(startCommand, startArgs); | ||
process.stdout.on('data', this._logger.debug); | ||
process.stderr.on('data', this._logger.error); | ||
process.on('close', browserClosedLog.bind(this, browserName)); | ||
browser.process = process; | ||
} | ||
function stopBrowser(browserName, browser) { | ||
/*jshint validthis: true */ | ||
if (browser.stop) { | ||
if (browser.stop.command) { | ||
this._logger.info("stop browser " + browserName + " by command"); | ||
var command = browser.stop.command.replace(/\$\{PID\}/g, | ||
browser.process.pid | ||
); | ||
this._logger.debug(command); | ||
cp.exec(command, processLog.bind(this)); | ||
} | ||
if (browser.stop.windowTitle) { | ||
this._logger.info("stop browser " + browserName | ||
+ " by closing window"); | ||
try { | ||
closeWindows(browser.stop.windowTitle); | ||
} catch (err) { | ||
this._logger.error(err.message); | ||
} | ||
} | ||
} else { | ||
this._logger.info("stop browser " + browserName); | ||
browser.process.kill(); | ||
} | ||
delete browser.process; | ||
} | ||
function Agent(config) { | ||
@@ -101,32 +147,8 @@ this._config = config; | ||
if (request.browsers.hasOwnProperty(browser)) { | ||
if (this._config.browsers[browser].prepareStart) { | ||
this._logger.info("prepare start"); | ||
this._logger.debug( | ||
this._config.browsers[browser].prepareStart | ||
); | ||
var preStartProc = cp.exec( | ||
this._config.browsers[browser].prepareStart, | ||
processLog.bind(this) | ||
); | ||
} | ||
var args = this._config.browsers[browser].startArgs; | ||
args = args ? args.slice(0) : []; | ||
var id = request.browsers[browser].id; | ||
if (request.url) { | ||
var url = id | ||
? request.url + "?id=" + id : request.url; | ||
args.push(url); | ||
} | ||
this._logger.info("start browser " + browser); | ||
var process = cp.spawn( | ||
this._config.browsers[browser].start, | ||
args | ||
startBrowser.call(this, | ||
browser, | ||
this._config.browsers[browser], | ||
request.url, | ||
request.browsers[browser].id | ||
); | ||
process.stdout.on('data', this._logger.debug); | ||
process.stderr.on('data', this._logger.error); | ||
process.on('close', browserClosedLog.bind(this, browser)); | ||
this._config.browsers[browser].process = process; | ||
} | ||
@@ -138,5 +160,4 @@ } | ||
if (request.browsers.hasOwnProperty(browser)) { | ||
this._logger.info("stop browser " + browser); | ||
this._config.browsers[browser].process.kill(); | ||
delete this._config.browsers[browser].process; | ||
stopBrowser.call(this, | ||
browser, this._config.browsers[browser]); | ||
} | ||
@@ -143,0 +164,0 @@ } |
{ | ||
"name": "buster-ci-agent", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Starts, captures and stops browsers for buster-ci.", | ||
@@ -32,2 +32,6 @@ "bugs": { | ||
}, | ||
"optionalDependencies": { | ||
"ffi": "~1.2", | ||
"ref": "~0.3" | ||
}, | ||
"devDependencies": { | ||
@@ -34,0 +38,0 @@ "buster": "*", |
@@ -8,1 +8,9 @@ # buster-ci-agent | ||
locally and remote. | ||
## Changelog | ||
**0.1.1** (21.10.2014) | ||
* browser can now be killed via configured command | ||
* browser can now be closed via native window api (only on Windows yet) |
@@ -8,5 +8,7 @@ "use strict"; | ||
childProcessStub = {}, | ||
closeWindowsStub = buster.sinon.stub(), | ||
Agent = proxyquire("../lib/agent.js", { | ||
"http": httpStub, | ||
"child_process": childProcessStub | ||
"child_process": childProcessStub, | ||
"./close-windows": closeWindowsStub | ||
}), | ||
@@ -265,6 +267,119 @@ | ||
assert.calledWithExactly(processChrome.kill); | ||
assert.calledWithExactly(processIE.kill); | ||
assert.called(processChrome.kill); | ||
assert.called(processIE.kill); | ||
}, | ||
"kills specified browsers by provided command": function () { | ||
var config = { | ||
port: 8888, | ||
browsers: { | ||
"IE": { | ||
start: "iexplore", | ||
stop: { | ||
command: "commandToStop" | ||
} | ||
} | ||
}, | ||
logLevel: 0 | ||
}; | ||
var processIE = Object.create(this.processStub); | ||
childProcessStub.spawn.withArgs(config.browsers.IE.start) | ||
.returns(processIE); | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
command: "start", | ||
browsers: { | ||
"IE": {} | ||
} | ||
}); | ||
this.agent.handleRequest({ | ||
command: "stop", | ||
browsers: { | ||
"IE": {} | ||
} | ||
}); | ||
refute.called(processIE.kill); | ||
assert.calledWith(childProcessStub.exec, "commandToStop"); | ||
}, | ||
"replaces place holder for pid in provided command": function () { | ||
var config = { | ||
port: 8888, | ||
browsers: { | ||
"IE": { | ||
start: "iexplore", | ||
stop: { | ||
command: "commandToStop ${PID}" | ||
} | ||
} | ||
}, | ||
logLevel: 0 | ||
}; | ||
var processIE = Object.create(this.processStub); | ||
processIE.pid = 1234; | ||
childProcessStub.spawn.withArgs(config.browsers.IE.start) | ||
.returns(processIE); | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
command: "start", | ||
browsers: { | ||
"IE": {} | ||
} | ||
}); | ||
this.agent.handleRequest({ | ||
command: "stop", | ||
browsers: { | ||
"IE": {} | ||
} | ||
}); | ||
refute.called(processIE.kill); | ||
assert.calledWith(childProcessStub.exec, "commandToStop 1234"); | ||
}, | ||
"kills browsers by provided window title": function () { | ||
var config = { | ||
port: 8888, | ||
browsers: { | ||
"IE": { | ||
start: "iexplore", | ||
stop: { | ||
windowTitle: "Buster - Windows Internet Explorer" | ||
} | ||
} | ||
}, | ||
logLevel: 0 | ||
}; | ||
var processIE = Object.create(this.processStub); | ||
processIE.pid = 1234; | ||
childProcessStub.spawn.withArgs(config.browsers.IE.start) | ||
.returns(processIE); | ||
this.agent = new Agent(config); | ||
this.agent.handleRequest({ | ||
command: "start", | ||
browsers: { | ||
"IE": {} | ||
} | ||
}); | ||
this.agent.handleRequest({ | ||
command: "stop", | ||
browsers: { | ||
"IE": {} | ||
} | ||
}); | ||
refute.called(processIE.kill); | ||
assert.calledWith( | ||
closeWindowsStub, | ||
config.browsers.IE.stop.windowTitle | ||
); | ||
} | ||
} | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23484
14
617
16
5