Skip to content

Commit

Permalink
Remove all flow adapters, use flow in all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kumar303 committed Mar 4, 2016
1 parent 5a9549b commit 3fc4899
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 142 deletions.
17 changes: 12 additions & 5 deletions tests/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow */
import path from 'path';
import sinon from 'sinon';
import {promisify} from '../src/util/es6-modules';
Expand All @@ -8,6 +9,7 @@ import yauzl from 'yauzl';
* A way to read zip files using promises for all the things.
*/
export class ZipFile {
_zip: any;

constructor() {
this._zip = null;
Expand All @@ -17,7 +19,7 @@ export class ZipFile {
* Open a zip file and return a promise that resolves to a yauzl
* zipfile object.
*/
open(...args) {
open(...args: Array<any>): Promise {
return promisify(yauzl.open)(...args)
.then((zip) => {
this._zip = zip;
Expand All @@ -30,9 +32,14 @@ export class ZipFile {
*
* The onRead callback receives a single argument, a yauzl Entry object.
*/
readEach(onRead) {
readEach(onRead: Function): Promise {
return new Promise((resolve, reject) => {

if (!this._zip) {
throw new Error(
'Cannot operate on a falsey zip file. Call open() first.');
}

this._zip.on('entry', (entry) => {
onRead(entry);
});
Expand All @@ -52,7 +59,7 @@ export class ZipFile {
/*
* Returns a path to a test fixture file. Invoke it the same as path.join().
*/
export function fixturePath(...pathParts) {
export function fixturePath(...pathParts: Array<string>): string {
return path.join(__dirname, 'fixtures', ...pathParts);
}

Expand All @@ -68,7 +75,7 @@ export function fixturePath(...pathParts) {
* // Safely make assertions about the error...
* });
*/
export function makeSureItFails() {
export function makeSureItFails(): Function {
return () => {
throw new Error('This test unexpectedly succeeded without an error');
};
Expand Down Expand Up @@ -103,7 +110,7 @@ export function makeSureItFails() {
* assert.equal(fakeProcess.exit.called, true);
*
*/
export function fake(original, methods={}) {
export function fake(original: Object, methods: Object = {}): Object {
var stub = {};

// Provide stubs for all original members:
Expand Down
23 changes: 0 additions & 23 deletions tests/test-cmd/test-build/adapter.js

This file was deleted.

14 changes: 0 additions & 14 deletions tests/test-cmd/test-run/adapter.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* @flow */
import {it, describe} from 'mocha';
import path from 'path';
import {assert} from 'chai';

import {safeFileName} from '../../../src/cmd/build';
import {withTempDir} from '../../../src/util/temp-dir';
import {ZipFile} from '../../helpers';
import build, {prepareBuildDir, safeFileName} from '../../src/cmd/build';
import {withTempDir} from '../../src/util/temp-dir';
import {fixturePath, ZipFile} from '../helpers';
import fs from 'mz/fs';
import {prepareBuildDir} from '../../../src/cmd/build';
import {basicManifest} from '../../test-util/test.manifest';

import * as adapter from './adapter';
import {basicManifest} from '../test-util/test.manifest';


describe('build', () => {
Expand All @@ -20,7 +19,10 @@ describe('build', () => {

return withTempDir(
(tmpDir) =>
adapter.buildMinimalExt(tmpDir)
build({
sourceDir: fixturePath('minimal-web-ext'),
buildDir: tmpDir.path(),
})
.then((buildResult) => {
assert.match(buildResult.extensionPath,
/minimal_extension-1\.0\.xpi$/);
Expand All @@ -46,7 +48,12 @@ describe('build', () => {

it('lets you specify a manifest', () => withTempDir(
(tmpDir) =>
adapter.buildMinimalExtWithManifest(tmpDir, basicManifest)
build({
sourceDir: fixturePath('minimal-web-ext'),
buildDir: tmpDir.path(),
}, {
manifestData: basicManifest,
})
.then((buildResult) => {
assert.match(buildResult.extensionPath,
/the_extension-0\.0\.1\.xpi$/);
Expand Down
20 changes: 12 additions & 8 deletions tests/test-cmd/test-run/test.run.js → tests/test-cmd/test.run.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/* @flow */
import {describe, it} from 'mocha';
import {assert} from 'chai';

import * as firefox from '../../../src/firefox';
import {fake} from '../../helpers';
import {fixturePath} from '../../helpers';

import * as adapter from './adapter';
import run from '../../src/cmd/run';
import * as firefox from '../../src/firefox';
import {fake, fixturePath} from '../helpers';


describe('run', () => {

function runMinimalExt(argv={}, ...optionalArgs) {
return run({sourceDir: fixturePath('minimal-web-ext'), ...argv},
...optionalArgs);
}

function getFakeFirefox(implementations={}) {
let allImplementations = {
createProfile: () => {
Expand All @@ -28,7 +33,7 @@ describe('run', () => {
createProfile: () => Promise.resolve(profile),
});

return adapter.run(fixturePath('minimal-web-ext'), fakeFirefox)
return runMinimalExt({}, {firefox: fakeFirefox})
.then(() => {

let install = fakeFirefox.installExtension;
Expand All @@ -48,8 +53,7 @@ describe('run', () => {
it('passes a custom Firefox binary when specified', () => {
let firefoxBinary = '/pretend/path/to/Firefox/firefox-bin';
let fakeFirefox = getFakeFirefox();
return adapter.runWithFirefox(
fixturePath('minimal-web-ext'), fakeFirefox, firefoxBinary)
return runMinimalExt({firefoxBinary}, {firefox: fakeFirefox})
.then(() => {
assert.equal(fakeFirefox.run.called, true);
assert.equal(fakeFirefox.run.firstCall.args[1].firefoxBinary,
Expand Down
41 changes: 0 additions & 41 deletions tests/test-firefox/adapter.js

This file was deleted.

43 changes: 25 additions & 18 deletions tests/test-firefox/test.firefox.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/* @flow */
import path from 'path';
import {describe, it} from 'mocha';
import {assert} from 'chai';
import deepcopy from 'deepcopy';
import sinon from 'sinon';
import FirefoxProfile from 'firefox-profile';

import * as firefox from '../../src/firefox';
import {onlyInstancesOf, WebExtError} from '../../src/errors';
import fs from 'mz/fs';
import {withTempDir} from '../../src/util/temp-dir';
import {fixturePath, makeSureItFails} from '../helpers';
import {basicManifest} from '../test-util/test.manifest';
import {defaultFirefoxEnv} from '../../src/firefox/';
import * as adapter from './adapter';


describe('firefox', () => {
Expand Down Expand Up @@ -45,7 +47,7 @@ describe('firefox', () => {

it('executes the Firefox runner with a given profile', () => {
let runner = createFakeFxRunner();
return adapter.run(fakeProfile, runner)
return firefox.run(fakeProfile, {fxRunner: runner})
.then(() => {
assert.equal(runner.called, true);
assert.equal(runner.firstCall.args[0].profile,
Expand All @@ -57,7 +59,7 @@ describe('firefox', () => {
let runner = createFakeFxRunner();
// Make sure it passes through process environment variables.
process.env._WEB_EXT_FIREFOX_ENV_TEST = 'thing';
return adapter.run(fakeProfile, runner)
return firefox.run(fakeProfile, {fxRunner: runner})
.then(() => {
let declaredEnv = runner.firstCall.args[0].env;
for (let key in defaultFirefoxEnv) {
Expand All @@ -78,7 +80,7 @@ describe('firefox', () => {
},
});

return adapter.run(fakeProfile, runner)
return firefox.run(fakeProfile, {fxRunner: runner})
.then(makeSureItFails())
.catch((error) => {
assert.equal(error.message, someError.message);
Expand All @@ -88,7 +90,7 @@ describe('firefox', () => {
it('passes a custom Firefox binary when specified', () => {
let runner = createFakeFxRunner();
let firefoxBinary = '/pretend/path/to/firefox-bin';
return adapter.runWithFirefox(fakeProfile, runner, firefoxBinary)
return firefox.run(fakeProfile, {fxRunner: runner, firefoxBinary})
.then(() => {
assert.equal(runner.called, true);
assert.equal(runner.firstCall.args[0].binary,
Expand All @@ -101,7 +103,8 @@ describe('firefox', () => {
describe('createProfile', () => {

it('resolves with a profile object', () => {
return adapter.createDefaultProfile(sinon.stub().returns({}))
return firefox.createProfile(undefined,
{getPrefs: sinon.stub().returns({})})
.then((profile) => {
assert.instanceOf(profile, FirefoxProfile);
});
Expand All @@ -110,7 +113,7 @@ describe('firefox', () => {
it('writes a Firefox profile', () => {
// This is a quick and paranoid sanity check that the FirefoxProfile
// object is working as expected.
return adapter.createProfile()
return firefox.createProfile()
.then((profile) => fs.readFile(path.join(profile.path(), 'user.js')))
.then((prefFile) => {
assert.include(prefFile.toString(),
Expand All @@ -120,15 +123,15 @@ describe('firefox', () => {

it('can create a Firefox profile with some defaults', () => {
let fakePrefGetter = sinon.stub().returns({});
return adapter.createDefaultProfile(fakePrefGetter)
return firefox.createProfile(undefined, {getPrefs: fakePrefGetter})
.then(() => {
assert.equal(fakePrefGetter.firstCall.args[0], 'firefox');
});
});

it('can create a Fennec profile with some defaults', () => {
let fakePrefGetter = sinon.stub().returns({});
return adapter.createFennecProfile(fakePrefGetter)
return firefox.createProfile('fennec', {getPrefs: fakePrefGetter})
.then(() => {
assert.equal(fakePrefGetter.firstCall.args[0], 'fennec');
});
Expand All @@ -138,7 +141,7 @@ describe('firefox', () => {

describe('installExtension', () => {

function setUp(testPromise) {
function setUp(testPromise: Function) {
return withTempDir(
(tmpDir) => {
let data = {
Expand All @@ -156,10 +159,18 @@ describe('firefox', () => {
});
}

function installBasicExt(data, config={}) {
return firefox.installExtension({
manifestData: basicManifest,
profile: data.profile,
extensionPath: data.extensionPath,
...config,
});
}

it('installs an extension file into a profile', () => setUp(
(data) => {
return adapter.installExtension(basicManifest, data.profile,
data.extensionPath)
return installBasicExt(data)
.then(() => fs.readdir(data.profile.extensionsDir))
.then((files) => {
assert.deepEqual(
Expand All @@ -171,19 +182,15 @@ describe('firefox', () => {
it('re-uses an existing extension directory', () => setUp(
(data) => {
return fs.mkdir(path.join(data.profile.extensionsDir))
.then(() => adapter.installExtension(basicManifest,
data.profile,
data.extensionPath))
.then(() => installBasicExt(data))
.then(() => fs.stat(data.profile.extensionsDir));
}
));

it('checks for an empty extensionsDir', () => setUp(
(data) => {
data.profile.extensionsDir = undefined;
return adapter.installExtension(basicManifest,
data.profile,
data.extensionPath)
return installBasicExt(data)
.then(makeSureItFails())
.catch(onlyInstancesOf(WebExtError, (error) => {
assert.match(error.message, /unexpectedly empty/);
Expand Down
Loading

0 comments on commit 3fc4899

Please sign in to comment.