Skip to content

Commit

Permalink
Merge pull request #406 from facebook/bluebird_promise
Browse files Browse the repository at this point in the history
Replace Q with Bluebird
  • Loading branch information
DmitrySoshnikov committed Jun 19, 2015
2 parents 5a6b8aa + 5c9bcf8 commit c229ed1
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 125 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"-W093": true,
"-W079": true,
"asi": false,
"bitwise": true,
"boss": false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"node-haste": "^1.2.8",
"node-worker-pool": "~2.4.2",
"optimist": "~0.6.0",
"q": "~0.9.7",
"bluebird": "~2.9.30",
"resolve": "~0.6.1",
"through": "^2.3.4",
"lodash.template": "^3.0.0"
Expand Down
62 changes: 27 additions & 35 deletions src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var moduleMocker = require('../lib/moduleMocker');
var NodeHaste = require('node-haste/lib/Haste');
var os = require('os');
var path = require('path');
var Q = require('q');
var Promise = require('bluebird');
var resolve = require('resolve');
var utils = require('../lib/utils');

Expand Down Expand Up @@ -162,44 +162,36 @@ function Loader(config, environment, resourceMap) {
}

Loader.loadResourceMap = function(config, options) {
options = options || {};

var deferred = Q.defer();
try {
_constructHasteInst(config, options).update(
_getCacheFilePath(config),
function(resourceMap) {
deferred.resolve(resourceMap);
}
);
} catch (e) {
deferred.reject(e);
}

return deferred.promise;
return new Promise(function(resolve, reject) {
try {
_constructHasteInst(config, options || {}).update(
_getCacheFilePath(config),
resolve
);
} catch (e) {
reject(e);
}
});
};

Loader.loadResourceMapFromCacheFile = function(config, options) {
options = options || {};

var deferred = Q.defer();
try {
var hasteInst = _constructHasteInst(config, options);
hasteInst.loadMap(
_getCacheFilePath(config),
function(err, map) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(map);
return new Promise(function(resolve, reject) {
try {
var hasteInst = _constructHasteInst(config, options || {});
hasteInst.loadMap(
_getCacheFilePath(config),
function(err, map) {
if (err) {
reject(err);
} else {
resolve(map);
}
}
}
);
} catch (e) {
deferred.reject(e);
}

return deferred.promise;
);
} catch (e) {
reject(e);
}
});
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
jest.autoMockOff();

var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var utils = require('../../lib/utils');

describe('HasteModuleLoader', function() {
Expand All @@ -30,7 +30,9 @@ describe('HasteModuleLoader', function() {
return buildLoader();
});
} else {
return q(new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap));
return Promise.resolve(
new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
jest.autoMockOff();

var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var utils = require('../../lib/utils');

describe('nodeHasteModuleLoader', function() {
Expand All @@ -30,7 +30,9 @@ describe('nodeHasteModuleLoader', function() {
return buildLoader();
});
} else {
return q(new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap));
return Promise.resolve(
new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
jest.autoMockOff();

var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var utils = require('../../lib/utils');

describe('HasteModuleLoader', function() {
Expand All @@ -35,7 +35,9 @@ describe('HasteModuleLoader', function() {
return buildLoader();
});
} else {
return q(new HasteModuleLoader(config, mockEnvironment, resourceMap));
return Promise.resolve(
new HasteModuleLoader(config, mockEnvironment, resourceMap)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
jest.autoMockOff();

var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var utils = require('../../lib/utils');

describe('HasteModuleLoader', function() {
Expand All @@ -30,7 +30,9 @@ describe('HasteModuleLoader', function() {
return buildLoader();
});
} else {
return q(new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap));
return Promise.resolve(
new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap)
);
}
}

Expand Down Expand Up @@ -144,7 +146,7 @@ describe('HasteModuleLoader', function() {
expect(loader.requireMock(null, 'events').EventEmitter).toBeDefined();
});
});

pit('throws on non-existant @providesModule modules', function() {
return buildLoader().then(function(loader) {
expect(function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
jest.autoMockOff();

var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var utils = require('../../lib/utils');

describe('HasteModuleLoader', function() {
Expand All @@ -30,7 +30,9 @@ describe('HasteModuleLoader', function() {
return buildLoader();
});
} else {
return q(new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap));
return Promise.resolve(
new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
jest.autoMockOff();

var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var utils = require('../../lib/utils');

describe('HasteModuleLoader', function() {
Expand All @@ -30,7 +30,9 @@ describe('HasteModuleLoader', function() {
return buildLoader();
});
} else {
return q(new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap));
return Promise.resolve(
new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap)
);
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
var fs = require('graceful-fs');
var os = require('os');
var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var through = require('through');
var utils = require('./lib/utils');
var WorkerPool = require('node-worker-pool');
var Console = require('./Console');

var TEST_WORKER_PATH = require.resolve('./TestWorker');

// To suppress warning caused by Bluebird, see:
// https://github.com/petkaantonov/bluebird/issues/661
process.setMaxListeners(0);

var DEFAULT_OPTIONS = {

/**
Expand Down Expand Up @@ -472,7 +476,7 @@ TestRunner.prototype._createTestRun = function(
TestRunner.prototype._createInBandTestRun = function(
testPaths, onTestResult, onRunFailure
) {
var testSequence = q();
var testSequence = Promise.resolve();
testPaths.forEach(function(testPath) {
testSequence = testSequence.then(this.runTest.bind(this, testPath))
.then(function(testResult) {
Expand Down Expand Up @@ -500,7 +504,7 @@ TestRunner.prototype._createParallelTestRun = function(

return this._getModuleLoaderResourceMap()
.then(function() {
return q.all(testPaths.map(function(testPath) {
return Promise.all(testPaths.map(function(testPath) {
return workerPool.sendMessage({testFilePath: testPath})
.then(function(testResult) {
onTestResult(testPath, testResult);
Expand Down Expand Up @@ -542,18 +546,18 @@ TestRunner.prototype._createParallelTestRun = function(
};

function _pathStreamToPromise(stream) {
var defer = q.defer();
var paths = [];
stream.on('data', function(path) {
paths.push(path);
});
stream.on('error', function(err) {
defer.reject(err);
});
stream.on('end', function() {
defer.resolve(paths);
return new Promise(function(resolve, reject) {
var paths = [];
stream.on('data', function(path) {
paths.push(path);
});
stream.on('error', function(err) {
reject(err);
});
stream.on('end', function() {
resolve(paths);
});
});
return defer.promise;
}


Expand Down
1 change: 0 additions & 1 deletion src/TestWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
'use strict';

var optimist = require('optimist');
//var q = require('q');
var TestRunner = require('./TestRunner');
var workerUtils = require('node-worker-pool/nodeWorkerUtils');

Expand Down
27 changes: 13 additions & 14 deletions src/__tests__/TestRunner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

jest.autoMockOff().mock('fs');

var q = require('q');
var Promise = require('bluebird');

describe('TestRunner', function() {
var TestRunner;
Expand Down Expand Up @@ -66,22 +66,21 @@ describe('TestRunner', function() {
var utils;

function pathStreamToPromise(pathStream) {
var deferred = q.defer();
return new Promise(function(resolve, reject) {
var paths = [];
pathStream.on('data', function(pathStr) {
paths.push(pathStr);
});

var paths = [];
pathStream.on('data', function(pathStr) {
paths.push(pathStr);
});
pathStream.on('error', function(err) {
reject(err);
});

pathStream.on('error', function(err) {
deferred.reject(err);
});
pathStream.on('end', function() {
resolve(paths);
});

pathStream.on('end', function() {
deferred.resolve(paths);
});

return deferred.promise;
}

beforeEach(function() {
Expand All @@ -94,7 +93,7 @@ describe('TestRunner', function() {

fakeDepsFromPath = {};
runner._constructModuleLoader = function() {
return q({
return Promise.resolve({
getDependentsFromPath: function(modulePath) {
return fakeDepsFromPath[modulePath] || [];
}
Expand Down
4 changes: 2 additions & 2 deletions src/jasmineTestRunner/JasmineReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var colors = require('../lib/colors');
var diff = require('diff');
var formatMsg = require('../lib/utils').formatMsg;
var jasmine = require('../../vendor/jasmine/jasmine-1.3.0').jasmine;
var Q = require('q');
var Promise = require('bluebird');

var ERROR_TITLE_COLOR = colors.RED + colors.BOLD + colors.UNDERLINE;
var DIFFABLE_MATCHERS = {
Expand All @@ -26,7 +26,7 @@ function JasmineReporter(config) {
jasmine.Reporter.call(this);
this._config = config || {};
this._logs = [];
this._resultsDeferred = Q.defer();
this._resultsDeferred = Promise.defer();
}

JasmineReporter.prototype = Object.create(jasmine.Reporter.prototype);
Expand Down
Loading

0 comments on commit c229ed1

Please sign in to comment.