chore: replace superspawn & child_process with execa (#862)

* chore: added execa dependency

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

* chore: execa - drop superspawn in android_sdk

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

* chore: execa - drop superspawn in build

* chore: execa - drop superspawn in check_reqs

Plus: Remove useless trimming of execa output

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

* chore: execa - drop superspawn in emulator

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

* chore: execa - drop superspawn in device

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

* chore: execa - drop superspawn in run_java_unit_tests

* chore: execa - drop superspawn in ProjectBuilder

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

* chore: execa - drop superspawn in adb

* chore: execa - drop superspawn in plugin.spec

* chore: execa - replace child_process in log

* chore: execa - replace child_process in check_reqs

* chore: execa - replace child_process in emulator

Co-authored-by: エリス <erisu@users.noreply.github.com>
This commit is contained in:
Raphael von der Grün
2020-01-06 23:15:22 +01:00
committed by GitHub
parent e3cc75caff
commit fd57909730
16 changed files with 138 additions and 157 deletions
+3 -2
View File
@@ -21,7 +21,8 @@ const os = require('os');
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
const { PluginInfoProvider, superspawn } = require('cordova-common');
const execa = require('execa');
const { PluginInfoProvider } = require('cordova-common');
const createBin = path.join(__dirname, '../../bin/create');
const fakePluginPath = path.join(__dirname, 'fixtures/cordova-plugin-fake');
@@ -44,7 +45,7 @@ describe('plugin add', function () {
const pluginInfo = new PluginInfoProvider().get(fakePluginPath);
return Promise.resolve()
.then(() => superspawn.spawn(createBin, [projectPath, projectid, projectname]))
.then(() => execa(createBin, [projectPath, projectid, projectname]))
.then(() => {
const Api = require(path.join(projectPath, 'cordova/Api.js'));
return new Api('android', projectPath).addPlugin(pluginInfo);
+17 -17
View File
@@ -33,12 +33,12 @@ emulator-5554\tdevice
const downgradeError = 'adb: failed to install app.apk: Failure[INSTALL_FAILED_VERSION_DOWNGRADE]';
let Adb;
let spawnSpy;
let execaSpy;
beforeEach(() => {
Adb = rewire('../../bin/templates/cordova/lib/Adb');
spawnSpy = jasmine.createSpy('spawn');
Adb.__set__('spawn', spawnSpy);
execaSpy = jasmine.createSpy('execa');
Adb.__set__('execa', execaSpy);
});
describe('isDevice', () => {
@@ -61,7 +61,7 @@ emulator-5554\tdevice
describe('devices', () => {
beforeEach(() => {
spawnSpy.and.returnValue(Promise.resolve(adbOutput));
execaSpy.and.returnValue(Promise.resolve({ stdout: adbOutput }));
});
it('should return only devices if no options are specified', () => {
@@ -81,12 +81,12 @@ emulator-5554\tdevice
describe('install', () => {
beforeEach(() => {
spawnSpy.and.returnValue(Promise.resolve(''));
execaSpy.and.returnValue(Promise.resolve({ stdout: '' }));
});
it('should target the passed device id to adb', () => {
return Adb.install(deviceId).then(() => {
const args = spawnSpy.calls.argsFor(0);
const args = execaSpy.calls.argsFor(0);
expect(args[0]).toBe('adb');
const adbArgs = args[1].join(' ');
@@ -96,7 +96,7 @@ emulator-5554\tdevice
it('should add the -r flag if opts.replace is set', () => {
return Adb.install(deviceId, '', { replace: true }).then(() => {
const adbArgs = spawnSpy.calls.argsFor(0)[1];
const adbArgs = execaSpy.calls.argsFor(0)[1];
expect(adbArgs).toContain('-r');
});
});
@@ -105,13 +105,13 @@ emulator-5554\tdevice
const packagePath = 'build/test/app.apk';
return Adb.install(deviceId, packagePath).then(() => {
const adbArgs = spawnSpy.calls.argsFor(0)[1];
const adbArgs = execaSpy.calls.argsFor(0)[1];
expect(adbArgs).toContain(packagePath);
});
});
it('should reject with a CordovaError if the adb output suggests a failure', () => {
spawnSpy.and.returnValue(Promise.resolve(alreadyExistsError));
execaSpy.and.returnValue(Promise.resolve({ stdout: alreadyExistsError }));
return Adb.install(deviceId, '').then(
() => fail('Unexpectedly resolved'),
@@ -124,7 +124,7 @@ emulator-5554\tdevice
// The following two tests are somewhat brittle as they are dependent on the
// exact message returned. But it is better to have them tested than not at all.
it('should give a more specific error message if there is a certificate failure', () => {
spawnSpy.and.returnValue(Promise.resolve(certificateError));
execaSpy.and.returnValue(Promise.resolve({ stdout: certificateError }));
return Adb.install(deviceId, '').then(
() => fail('Unexpectedly resolved'),
@@ -136,7 +136,7 @@ emulator-5554\tdevice
});
it('should give a more specific error message if there is a downgrade error', () => {
spawnSpy.and.returnValue(Promise.resolve(downgradeError));
execaSpy.and.returnValue(Promise.resolve({ stdout: downgradeError }));
return Adb.install(deviceId, '').then(
() => fail('Unexpectedly resolved'),
@@ -151,10 +151,10 @@ emulator-5554\tdevice
describe('uninstall', () => {
it('should call adb uninstall with the correct arguments', () => {
const packageId = 'io.cordova.test';
spawnSpy.and.returnValue(Promise.resolve(''));
execaSpy.and.returnValue(Promise.resolve({ stdout: '' }));
return Adb.uninstall(deviceId, packageId).then(() => {
const args = spawnSpy.calls.argsFor(0);
const args = execaSpy.calls.argsFor(0);
expect(args[0]).toBe('adb');
const adbArgs = args[1];
@@ -169,10 +169,10 @@ emulator-5554\tdevice
const shellCommand = 'ls -l /sdcard';
it('should run the passed command on the target device', () => {
spawnSpy.and.returnValue(Promise.resolve(''));
execaSpy.and.returnValue(Promise.resolve({ stdout: '' }));
return Adb.shell(deviceId, shellCommand).then(() => {
const args = spawnSpy.calls.argsFor(0);
const args = execaSpy.calls.argsFor(0);
expect(args[0]).toBe('adb');
const adbArgs = args[1].join(' ');
@@ -184,7 +184,7 @@ emulator-5554\tdevice
it('should reject with a CordovaError on failure', () => {
const errorMessage = 'shell error';
spawnSpy.and.returnValue(Promise.reject(errorMessage));
execaSpy.and.rejectWith(new Error(errorMessage));
return Adb.shell(deviceId, shellCommand).then(
() => fail('Unexpectedly resolved'),
@@ -214,7 +214,7 @@ emulator-5554\tdevice
it('should reject with a CordovaError on a shell error', () => {
const errorMessage = 'Test Start error';
spyOn(Adb, 'shell').and.returnValue(Promise.reject(errorMessage));
spyOn(Adb, 'shell').and.rejectWith(new CordovaError(errorMessage));
return Adb.start(deviceId, activityName).then(
() => fail('Unexpectedly resolved'),
+7 -5
View File
@@ -17,16 +17,18 @@
under the License.
*/
const superspawn = require('cordova-common').superspawn;
const fs = require('fs');
const path = require('path');
const rewire = require('rewire');
describe('android_sdk', () => {
let android_sdk;
let execaSpy;
beforeEach(() => {
android_sdk = rewire('../../bin/templates/cordova/lib/android_sdk');
execaSpy = jasmine.createSpy('execa');
android_sdk.__set__('execa', execaSpy);
});
describe('sort_by_largest_numerical_suffix', () => {
@@ -59,14 +61,14 @@ describe('android_sdk', () => {
describe('list_targets_with_android', () => {
it('should invoke `android` with the `list target` command and _not_ the `list targets` command, as the plural form is not supported in some Android SDK Tools versions', () => {
spyOn(superspawn, 'spawn').and.returnValue(new Promise(() => {}, () => {}));
execaSpy.and.returnValue(Promise.resolve({ stdout: '' }));
android_sdk.list_targets_with_android();
expect(superspawn.spawn).toHaveBeenCalledWith('android', ['list', 'target']);
expect(execaSpy).toHaveBeenCalledWith('android', ['list', 'target']);
});
it('should parse and return results from `android list targets` command', () => {
const testTargets = fs.readFileSync(path.join('spec', 'fixtures', 'sdk25.2-android_list_targets.txt'), 'utf-8');
spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(testTargets));
execaSpy.and.returnValue(Promise.resolve({ stdout: testTargets }));
return android_sdk.list_targets_with_android().then(list => {
[ 'Google Inc.:Google APIs:23',
@@ -87,7 +89,7 @@ describe('android_sdk', () => {
describe('list_targets_with_avdmanager', () => {
it('should parse and return results from `avdmanager list target` command', () => {
const testTargets = fs.readFileSync(path.join('spec', 'fixtures', 'sdk25.3-avdmanager_list_target.txt'), 'utf-8');
spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(testTargets));
execaSpy.and.returnValue(Promise.resolve({ stdout: testTargets }));
return android_sdk.list_targets_with_avdmanager().then(list => {
expect(list).toContain('android-25');
+18 -19
View File
@@ -19,7 +19,6 @@
const fs = require('fs');
const path = require('path');
const Q = require('q');
const rewire = require('rewire');
const CordovaError = require('cordova-common').CordovaError;
@@ -29,12 +28,12 @@ describe('ProjectBuilder', () => {
let builder;
let ProjectBuilder;
let spawnSpy;
let execaSpy;
beforeEach(() => {
spawnSpy = jasmine.createSpy('spawn').and.returnValue(Q.defer().promise);
execaSpy = jasmine.createSpy('execa').and.returnValue(new Promise(() => {}));
ProjectBuilder = rewire('../../../bin/templates/cordova/lib/builders/ProjectBuilder');
ProjectBuilder.__set__('spawn', spawnSpy);
ProjectBuilder.__set__('execa', execaSpy);
builder = new ProjectBuilder(rootDir);
});
@@ -120,13 +119,13 @@ describe('ProjectBuilder', () => {
it('should run the provided gradle command if a gradle wrapper does not already exist', () => {
spyOn(fs, 'existsSync').and.returnValue(false);
builder.runGradleWrapper('/my/sweet/gradle');
expect(spawnSpy).toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
expect(execaSpy).toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
});
it('should do nothing if a gradle wrapper exists in the project directory', () => {
spyOn(fs, 'existsSync').and.returnValue(true);
builder.runGradleWrapper('/my/sweet/gradle');
expect(spawnSpy).not.toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
expect(execaSpy).not.toHaveBeenCalledWith('/my/sweet/gradle', jasmine.any(Array), jasmine.any(Object));
});
});
@@ -182,34 +181,34 @@ describe('ProjectBuilder', () => {
builder.build({});
expect(spawnSpy).toHaveBeenCalledWith(path.join(rootDir, 'gradlew'), testArgs, jasmine.anything());
expect(execaSpy).toHaveBeenCalledWith(path.join(rootDir, 'gradlew'), testArgs, jasmine.anything());
});
it('should reject if the spawn fails', () => {
const errorMessage = 'ERROR: Failed to spawn';
spawnSpy.and.returnValue(Q.reject(errorMessage));
const errorMessage = 'Test error';
execaSpy.and.rejectWith(new Error(errorMessage));
return builder.build({}).then(
() => fail('Unexpectedly resolved'),
err => {
expect(err).toBe(errorMessage);
error => {
expect(error.message).toBe(errorMessage);
}
);
});
it('should check the Android target if failed to find target', () => {
const checkReqsSpy = jasmine.createSpyObj('check_reqs', ['check_android_target']);
const errorMessage = 'ERROR: failed to find target with hash string';
const testError = 'failed to find target with hash string';
ProjectBuilder.__set__('check_reqs', checkReqsSpy);
checkReqsSpy.check_android_target.and.returnValue(Q.resolve());
spawnSpy.and.returnValue(Q.reject(errorMessage));
checkReqsSpy.check_android_target.and.returnValue(Promise.resolve());
execaSpy.and.rejectWith(testError);
return builder.build({}).then(
() => fail('Unexpectedly resolved'),
err => {
expect(checkReqsSpy.check_android_target).toHaveBeenCalledWith(errorMessage);
expect(err).toBe(errorMessage);
error => {
expect(checkReqsSpy.check_android_target).toHaveBeenCalledWith(testError);
expect(error).toBe(testError);
}
);
});
@@ -222,7 +221,7 @@ describe('ProjectBuilder', () => {
shellSpy = jasmine.createSpyObj('shell', ['rm']);
ProjectBuilder.__set__('shell', shellSpy);
spyOn(builder, 'getArgs');
spawnSpy.and.returnValue(Promise.resolve());
execaSpy.and.returnValue(Promise.resolve());
});
it('should get arguments for cleaning', () => {
@@ -238,7 +237,7 @@ describe('ProjectBuilder', () => {
builder.getArgs.and.returnValue(gradleArgs);
return builder.clean(opts).then(() => {
expect(spawnSpy).toHaveBeenCalledWith(path.join(rootDir, 'gradlew'), gradleArgs, jasmine.anything());
expect(execaSpy).toHaveBeenCalledWith(path.join(rootDir, 'gradlew'), gradleArgs, jasmine.anything());
});
});
+6 -6
View File
@@ -42,12 +42,12 @@ describe('device', () => {
});
it('should kill adb and try to get devices again if none are found the first time, and `lookHarder` is set', () => {
const spawnSpy = jasmine.createSpy('spawn').and.returnValue(Promise.resolve());
device.__set__('spawn', spawnSpy);
const execaSpy = jasmine.createSpy('execa').and.returnValue(Promise.resolve());
device.__set__('execa', execaSpy);
AdbSpy.devices.and.returnValues(Promise.resolve([]), Promise.resolve(DEVICE_LIST));
return device.list(true).then(list => {
expect(spawnSpy).toHaveBeenCalledWith('killall', ['adb']);
expect(execaSpy).toHaveBeenCalledWith('killall', ['adb']);
expect(list).toBe(DEVICE_LIST);
expect(AdbSpy.devices).toHaveBeenCalledTimes(2);
});
@@ -55,12 +55,12 @@ describe('device', () => {
it('should return the empty list if killing adb fails', () => {
const emptyDevices = [];
const spawnSpy = jasmine.createSpy('spawn').and.returnValue(Promise.reject());
device.__set__('spawn', spawnSpy);
const execaSpy = jasmine.createSpy('execa').and.returnValue(Promise.reject());
device.__set__('execa', execaSpy);
AdbSpy.devices.and.returnValues(Promise.resolve(emptyDevices));
return device.list(true).then(list => {
expect(spawnSpy).toHaveBeenCalledWith('killall', ['adb']);
expect(execaSpy).toHaveBeenCalledWith('killall', ['adb']);
expect(list).toBe(emptyDevices);
expect(AdbSpy.devices).toHaveBeenCalledTimes(1);
});
+29 -31
View File
@@ -24,7 +24,6 @@ const shelljs = require('shelljs');
const CordovaError = require('cordova-common').CordovaError;
const check_reqs = require('../../bin/templates/cordova/lib/check_reqs');
const superspawn = require('cordova-common').superspawn;
describe('emulator', () => {
const EMULATOR_LIST = ['emulator-5555', 'emulator-5556', 'emulator-5557'];
@@ -37,7 +36,9 @@ describe('emulator', () => {
describe('list_images_using_avdmanager', () => {
it('should properly parse details of SDK Tools 25.3.1 `avdmanager` output', () => {
const avdList = fs.readFileSync(path.join('spec', 'fixtures', 'sdk25.3-avdmanager_list_avd.txt'), 'utf-8');
spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(avdList));
let execaSpy = jasmine.createSpy('execa').and.returnValue(Promise.resolve({ stdout: avdList }));
emu.__set__('execa', execaSpy);
return emu.list_images_using_avdmanager().then(list => {
expect(list).toBeDefined();
@@ -51,14 +52,18 @@ describe('emulator', () => {
describe('list_images_using_android', () => {
it('should invoke `android` with the `list avd` command and _not_ the `list avds` command, as the plural form is not supported in some Android SDK Tools versions', () => {
spyOn(superspawn, 'spawn').and.returnValue(new Promise(() => {}, () => {}));
let execaSpy = jasmine.createSpy('execa').and.returnValue(Promise.resolve({ stdout: '' }));
emu.__set__('execa', execaSpy);
emu.list_images_using_android();
expect(superspawn.spawn).toHaveBeenCalledWith('android', ['list', 'avd']);
expect(execaSpy).toHaveBeenCalledWith('android', ['list', 'avd']);
});
it('should properly parse details of SDK Tools pre-25.3.1 `android list avd` output', () => {
const avdList = fs.readFileSync(path.join('spec', 'fixtures', 'sdk25.2-android_list_avd.txt'), 'utf-8');
spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(avdList));
let execaSpy = jasmine.createSpy('execa').and.returnValue(Promise.resolve({ stdout: avdList }));
emu.__set__('execa', execaSpy);
return emu.list_images_using_android().then(list => {
expect(list).toBeDefined();
@@ -249,7 +254,7 @@ describe('emulator', () => {
let emulator;
let AdbSpy;
let checkReqsSpy;
let childProcessSpy;
let execaSpy;
let shellJsSpy;
beforeEach(() => {
@@ -268,9 +273,10 @@ describe('emulator', () => {
checkReqsSpy = jasmine.createSpyObj('create_reqs', ['getAbsoluteAndroidCmd']);
emu.__set__('check_reqs', checkReqsSpy);
childProcessSpy = jasmine.createSpyObj('child_process', ['spawn']);
childProcessSpy.spawn.and.returnValue(jasmine.createSpyObj('spawnFns', ['unref']));
emu.__set__('child_process', childProcessSpy);
execaSpy = jasmine.createSpy('execa').and.returnValue(
jasmine.createSpyObj('spawnFns', ['unref'])
);
emu.__set__('execa', execaSpy);
spyOn(emu, 'get_available_port').and.returnValue(Promise.resolve(port));
spyOn(emu, 'wait_for_emulator').and.returnValue(Promise.resolve('randomname'));
@@ -291,7 +297,7 @@ describe('emulator', () => {
return emu.start().then(() => {
// This is the earliest part in the code where we can hook in and check
// the emulator that has been selected.
const spawnArgs = childProcessSpy.spawn.calls.argsFor(0);
const spawnArgs = execaSpy.calls.argsFor(0);
expect(spawnArgs[1]).toContain(emulator.name);
});
});
@@ -302,7 +308,7 @@ describe('emulator', () => {
return emu.start(emulator.name).then(() => {
expect(emu.best_image).not.toHaveBeenCalled();
const spawnArgs = childProcessSpy.spawn.calls.argsFor(0);
const spawnArgs = execaSpy.calls.argsFor(0);
expect(spawnArgs[1]).toContain(emulator.name);
});
});
@@ -576,7 +582,7 @@ describe('emulator', () => {
let AndroidManifestGetActivitySpy;
let AdbSpy;
let buildSpy;
let childProcessSpy;
let execaSpy;
let target;
beforeEach(() => {
@@ -597,9 +603,8 @@ describe('emulator', () => {
AdbSpy.uninstall.and.returnValue(Promise.resolve());
emu.__set__('Adb', AdbSpy);
childProcessSpy = jasmine.createSpyObj('child_process', ['exec']);
childProcessSpy.exec.and.callFake((cmd, opts, callback) => callback());
emu.__set__('child_process', childProcessSpy);
execaSpy = jasmine.createSpy('execa').and.resolveTo({});
emu.__set__('execa', execaSpy);
});
it('should get the full target object if only id is specified', () => {
@@ -613,7 +618,7 @@ describe('emulator', () => {
it('should install to the passed target', () => {
return emu.install(target, {}).then(() => {
const execCmd = childProcessSpy.exec.calls.argsFor(0)[0];
const execCmd = execaSpy.calls.argsFor(0)[1].join(' ');
expect(execCmd).toContain(`-s ${target.target} install`);
});
});
@@ -631,33 +636,26 @@ describe('emulator', () => {
return emu.install(target, buildResults).then(() => {
expect(buildSpy.findBestApkForArchitecture).toHaveBeenCalledWith(buildResults, target.arch);
const execCmd = childProcessSpy.exec.calls.argsFor(0)[0];
expect(execCmd).toMatch(new RegExp(`install.*${apkPath}`));
const execCmd = execaSpy.calls.argsFor(0)[1].join(' ');
expect(execCmd).toContain(`install -r ${apkPath}`);
});
});
it('should uninstall and reinstall app if failure is due to different certificates', () => {
let execAlreadyCalled;
childProcessSpy.exec.and.callFake((cmd, opts, callback) => {
if (!execAlreadyCalled) {
execAlreadyCalled = true;
callback(null, 'Failure: INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES');
} else {
callback();
}
});
execaSpy.and.returnValues(
...['Failure: INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES', '']
.map(out => Promise.resolve({ stdout: out }))
);
return emu.install(target, {}).then(() => {
expect(childProcessSpy.exec).toHaveBeenCalledTimes(2);
expect(execaSpy).toHaveBeenCalledTimes(2);
expect(AdbSpy.uninstall).toHaveBeenCalled();
});
});
it('should throw any error not caused by different certificates', () => {
const errorMsg = 'Failure: Failed to install';
childProcessSpy.exec.and.callFake((cmd, opts, callback) => {
callback(null, errorMsg);
});
execaSpy.and.resolveTo({ stdout: errorMsg });
return emu.install(target, {}).then(
() => fail('Unexpectedly resolved'),