Skip to content

Commit

Permalink
Merge 78cf2f8 into 36cc19f
Browse files Browse the repository at this point in the history
  • Loading branch information
ccarruitero committed Apr 28, 2017
2 parents 36cc19f + 78cf2f8 commit bf8c43a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
18 changes: 10 additions & 8 deletions src/cmd/run.js
Expand Up @@ -25,7 +25,7 @@ import {
import type {FirefoxPreferences} from '../firefox/preferences';
import type {OnSourceChangeFn} from '../watcher';
import type {
FirefoxProcess, // eslint-disable-line import/named
FirefoxProcess, FirefoxRunResponse, // eslint-disable-line import/named
} from '../firefox/index';
import type {
FirefoxConnectorFn, RemoteFirefox,
Expand Down Expand Up @@ -172,23 +172,24 @@ export function defaultReloadStrategy(

export type CreateFirefoxClientParams = {|
connectToFirefox?: FirefoxConnectorFn,
maxRetries: number,
retryInterval: number,
maxRetries?: number,
retryInterval?: number,
port: number,
|};

export function defaultFirefoxClient(
{
connectToFirefox = defaultFirefoxConnector,
// A max of 250 will try connecting for 30 seconds.
maxRetries = 250, retryInterval = 120,
maxRetries = 250, retryInterval = 120, port,
}: CreateFirefoxClientParams = {}
): Promise<RemoteFirefox> {
async function establishConnection() {
var lastError;

for (let retries = 0; retries <= maxRetries; retries++) {
try {
return await connectToFirefox();
return await connectToFirefox(port);
} catch (error) {
if (isErrorWithCode('ECONNREFUSED', error)) {
// Wait for `retryInterval` ms.
Expand Down Expand Up @@ -290,13 +291,13 @@ export default async function run(
installed = true;
}

const runningFirefox = await runner.run(profile);
const { firefox: runningFirefox, port } = await runner.run(profile);

if (installed) {
log.debug('Not installing as temporary add-on because the ' +
'add-on was already installed');
} else if (requiresRemote) {
client = await firefoxClient();
client = await firefoxClient({ port });

try {
addonId = await runner.installAsTemporaryAddon(client).then(
Expand Down Expand Up @@ -419,7 +420,7 @@ export class ExtensionRunner {
.then(() => getManifestId(manifestData));
}

run(profile: FirefoxProfile): Promise<FirefoxProcess> {
run(profile: FirefoxProfile): Promise<FirefoxRunResponse> {
const binaryArgs = [];
const {firefoxApp, firefox, startUrl} = this;
if (this.browserConsole) {
Expand All @@ -431,6 +432,7 @@ export class ExtensionRunner {
binaryArgs.push('--url', url);
}
}

return firefoxApp.run(profile, {
firefoxBinary: firefox, binaryArgs,
});
Expand Down
9 changes: 7 additions & 2 deletions src/firefox/index.js
Expand Up @@ -109,6 +109,11 @@ export type FirefoxRunnerFn =
(params: FirefoxRunnerParams) => Promise<FirefoxRunnerResults>;


export type FirefoxRunResponse = {|
firefox: FirefoxProcess,
port: number,
|}

// Run command types and implementaion.

export type FirefoxRunOptions = {|
Expand All @@ -129,7 +134,7 @@ export async function run(
findRemotePort = defaultRemotePortFinder,
firefoxBinary, binaryArgs,
}: FirefoxRunOptions = {}
): Promise<FirefoxProcess> {
): Promise<FirefoxRunResponse> {

log.debug(`Running Firefox with profile at ${profile.path()}`);

Expand Down Expand Up @@ -180,7 +185,7 @@ export async function run(
log.debug('Firefox closed');
});

return firefox;
return { firefox, port: remotePort };
}


Expand Down
2 changes: 1 addition & 1 deletion src/firefox/remote.js
Expand Up @@ -204,6 +204,6 @@ export default async function connect(
): Promise<RemoteFirefox> {
log.debug(`Connecting to Firefox on port ${port}`);
const client = await connectToFirefox(port);
log.debug('Connected to the remote Firefox debugger');
log.debug(`Connected to the remote Firefox debugger on port ${port}`);
return new RemoteFirefox(client);
}
24 changes: 21 additions & 3 deletions tests/unit/test-cmd/test.run.js
Expand Up @@ -67,14 +67,15 @@ describe('run', () => {
};
}

function getFakeFirefox(implementations = {}) {
function getFakeFirefox(implementations = {}, port = 6005) {
const profile = {}; // empty object just to avoid errors.
const firefox = () => Promise.resolve();
const allImplementations = {
createProfile: () => Promise.resolve(profile),
copyProfile: () => Promise.resolve(profile),
useProfile: () => Promise.resolve(profile),
installExtension: () => Promise.resolve(),
run: () => Promise.resolve(),
run: () => Promise.resolve({firefox, port}),
...implementations,
};
return fake(defaultFirefoxApp, allImplementations);
Expand Down Expand Up @@ -104,6 +105,21 @@ describe('run', () => {
});
});

it('run extension in correct port', () => {
const cmd = prepareRun();
const {firefoxClient} = cmd.options;
const port = 6008;
const firefoxApp = getFakeFirefox({}, port);

return cmd.run({}, {
firefoxApp,
}).then(() => {
assert.equal(firefoxApp.run.called, true);

assert.equal(firefoxClient.firstCall.args[0].port, port);
});
});

it('suggests --pre-install when remote install not supported', () => {
const cmd = prepareRun();
const firefoxClient = fake(RemoteFirefox.prototype, {
Expand Down Expand Up @@ -523,7 +539,9 @@ describe('run', () => {
describe('firefoxClient', () => {

function firefoxClient(opt = {}) {
return defaultFirefoxClient({maxRetries: 0, retryInterval: 1, ...opt});
return defaultFirefoxClient({
maxRetries: 0, retryInterval: 1, port: 6005, ...opt,
});
}

it('retries after a connection error', () => {
Expand Down

0 comments on commit bf8c43a

Please sign in to comment.