Skip to content
Permalink
Browse files
Use superspawn to get rid of Q.defer in plugman/install
  • Loading branch information
raphinesse committed Sep 29, 2018
1 parent 7681351 commit eb9aa97fcd5177b066ed6715fbb1b3a10bc90177
Showing 2 changed files with 29 additions and 48 deletions.
@@ -17,7 +17,6 @@
under the License.
*/

const child_process = require('child_process');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
@@ -69,7 +68,7 @@ const fake = {
};

describe('plugman/install', () => {
let exec, fetchSpy;
let fetchSpy;

beforeAll(() => {
results['emit_results'] = [];
@@ -111,10 +110,7 @@ describe('plugman/install', () => {
});

beforeEach(() => {
exec = spyOn(child_process, 'exec').and.callFake((cmd, cb) => {
cb(null, '', '');
});
spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve('3.1.0'));
spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(''));
spyOn(fs, 'ensureDirSync');
spyOn(platforms, 'copyFile').and.returnValue(true);

@@ -155,29 +151,29 @@ describe('plugman/install', () => {
});

it('Test 007 : should check version if plugin has engine tag', () => {
exec.and.callFake((cmd, cb) => { cb(null, '2.5.0\n'); });
superspawn.spawn.and.returnValue(Promise.resolve('2.5.0'));
return install('android', project, pluginDir('com.cordova.engine'))
.then(() => {
expect(satisfies).toHaveBeenCalledWith('2.5.0', '>=1.0.0', true);
});
}, TIMEOUT);
it('Test 008 : should check version and munge it a little if it has "rc" in it so it plays nice with semver (introduce a dash in it)', () => {
exec.and.callFake((cmd, cb) => { cb(null, '3.0.0rc1\n'); });
superspawn.spawn.and.returnValue(Promise.resolve('3.0.0rc1'));
return install('android', project, pluginDir('com.cordova.engine'))
.then(() => {
expect(satisfies).toHaveBeenCalledWith('3.0.0-rc1', '>=1.0.0', true);
});
}, TIMEOUT);
it('Test 009 : should check specific platform version over cordova version if specified', () => {
exec.and.callFake((cmd, cb) => { cb(null, '3.1.0\n'); });
superspawn.spawn.and.returnValue(Promise.resolve('3.1.0'));
return install('android', project, pluginDir('com.cordova.engine-android'))
.then(() => {
expect(satisfies).toHaveBeenCalledWith('3.1.0', '>=3.1.0', true);
});
}, TIMEOUT);
it('Test 010 : should check platform sdk version if specified', () => {
const cordovaVersion = require('../../package.json').version.replace(/-dev|-nightly.*$/, '');
exec.and.callFake((cmd, cb) => { cb(null, '18\n'); });
superspawn.spawn.and.returnValue(Promise.resolve('18'));
return install('android', project, pluginDir('com.cordova.engine-android'))
.then(() => {
expect(satisfies.calls.count()).toBe(3);
@@ -222,9 +218,7 @@ describe('plugman/install', () => {
spyOn(fs, 'existsSync').and.callFake(fake['existsSync']['noPlugins']);
fetchSpy.and.callFake(fake['fetch']['dependencies']);
emit = spyOn(events, 'emit');
exec.and.callFake((cmd, cb) => {
cb(null, '9.0.0\n');
});
superspawn.spawn.and.returnValue(Promise.resolve('9.0.0'));

class PlatformApiMock {
static addPlugin () { return Promise.resolve(); }
@@ -343,9 +337,8 @@ describe('plugman/install', () => {

it('Test 025 :should not fail when trying to install plugin less than minimum version. Skip instead ', () => {
spyOn(semver, 'satisfies').and.returnValue(false);
exec.and.callFake((cmd, cb) => {
cb(null, '0.0.1\n');
});
superspawn.spawn.and.returnValue(Promise.resolve('0.0.1'));

return install('android', project, pluginDir('com.cordova.engine'))
.then(result => {
expect(result).toBe(true);
@@ -21,11 +21,9 @@ var path = require('path');
var fs = require('fs-extra');
var ActionStack = require('cordova-common').ActionStack;
var DepGraph = require('dep-graph');
var child_process = require('child_process');
var semver = require('semver');
var PlatformJson = require('cordova-common').PlatformJson;
var CordovaError = require('cordova-common').CordovaError;
var Q = require('q');
var platform_modules = require('../platforms/platforms');
var os = require('os');
var underscore = require('underscore');
@@ -170,26 +168,21 @@ function callEngineScripts (engines, project_dir) {
var scriptPath = engine.scriptSrc ? '"' + engine.scriptSrc + '"' : null;
if (scriptPath && (isWindows || fs.existsSync(engine.scriptSrc))) {

var d = Q.defer();
if (!isWindows) { // not required on Windows
fs.chmodSync(engine.scriptSrc, '755');
}
child_process.exec(scriptPath, function (error, stdout, stderr) {
if (error) {
events.emit('warn', engine.name + ' version check failed (' + scriptPath + '), continuing anyways.');
engine.currentVersion = null;
} else {
return superspawn.spawn(scriptPath)
.then(stdout => {
engine.currentVersion = cleanVersionOutput(stdout, engine.name);
if (engine.currentVersion === '') {
events.emit('warn', engine.name + ' version check returned nothing (' + scriptPath + '), continuing anyways.');
engine.currentVersion = null;
}
}

d.resolve(engine);
});
return d.promise;

}, () => {
events.emit('warn', engine.name + ' version check failed (' + scriptPath + '), continuing anyways.');
engine.currentVersion = null;
})
.then(_ => engine);
} else {

if (engine.currentVersion) {
@@ -437,31 +430,26 @@ function tryFetchDependency (dep, install, options) {
}

// Now there are two cases here: local directory, and git URL.
var d = Q.defer();

if (fetchdata.source.type === 'local') {

dep.url = fetchdata.source.path;

child_process.exec('git rev-parse --show-toplevel', { cwd: dep.url }, function (err, stdout, stderr) {
if (err) {
return superspawn.spawn('git rev-parse --show-toplevel', { cwd: dep.url })
.catch(err => {
if (err.code === 128) {
return d.reject(new Error('Plugin ' + dep.id + ' is not in git repository. All plugins must be in a git repository.'));
throw new Error('Plugin ' + dep.id + ' is not in git repository. All plugins must be in a git repository.');
} else {
return d.reject(new Error('Failed to locate git repository for ' + dep.id + ' plugin.'));
throw new Error('Failed to locate git repository for ' + dep.id + ' plugin.');
}
}
return d.resolve(stdout.trim());
});

return d.promise.then(function (git_repo) {
// Clear out the subdir since the url now contains it
var url = path.join(git_repo, dep.subdir);
dep.subdir = '';
return Promise.resolve(url);
}).catch(function () {
return Promise.resolve(dep.url);
});
})
.then(function (git_repo) {
// Clear out the subdir since the url now contains it
var url = path.join(git_repo, dep.subdir);
dep.subdir = '';
return Promise.resolve(url);
}).catch(function () {
return Promise.resolve(dep.url);
});

} else if (fetchdata.source.type === 'git') {
return Promise.resolve(fetchdata.source.url);

0 comments on commit eb9aa97

Please sign in to comment.