Skip to content
Permalink
Browse files
Merge pull request #710 from raphinesse/q-nomore
Drop dependency on Q, use native promises
  • Loading branch information
raphinesse committed Sep 30, 2018
2 parents ba90e39 + 54a9b3d commit 53b2ebbe66c0a429e57e5daf3b80a17b3b927b68
Show file tree
Hide file tree
Showing 52 changed files with 256 additions and 326 deletions.
@@ -11,3 +11,4 @@ rules:
linebreak-style:
- error
- unix
promise/catch-or-return: error
@@ -17,9 +17,9 @@
under the License.
**/

const Q = require('q');
const path = require('path');
const fs = require('fs-extra');
const delay = require('delay');
const globby = require('globby');

const HooksRunner = require('../src/hooks/HooksRunner');
@@ -262,7 +262,7 @@ describe('HooksRunner', function () {
} else if (cmd.match(/version/)) {
return '3.6.0';
}
return Q();
return Promise.resolve();
});
});

@@ -319,7 +319,7 @@ describe('HooksRunner', function () {
});

describe('module-level hooks (event handlers)', function () {
var handler = jasmine.createSpy().and.returnValue(Q());
var handler = jasmine.createSpy().and.returnValue(Promise.resolve());

afterEach(function () {
cordova.removeAllListeners(test_event);
@@ -352,8 +352,8 @@ describe('HooksRunner', function () {
const order = [];
// Delay 100 ms here to check that h2 is not executed until after
// the promise returned by h1 is resolved.
const h1 = _ => Q.delay(100).then(_ => order.push(1));
const h2 = _ => Q().then(_ => order.push(2));
const h1 = _ => delay(100).then(_ => order.push(1));
const h2 = _ => Promise.resolve().then(_ => order.push(2));

cordova.on(test_event, h1);
cordova.on(test_event, h2);
@@ -365,7 +365,7 @@ describe('HooksRunner', function () {
it('Test 021 : should pass data object that fire calls into async handlers', function () {
var asyncHandler = function (opts) {
expect(opts).toEqual(hookOptions);
return Q();
return Promise.resolve();
};
cordova.on(test_event, asyncHandler);
return hooksRunner.fire(test_event, hookOptions);
@@ -486,7 +486,7 @@ describe('pkgJson', function () {
expect(installedPlatforms()).toEqual([]);

// Remove for testing purposes so platform is not pre-installed.
cordova.platform('rm', PLATFORM, {save: true}).then(function () {
return cordova.platform('rm', PLATFORM, {save: true}).then(function () {
return cordova.platform('add', PLATFORM, {save: true});
}).then(function () {
// pkg.json has new platform.
@@ -20,7 +20,6 @@
var fs = require('fs-extra');
var helpers = require('../spec/helpers');
var path = require('path');
var Q = require('q');
var events = require('cordova-common').events;
var cordova = require('../src/cordova/cordova');
var platforms = require('../src/platforms/platforms');
@@ -96,7 +95,7 @@ function mockPluginFetch (id, dir) {
var dest = path.join(project, 'plugins', id);

fs.copySync(path.join(dir, 'plugin.xml'), path.join(dest, 'plugin.xml'));
return Q(dest);
return Promise.resolve(dest);
});
}

@@ -226,7 +225,7 @@ describe('plugin end-to-end', function () {
var scopedPackage = '@testscope/' + npmInfoTestPlugin;
mockPluginFetch(npmInfoTestPlugin, path.join(pluginsDir, npmInfoTestPlugin));

spyOn(plugin_util, 'info').and.returnValue(Q({}));
spyOn(plugin_util, 'info').and.returnValue(Promise.resolve({}));
return addPlugin(scopedPackage, npmInfoTestPlugin, {})
.then(function () {
// Check to make sure that we are at least trying to get the correct package.
@@ -29,7 +29,6 @@ var test_pkgjson_plugin = path.join(plugins_dir, 'pkgjson-test-plugin');
var test_plugin_searchpath = path.join(test_plugin, '..');
var test_plugin_id = 'org.test.plugins.childbrowser';
var test_plugin_version = '0.6.0';
var Q = require('q');

describe('fetch', function () {

@@ -73,7 +72,7 @@ describe('fetch', function () {
revertLocal = fetch.__set__('localPlugins', null);
revertFetch = fetch.__set__('fetch', function (pluginDir) {
fetchCalls++;
return Q(pluginDir);
return Promise.resolve(pluginDir);
});
});

@@ -150,7 +149,7 @@ describe('fetch', function () {
var srcDir = path.join(plugins_dir, 'recursivePlug');
var appDir = path.join(plugins_dir, 'recursivePlug', 'demo');
fetch.__set__('fetch', function (pluginDir) {
return Q(pluginDir);
return Promise.resolve(pluginDir);
});

it('Test 021 : should skip copy to avoid recursive error', function () {
@@ -17,7 +17,6 @@
under the License.
*/

const Q = require('q');
const fs = require('fs-extra');
const path = require('path');
const rewire = require('rewire');
@@ -102,14 +101,14 @@ describe('plugman/uninstall', () => {
beforeEach(function () {
setupProject('uninstall.test');

spyOn(ActionStack.prototype, 'process').and.returnValue(Q());
spyOn(ActionStack.prototype, 'process').and.returnValue(Promise.resolve());
spyOn(fs, 'copySync');
});

describe('success', function () {

it('Test 002 : should get PlatformApi instance for platform and invoke its\' removePlugin method', function () {
const platformApi = { removePlugin: jasmine.createSpy('removePlugin').and.returnValue(Q()) };
const platformApi = { removePlugin: jasmine.createSpy('removePlugin').and.returnValue(Promise.resolve()) };
const getPlatformApi = spyOn(platforms, 'getPlatformApi').and.returnValue(platformApi);

return uninstall.uninstallPlatform('android', project, dummy_id)
@@ -137,7 +136,7 @@ describe('plugman/uninstall', () => {
return values.reduce(function (promise, value) {
return promise
.then(function () {
platformApi.removePlugin.and.returnValue(Q(value));
platformApi.removePlugin.and.returnValue(Promise.resolve(value));
return uninstall.uninstallPlatform('android', project, dummy_id, null,
{ pluginInfoProvider: fakeProvider, platformVersion: '9.9.9' });
})
@@ -146,7 +145,7 @@ describe('plugman/uninstall', () => {
}, function (err) {
expect(err).toBeUndefined();
});
}, Q());
}, Promise.resolve());
}

return validateReturnedResultFor([ true, {}, [], 'foo', function () {} ], true)
@@ -29,14 +29,14 @@
"indent-string": "^3.2.0",
"init-package-json": "^1.2.0",
"md5-file": "^4.0.0",
"q": "^1.5.1",
"read-chunk": "^3.0.0",
"semver": "^5.3.0",
"shebang-command": "^1.2.0",
"underscore": "^1.9.0"
},
"devDependencies": {
"codecov": "^3.0.1",
"delay": "^4.0.0",
"eslint": "^4.2.0",
"eslint-config-semistandard": "^12.0.1",
"eslint-config-standard": "^11.0.0",
@@ -19,7 +19,6 @@
var cordova = require('../../src/cordova/cordova');
var platforms = require('../../src/platforms/platforms');
var HooksRunner = require('../../src/hooks/HooksRunner');
var Q = require('q');
var util = require('../../src/cordova/util');

var supported_platforms = Object.keys(platforms);
@@ -35,9 +34,9 @@ describe('build command', function () {
is_cordova = spyOn(util, 'isCordova').and.returnValue(project_dir);
spyOn(util, 'cdProjectRoot').and.returnValue(project_dir);
list_platforms = spyOn(util, 'listPlatforms').and.returnValue(supported_platforms);
fire = spyOn(HooksRunner.prototype, 'fire').and.returnValue(Q());
prepare_spy = spyOn(cordova, 'prepare').and.returnValue(Q());
compile_spy = spyOn(cordova, 'compile').and.returnValue(Q());
fire = spyOn(HooksRunner.prototype, 'fire').and.returnValue(Promise.resolve());
prepare_spy = spyOn(cordova, 'prepare').and.returnValue(Promise.resolve());
compile_spy = spyOn(cordova, 'compile').and.returnValue(Promise.resolve());
});
describe('failure', function () {
it('Test 001 : should not run inside a project with no platforms', function () {
@@ -102,7 +101,7 @@ describe('build command', function () {
describe('with no platforms added', function () {
it('Test 008 : should not fire the hooker', function () {
list_platforms.and.returnValue([]);
return Q().then(cordova.build).then(function () {
return Promise.resolve().then(cordova.build).then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
@@ -20,7 +20,6 @@ var cordova = require('../../src/cordova/cordova');
var platforms = require('../../src/platforms/platforms');
var HooksRunner = require('../../src/hooks/HooksRunner');
var util = require('../../src/cordova/util');
var Q = require('q');

var supported_platforms = Object.keys(platforms).filter(function (p) { return p !== 'www'; });

@@ -36,8 +35,8 @@ describe('compile command', function () {
is_cordova = spyOn(util, 'isCordova').and.returnValue(project_dir);
spyOn(util, 'cdProjectRoot').and.returnValue(project_dir);
list_platforms = spyOn(util, 'listPlatforms').and.returnValue(supported_platforms);
fire = spyOn(HooksRunner.prototype, 'fire').and.returnValue(Q());
platformApi = { build: jasmine.createSpy('build').and.returnValue(Q()) };
fire = spyOn(HooksRunner.prototype, 'fire').and.returnValue(Promise.resolve());
platformApi = { build: jasmine.createSpy('build').and.returnValue(Promise.resolve()) };
getPlatformApi = spyOn(platforms, 'getPlatformApi').and.returnValue(platformApi);
});
describe('failure', function () {
@@ -100,7 +99,7 @@ describe('compile command', function () {
describe('with no platforms added', function () {
it('Test 008 : should not fire the hooker', function () {
list_platforms.and.returnValue([]);
return Q().then(cordova.compile)
return Promise.resolve().then(cordova.compile)
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
@@ -19,7 +19,6 @@
var cordova = require('../../src/cordova/cordova');
var platforms = require('../../src/platforms/platforms');
var HooksRunner = require('../../src/hooks/HooksRunner');
var Q = require('q');
var util = require('../../src/cordova/util');

var supported_platforms = Object.keys(platforms).filter(function (p) { return p !== 'www'; });
@@ -35,11 +34,11 @@ describe('emulate command', function () {
is_cordova = spyOn(util, 'isCordova').and.returnValue(project_dir);
spyOn(util, 'cdProjectRoot').and.returnValue(project_dir);
list_platforms = spyOn(util, 'listPlatforms').and.returnValue(supported_platforms);
fire = spyOn(HooksRunner.prototype, 'fire').and.returnValue(Q());
prepare_spy = spyOn(cordova, 'prepare').and.returnValue(Q());
fire = spyOn(HooksRunner.prototype, 'fire').and.returnValue(Promise.resolve());
prepare_spy = spyOn(cordova, 'prepare').and.returnValue(Promise.resolve());
platformApi = {
run: jasmine.createSpy('run').and.returnValue(Q()),
build: jasmine.createSpy('build').and.returnValue(Q())
run: jasmine.createSpy('run').and.returnValue(Promise.resolve()),
build: jasmine.createSpy('build').and.returnValue(Promise.resolve())
};

getPlatformApi = spyOn(platforms, 'getPlatformApi').and.returnValue(platformApi);
@@ -93,7 +92,7 @@ describe('emulate command', function () {
originalBuildSpy = platformApi.build;
platformApi.build = jasmine.createSpy('build').and.callFake(function (opts) {
opts.couldBeModified = 'insideBuild';
return Q();
return Promise.resolve();
});
});
afterEach(function () {
@@ -149,7 +148,7 @@ describe('emulate command', function () {
describe('with no platforms added', function () {
it('Test 011 : should not fire the hooker', function () {
list_platforms.and.returnValue([]);
return Q().then(cordova.emulate).then(function () {
return Promise.resolve().then(cordova.emulate).then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
@@ -17,7 +17,6 @@

var path = require('path');
var fs = require('fs-extra');
var Q = require('q');
var events = require('cordova-common').events;
var rewire = require('rewire');
var platform_module = require('../../../src/cordova/platform');
@@ -42,15 +41,15 @@ describe('cordova/platform/addHelper', function () {
devDependencies: {}
};
hooks_mock = jasmine.createSpyObj('hooksRunner mock', ['fire']);
hooks_mock.fire.and.returnValue(Q());
hooks_mock.fire.and.returnValue(Promise.resolve());

cfg_parser_mock = function () {};
cfg_parser_mock.prototype = jasmine.createSpyObj('config parser mock', [
'write', 'removeEngine', 'addEngine', 'getHookScripts'
]);
fetch_mock = jasmine.createSpy('fetch mock').and.returnValue(Q());
prepare_mock = jasmine.createSpy('prepare mock').and.returnValue(Q());
prepare_mock.preparePlatforms = jasmine.createSpy('preparePlatforms mock').and.returnValue(Q());
fetch_mock = jasmine.createSpy('fetch mock').and.returnValue(Promise.resolve());
prepare_mock = jasmine.createSpy('prepare mock').and.returnValue(Promise.resolve());
prepare_mock.preparePlatforms = jasmine.createSpy('preparePlatforms mock').and.returnValue(Promise.resolve());

// `cordova.prepare` is never saved to a variable, so we need to fake `require`
platform_addHelper = rewire('../../../src/cordova/platform/addHelper');
@@ -79,13 +78,13 @@ describe('cordova/platform/addHelper', function () {
// Fake platform details we will use for our mocks, returned by either
// getPlatfromDetailsFromDir (in the local-directory case), or
// downloadPlatform (in every other case)
spyOn(platform_module, 'getPlatformDetailsFromDir').and.returnValue(Q(fake_platform));
spyOn(platform_addHelper, 'downloadPlatform').and.returnValue(Q(fake_platform));
spyOn(platform_module, 'getPlatformDetailsFromDir').and.returnValue(Promise.resolve(fake_platform));
spyOn(platform_addHelper, 'downloadPlatform').and.returnValue(Promise.resolve(fake_platform));
spyOn(platform_addHelper, 'getVersionFromConfigFile').and.returnValue(false);
spyOn(platform_addHelper, 'installPluginsForNewPlatform').and.returnValue(Q());
spyOn(platform_addHelper, 'installPluginsForNewPlatform').and.returnValue(Promise.resolve());
platform_api_mock = jasmine.createSpyObj('platform api mock', ['createPlatform', 'updatePlatform']);
platform_api_mock.createPlatform.and.returnValue(Q());
platform_api_mock.updatePlatform.and.returnValue(Q());
platform_api_mock.createPlatform.and.returnValue(Promise.resolve());
platform_api_mock.updatePlatform.and.returnValue(Promise.resolve());
spyOn(cordova_util, 'getPlatformApiFunction').and.returnValue(platform_api_mock);
spyOn(cordova_util, 'requireNoCache').and.returnValue({});
});
@@ -280,7 +279,7 @@ describe('cordova/platform/addHelper', function () {
});
describe('errors', function () {
it('should reject the promise should fetch fail', function () {
fetch_mock.and.returnValue(Q.reject('fetch has failed, rejecting promise'));
fetch_mock.and.returnValue(Promise.reject('fetch has failed, rejecting promise'));
return platform_addHelper.downloadPlatform(projectRoot, 'android', '67').then(function () {
fail('success handler unexpectedly invoked');
}, function (e) {
@@ -306,7 +305,7 @@ describe('cordova/platform/addHelper', function () {
describe('installPluginsForNewPlatform', function () {
beforeEach(function () {
spyOn(fetch_metadata, 'get_fetch_metadata');
spyOn(plugman, 'install').and.returnValue(Q());
spyOn(plugman, 'install').and.returnValue(Promise.resolve());
platform_addHelper.installPluginsForNewPlatform.and.callThrough();
});

@@ -15,7 +15,6 @@ http://www.apache.org/licenses/LICENSE-2.0
under the License.
*/

var Q = require('q');
var fs = require('fs-extra');
var events = require('cordova-common').events;
var superspawn = require('cordova-common').superspawn;
@@ -33,12 +32,11 @@ describe('cordova/platform/check', function () {
spyOn(superspawn, 'spawn').and.callThrough();
spyOn(fs, 'removeSync');
spyOn(cordova_util, 'listPlatforms').and.returnValue(['ios']);
spyOn(platform, 'add').and.returnValue(Q());
spyOn(platform, 'add').and.returnValue(Promise.resolve());
});

it('If no results, platforms cannot be updated', function () {
platform.add.and.returnValue(Q.reject());
cordova_util.listPlatforms.and.callThrough();
it('If no platforms, platforms cannot be updated', function () {
cordova_util.listPlatforms.and.returnValue([]);
return platform_check(hooks_mock, projectRoot).then(function () {
expect(events.emit).toHaveBeenCalledWith('results', jasmine.stringMatching(/No platforms can be updated/));
expect(superspawn.spawn).toHaveBeenCalledWith('npm', ['--loglevel=silent', '--json', 'outdated', 'cordova-lib'], jasmine.any(Object));
@@ -47,21 +45,21 @@ describe('cordova/platform/check', function () {
});

it('Should warn if install failed', function () {
platform.add.and.returnValue(Q.reject());
platform.add.and.returnValue(Promise.reject());
return platform_check(hooks_mock, projectRoot).then(function () {
expect(events.emit).toHaveBeenCalledWith('results', jasmine.stringMatching(/current did not install/));
});
});

it('Should warn if version-empty', function () {
superspawn.spawn.and.returnValue(Q());
superspawn.spawn.and.returnValue(Promise.resolve());
return platform_check(hooks_mock, projectRoot).then(function () {
expect(events.emit).toHaveBeenCalledWith('results', jasmine.stringMatching(/current version script failed to return a version/));
});
});

it('Should warn if version-failed', function () {
spyOn(superspawn, 'maybeSpawn').and.returnValue(Q('version-failed'));
spyOn(superspawn, 'maybeSpawn').and.returnValue(Promise.resolve('version-failed'));
return platform_check(hooks_mock, projectRoot).then(function () {
expect(events.emit).toHaveBeenCalledWith('results', jasmine.stringMatching(/current version script failed, and/));
});

0 comments on commit 53b2ebb

Please sign in to comment.