From 5aee11127d714530fab4e79d5b7c83b7d7598ef8 Mon Sep 17 00:00:00 2001 From: James Lal Date: Sat, 5 Oct 2013 13:04:08 -0400 Subject: [PATCH 1/5] initial package.json --- README.md | 2 +- package.json | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 package.json diff --git a/README.md b/README.md index b1c162c..89d04be 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ firefox-launcher ================ -test-agent firefox launcher +test-agent firefox launcher. diff --git a/package.json b/package.json new file mode 100644 index 0000000..318e883 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "firefox-launcher", + "version": "0.0.0", + "description": "firefox-launcher ================", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/test-agent/firefox-launcher.git" + }, + "author": "", + "license": "Apache2" +} From f7fa0b9384d4618f3b6ee97c8f8003911e89f598 Mon Sep 17 00:00:00 2001 From: James Lal Date: Sat, 5 Oct 2013 13:04:48 -0400 Subject: [PATCH 2/5] git ignore --- .gitignore | 1 + package.json | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/package.json b/package.json index 318e883..bd2cd7c 100644 --- a/package.json +++ b/package.json @@ -11,5 +11,9 @@ "url": "https://github.com/test-agent/firefox-launcher.git" }, "author": "", - "license": "Apache2" + "license": "Apache2", + "dependencies": { + "mozilla-runner": "0.0.1", + "ws": "~0.4.31" + } } From 3b1b1cee036393a1f8b1d325ae57fecef46a46f3 Mon Sep 17 00:00:00 2001 From: James Lal Date: Sat, 5 Oct 2013 13:11:52 -0400 Subject: [PATCH 3/5] add a firefox make target for testing --- .gitignore | 1 + Makefile | 2 ++ package.json | 5 +++++ 3 files changed, 8 insertions(+) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 3c3629e..b7b7c40 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +firefox diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3561729 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +firefox: + mozilla-download --product firefox firefox diff --git a/package.json b/package.json index bd2cd7c..cbe1ba3 100644 --- a/package.json +++ b/package.json @@ -15,5 +15,10 @@ "dependencies": { "mozilla-runner": "0.0.1", "ws": "~0.4.31" + }, + "devDependencies": { + "mocha": "~1.13.0", + "node-static": "~0.7.1", + "mozilla-download": "~0.2.1" } } From b4c967b49981b71968805cf84aa23b189ef5db67 Mon Sep 17 00:00:00 2001 From: James Lal Date: Sun, 6 Oct 2013 14:58:53 -0400 Subject: [PATCH 4/5] Bug 923816 - Initial firefox launcher executable --- HISTORY.md | 3 + Makefile | 5 ++ README.md | 21 +++++- bin/test-agent-firefox-launcher | 67 ++++++++++++++++++++ index.js | 1 + package.json | 15 +++-- test/bin/test-agent-firefox-launcher_test.js | 56 ++++++++++++++++ test/fixtures/index.html | 10 +++ test/fixtures/websocket.js | 5 ++ test/helper.js | 27 ++++++++ test/mocha.opts | 2 + 11 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 HISTORY.md create mode 100755 bin/test-agent-firefox-launcher create mode 100644 index.js create mode 100644 test/bin/test-agent-firefox-launcher_test.js create mode 100644 test/fixtures/index.html create mode 100644 test/fixtures/websocket.js create mode 100644 test/helper.js create mode 100644 test/mocha.opts diff --git a/HISTORY.md b/HISTORY.md new file mode 100644 index 0000000..fc21344 --- /dev/null +++ b/HISTORY.md @@ -0,0 +1,3 @@ +# 0.0.1 +- Initial release with very basic firefox launching +- Each launch gets a clean profile diff --git a/Makefile b/Makefile index 3561729..36b64c2 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,7 @@ +default: test firefox: mozilla-download --product firefox firefox + +.PHONY: test +test: + ./node_modules/.bin/mocha $(shell find . -name "*_test.js") diff --git a/README.md b/README.md index 89d04be..1daa9ef 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,19 @@ -firefox-launcher -================ +# Test Agent Firefox Launcher -test-agent firefox launcher. +The intent is this module is never used directly as it is very low level +guts for launching firefox with a clean profile each time... + +This should be used in conjunction with (test-agent)[https://github.com/test-agent/test-agent] + +## Usage + +(there is no node interface) + +```sh +# if you installed with npm install -g +EXEC=test-agent-firefox-launcher +# if you installed with npm install +EXEC=./node_modules/.bin/test-aget-firefox-launcher + +$EXEC --exec /path/to/firefox/folder $URL +``` diff --git a/bin/test-agent-firefox-launcher b/bin/test-agent-firefox-launcher new file mode 100755 index 0000000..777641a --- /dev/null +++ b/bin/test-agent-firefox-launcher @@ -0,0 +1,67 @@ +#! /usr/bin/env node +/** +TestAgent firefox launcher plugin executable. Handles the details of launching +the firefox instance. + +Why would we have a binary that wraps the launch of other binaries??? + + - auto detection of exectuable firefox + - better control of logging + - unified? spot where we can alter how the binary is launched for firefox + +*/ + +var program = require('commander'), + mozrunner = require('mozilla-runner'), + mozprofile = require('mozilla-profile-builder'), + fs = require('fs'); + +program. + option( + '--exec-name ', + 'Name of executable to launch (like b2g-bin or firefox)', + 'firefox' + ). + option('--exec ', 'Path to the firefox executable'). + option('--profile ', 'path to base profile'). + parse(process.argv); + +if (!program.exec) { + console.error('must pass an --exec flag') + program.outputHelp(); + process.exit(1); +} + +if (!fs.existsSync(program.exec)) { + console.error( + '--exec must be a path on the file system (maybe you need to use which?)' + ); + program.outputHelp(); + process.exit(1); +} + +// options for the run +var profileOptions = {}; +var runnerOptions = { argv: program.args }; + +/** +Utility for managing the death of the parent (current) process. +*/ +function handleFirefoxProcess(proc) { + process.once('SIGTERM', proc.kill.bind(proc)); +} + +mozprofile.create(profileOptions, function(err, instance) { + if (err) throw err; + + // set the profile path + runnerOptions.profile = instance.path; + mozrunner.run( + program.execName, + program.exec, runnerOptions, + function(err, child) { + if (err) throw err; + handleFirefoxProcess(child); + } + ); +}); diff --git a/index.js b/index.js new file mode 100644 index 0000000..2b392a0 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +throw new Error('no public node interface'); diff --git a/package.json b/package.json index cbe1ba3..40805d3 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,13 @@ { - "name": "firefox-launcher", - "version": "0.0.0", - "description": "firefox-launcher ================", + "name": "test-agent-firefox-launcher", + "version": "0.0.1", + "description": "Test agent firefox launcher executable", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "make test" + }, + "bin": { + "test-agent-firefox-launcher": "./bin/test-agent-firefox-launcher" }, "repository": { "type": "git", @@ -14,7 +17,9 @@ "license": "Apache2", "dependencies": { "mozilla-runner": "0.0.1", - "ws": "~0.4.31" + "ws": "~0.4.31", + "commander": "~2.0.0", + "mozilla-profile-builder": "~0.3.0" }, "devDependencies": { "mocha": "~1.13.0", diff --git a/test/bin/test-agent-firefox-launcher_test.js b/test/bin/test-agent-firefox-launcher_test.js new file mode 100644 index 0000000..a847566 --- /dev/null +++ b/test/bin/test-agent-firefox-launcher_test.js @@ -0,0 +1,56 @@ +suite('test-agent-firefox-launcher', function() { + var spawn = require('child_process').spawn, + executable = __dirname + '/../../bin/test-agent-firefox-launcher', + firefox = __dirname + '/../../firefox', + // this also spawns the server! + url = testServer(), + WebSocketServer = require('ws').Server; + + function launch(args) { + var proc = spawn(executable, args); + if (process.env.DEBUG) { + proc.stdout.pipe(process.stdout); + proc.stderr.pipe(process.stderr); + } + return proc; + } + + function verifyNonZeroExit(proc, done) { + proc.on('exit', function(status) { + assert(status !== 0, 'should exit process with a status other than 0'); + done(); + }); + } + + test('failure - no exec argument', function(done) { + var proc = launch(); + verifyNonZeroExit(proc, done); + }); + + test('failure - missing firefox', function(done) { + var proc = launch(['--exec', '/iam/not/heere']); + verifyNonZeroExit(proc, done); + }); + + suite('success - launch', function() { + var wsServer, proc; + setup(function() { + wsServer = new WebSocketServer({ port: 60001 }); + }); + + teardown(function() { + wsServer.close(); + }); + + test('client connects to ws server', function(done) { + proc = launch(['--exec', firefox, url]); + wsServer.on('connection', function() { + proc.kill(); + proc.on('exit', function() { + done(); + }); + }); + }); + }); + +}); diff --git a/test/fixtures/index.html b/test/fixtures/index.html new file mode 100644 index 0000000..e16859e --- /dev/null +++ b/test/fixtures/index.html @@ -0,0 +1,10 @@ + + + + + Head + + + + + diff --git a/test/fixtures/websocket.js b/test/fixtures/websocket.js new file mode 100644 index 0000000..afbe760 --- /dev/null +++ b/test/fixtures/websocket.js @@ -0,0 +1,5 @@ +(function() { + // all we need to do is verify that the socket is opened that is + // enough to tell us that the plugin works.... + new WebSocket('ws://localhost:60001'); +}()); diff --git a/test/helper.js b/test/helper.js new file mode 100644 index 0000000..16869d6 --- /dev/null +++ b/test/helper.js @@ -0,0 +1,27 @@ +global.assert = require('assert'); +var TEST_PORT = 8787; + +global.testServer = function() { + // reference to the sever must be at this function scope so + // suiteTeardown can actually close it. + var server; + + suiteSetup(function() { + var static = require('node-static'); + // root of the project + var file = new static.Server(__dirname + '/fixtures/'); + + server = require('http').createServer(function(request, response) { + request.addListener('end', function() { + // Serve files! + file.serve(request, response); + }).resume(); + }).listen(8787); + }); + + suiteTeardown(function() { + server.close(); + }); + + return 'http://localhost:' + TEST_PORT + '/index.html'; +}; diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..a61c3e1 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,2 @@ +--require test/helper +--ui tdd From e5056bb7ea54689bcad2108876e390e2fd41b1e6 Mon Sep 17 00:00:00 2001 From: James Lal Date: Mon, 14 Oct 2013 07:29:21 -0700 Subject: [PATCH 5/5] add npm install to makefile --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 36b64c2..51bf3be 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,9 @@ default: test firefox: mozilla-download --product firefox firefox +node_modules: + npm install + .PHONY: test -test: +test: node_modules ./node_modules/.bin/mocha $(shell find . -name "*_test.js")