From 5c9bcf80e2da55fca7d934a478bc4beefe6e261d Mon Sep 17 00:00:00 2001 From: Dmitry Soshnikov Date: Tue, 16 Jun 2015 21:13:56 -0700 Subject: [PATCH] Replace Q with Bluebird --- .jshintrc | 1 + package.json | 2 +- src/HasteModuleLoader/HasteModuleLoader.js | 62 +++++++------- .../HasteModuleLoader-NODE_PATH-test.js | 6 +- ...asteModuleLoader-genMockFromModule-test.js | 6 +- .../HasteModuleLoader-getTestEnvData-test.js | 6 +- .../HasteModuleLoader-requireMock-test.js | 8 +- .../HasteModuleLoader-requireModule-test.js | 6 +- ...teModuleLoader-requireModuleOrMock-test.js | 6 +- src/TestRunner.js | 32 ++++---- src/TestWorker.js | 1 - src/__tests__/TestRunner-test.js | 27 +++---- src/jasmineTestRunner/JasmineReporter.js | 4 +- src/jest.js | 80 +++++++++---------- src/lib/utils.js | 8 +- 15 files changed, 130 insertions(+), 125 deletions(-) diff --git a/.jshintrc b/.jshintrc index 77d3b2a0cd96..698e083de51e 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,5 +1,6 @@ { "-W093": true, + "-W079": true, "asi": false, "bitwise": true, "boss": false, diff --git a/package.json b/package.json index 93d5515f145f..3ffd7eb8cbd4 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/HasteModuleLoader/HasteModuleLoader.js b/src/HasteModuleLoader/HasteModuleLoader.js index 77648f855323..3489b8c30149 100644 --- a/src/HasteModuleLoader/HasteModuleLoader.js +++ b/src/HasteModuleLoader/HasteModuleLoader.js @@ -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'); @@ -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); + } + }); }; /** diff --git a/src/HasteModuleLoader/__tests__/HasteModuleLoader-NODE_PATH-test.js b/src/HasteModuleLoader/__tests__/HasteModuleLoader-NODE_PATH-test.js index 8b9183a57a43..5d4abeb636ae 100644 --- a/src/HasteModuleLoader/__tests__/HasteModuleLoader-NODE_PATH-test.js +++ b/src/HasteModuleLoader/__tests__/HasteModuleLoader-NODE_PATH-test.js @@ -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() { @@ -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) + ); } } diff --git a/src/HasteModuleLoader/__tests__/HasteModuleLoader-genMockFromModule-test.js b/src/HasteModuleLoader/__tests__/HasteModuleLoader-genMockFromModule-test.js index 37c3e9868d48..ea8719e146a3 100644 --- a/src/HasteModuleLoader/__tests__/HasteModuleLoader-genMockFromModule-test.js +++ b/src/HasteModuleLoader/__tests__/HasteModuleLoader-genMockFromModule-test.js @@ -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() { @@ -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) + ); } } diff --git a/src/HasteModuleLoader/__tests__/HasteModuleLoader-getTestEnvData-test.js b/src/HasteModuleLoader/__tests__/HasteModuleLoader-getTestEnvData-test.js index aba8a22eff28..331e6c11ca72 100644 --- a/src/HasteModuleLoader/__tests__/HasteModuleLoader-getTestEnvData-test.js +++ b/src/HasteModuleLoader/__tests__/HasteModuleLoader-getTestEnvData-test.js @@ -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() { @@ -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) + ); } } diff --git a/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireMock-test.js b/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireMock-test.js index d5f8218d6246..b3b658e8d3f0 100644 --- a/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireMock-test.js +++ b/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireMock-test.js @@ -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() { @@ -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) + ); } } @@ -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() { diff --git a/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModule-test.js b/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModule-test.js index 8276ee71e223..c1ef374b72bf 100644 --- a/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModule-test.js +++ b/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModule-test.js @@ -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() { @@ -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) + ); } } diff --git a/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModuleOrMock-test.js b/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModuleOrMock-test.js index 04c0ab5a0b9c..1477cf0d1fcb 100644 --- a/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModuleOrMock-test.js +++ b/src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModuleOrMock-test.js @@ -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() { @@ -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) + ); } } diff --git a/src/TestRunner.js b/src/TestRunner.js index 08b36e23f118..f7f42c626ab0 100644 --- a/src/TestRunner.js +++ b/src/TestRunner.js @@ -10,7 +10,7 @@ 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'); @@ -18,6 +18,10 @@ 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 = { /** @@ -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) { @@ -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); @@ -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; } diff --git a/src/TestWorker.js b/src/TestWorker.js index 05c16aa1c789..5cc495b9a2d3 100644 --- a/src/TestWorker.js +++ b/src/TestWorker.js @@ -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'); diff --git a/src/__tests__/TestRunner-test.js b/src/__tests__/TestRunner-test.js index 905bc2e3f7eb..997265644cd3 100644 --- a/src/__tests__/TestRunner-test.js +++ b/src/__tests__/TestRunner-test.js @@ -9,7 +9,7 @@ jest.autoMockOff().mock('fs'); -var q = require('q'); +var Promise = require('bluebird'); describe('TestRunner', function() { var TestRunner; @@ -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() { @@ -94,7 +93,7 @@ describe('TestRunner', function() { fakeDepsFromPath = {}; runner._constructModuleLoader = function() { - return q({ + return Promise.resolve({ getDependentsFromPath: function(modulePath) { return fakeDepsFromPath[modulePath] || []; } diff --git a/src/jasmineTestRunner/JasmineReporter.js b/src/jasmineTestRunner/JasmineReporter.js index 17720e965a02..7b727efe45f0 100644 --- a/src/jasmineTestRunner/JasmineReporter.js +++ b/src/jasmineTestRunner/JasmineReporter.js @@ -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 = { @@ -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); diff --git a/src/jest.js b/src/jest.js index b7c24b28fdff..8e85f0a794c4 100644 --- a/src/jest.js +++ b/src/jest.js @@ -10,7 +10,7 @@ var childProcess = require('child_process'); var fs = require('fs'); var path = require('path'); -var q = require('q'); +var Promise = require('bluebird'); var TestRunner = require('./TestRunner'); var utils = require('./lib/utils'); @@ -24,50 +24,46 @@ function getVersion() { } function _findChangedFiles(dirPath) { - var deferred = q.defer(); - - var args = - ['diff', '--name-only', '--diff-filter=ACMR']; - var child = childProcess.spawn('git', args, {cwd: dirPath}); - - var stdout = ''; - child.stdout.on('data', function(data) { - stdout += data; - }); + return new Promise(function(resolve, reject) { + var args = + ['diff', '--name-only', '--diff-filter=ACMR']; + var child = childProcess.spawn('git', args, {cwd: dirPath}); + + var stdout = ''; + child.stdout.on('data', function(data) { + stdout += data; + }); - var stderr = ''; - child.stderr.on('data', function(data) { - stderr += data; - }); + var stderr = ''; + child.stderr.on('data', function(data) { + stderr += data; + }); - child.on('close', function(code) { - if (code === 0) { - stdout = stdout.trim(); - if (stdout === '') { - deferred.resolve([]); + child.on('close', function(code) { + if (code === 0) { + stdout = stdout.trim(); + if (stdout === '') { + resolve([]); + } else { + resolve(stdout.split('\n').map(function(changedPath) { + return path.resolve(dirPath, changedPath); + })); + } } else { - deferred.resolve(stdout.split('\n').map(function(changedPath) { - return path.resolve(dirPath, changedPath); - })); + reject(code + ': ' + stderr); } - } else { - deferred.reject(code + ': ' + stderr); - } + }); }); - - return deferred.promise; } function _verifyIsGitRepository(dirPath) { - var deferred = q.defer(); - - childProcess.spawn('git', ['rev-parse', '--git-dir'], {cwd: dirPath}) - .on('close', function(code) { - var isGitRepo = code === 0; - deferred.resolve(isGitRepo); - }); - - return deferred.promise; + return new Promise(function(resolve) { + childProcess.spawn('git', ['rev-parse', '--git-dir'], {cwd: dirPath}) + .on('close', function(code) { + var isGitRepo = code === 0; + resolve(isGitRepo); + }); + }); } function _testRunnerOptions(argv) { @@ -111,7 +107,7 @@ function _promiseRawConfig(argv, packageRoot) { } if (typeof argv.config === 'object') { - return q(utils.normalizeConfig(argv.config)); + return Promise.resolve(utils.normalizeConfig(argv.config)); } var pkgJsonPath = path.join(packageRoot, 'package.json'); @@ -126,11 +122,11 @@ function _promiseRawConfig(argv, packageRoot) { } var config = utils.normalizeConfig(pkgJson.jest); config.name = pkgJson.name; - return q(config); + return Promise.resolve(config); } // Sane default config - return q(utils.normalizeConfig({ + return Promise.resolve(utils.normalizeConfig({ name: packageRoot.replace(/[/\\]/g, '_'), rootDir: packageRoot, testPathDirs: [packageRoot], @@ -140,7 +136,7 @@ function _promiseRawConfig(argv, packageRoot) { function _promiseOnlyChangedTestPaths(testRunner, config) { var testPathDirsAreGit = config.testPathDirs.map(_verifyIsGitRepository); - return q.all(testPathDirsAreGit) + return Promise.all(testPathDirsAreGit) .then(function(results) { if (!results.every(function(result) { return result; })) { throw ( @@ -149,7 +145,7 @@ function _promiseOnlyChangedTestPaths(testRunner, config) { 'with git projects.\n' ); } - return q.all(config.testPathDirs.map(_findChangedFiles)); + return Promise.all(config.testPathDirs.map(_findChangedFiles)); }) .then(function(changedPathSets) { // Collapse changed files from each of the testPathDirs into a single list diff --git a/src/lib/utils.js b/src/lib/utils.js index b287e47e9a1c..cb8b761c6550 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -10,7 +10,7 @@ var colors = require('./colors'); var fs = require('graceful-fs'); var path = require('path'); -var Q = require('q'); +var Promise = require('bluebird'); var DEFAULT_CONFIG_VALUES = { cacheDirectory: path.resolve(__dirname, '..', '..', '.haste_cache'), @@ -291,9 +291,10 @@ function pathNormalize(dir) { return path.normalize(dir.replace(/\\/g, '/')).replace(/\\/g, '/'); } +var readFile = Promise.promisify(fs.readFile); function loadConfigFromFile(filePath) { var fileDir = path.dirname(filePath); - return Q.nfcall(fs.readFile, filePath, 'utf8').then(function(fileData) { + return readFile(filePath, 'utf8').then(function(fileData) { var config = JSON.parse(fileData); if (!config.hasOwnProperty('rootDir')) { config.rootDir = fileDir; @@ -306,7 +307,7 @@ function loadConfigFromFile(filePath) { function loadConfigFromPackageJson(filePath) { var pkgJsonDir = path.dirname(filePath); - return Q.nfcall(fs.readFile, filePath, 'utf8').then(function(fileData) { + return readFile(filePath, 'utf8').then(function(fileData) { var packageJsonData = JSON.parse(fileData); var config = packageJsonData.jest; config.name = packageJsonData.name; @@ -443,6 +444,7 @@ function formatMsg(msg, color, _config) { // (mostly because these paths represent noisy/unhelpful libs) var STACK_TRACE_LINE_IGNORE_RE = new RegExp('^(?:' + [ path.resolve(__dirname, '..', 'node_modules', 'q'), + path.resolve(__dirname, '..', 'node_modules', 'bluebird'), path.resolve(__dirname, '..', 'vendor', 'jasmine') ].join('|') + ')');