forked from github/cordova-android
feat(Adb): list devices _and_ emulators in one go (#1125)
This commit is contained in:
parent
0e8234abfd
commit
aa679ea1d6
36
bin/templates/cordova/lib/Adb.js
vendored
36
bin/templates/cordova/lib/Adb.js
vendored
@ -24,33 +24,27 @@ var CordovaError = require('cordova-common').CordovaError;
|
||||
|
||||
var Adb = {};
|
||||
|
||||
function isDevice (line) {
|
||||
return line.match(/\w+\tdevice/) && !line.match(/emulator/);
|
||||
}
|
||||
|
||||
function isEmulator (line) {
|
||||
return line.match(/device/) && line.match(/emulator/);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists available/connected devices and emulators
|
||||
*
|
||||
* @param {Object} opts Various options
|
||||
* @param {Boolean} opts.emulators Specifies whether this method returns
|
||||
* emulators only
|
||||
*
|
||||
* @return {Promise<String[]>} list of available/connected
|
||||
* devices/emulators
|
||||
*/
|
||||
Adb.devices = function (opts) {
|
||||
return execa('adb', ['devices'], { cwd: os.tmpdir() }).then(({ stdout: output }) => {
|
||||
return output.split('\n').filter(function (line) {
|
||||
// Filter out either real devices or emulators, depending on options
|
||||
return (line && opts && opts.emulators) ? isEmulator(line) : isDevice(line);
|
||||
}).map(function (line) {
|
||||
return line.replace(/\tdevice/, '').replace('\r', '');
|
||||
});
|
||||
});
|
||||
Adb.devices = async function () {
|
||||
const { stdout } = await execa('adb', ['devices'], { cwd: os.tmpdir() });
|
||||
|
||||
// Split into lines & drop first one (header)
|
||||
const rawDeviceLines = stdout.trim().split(/\r?\n/).slice(1);
|
||||
|
||||
return rawDeviceLines
|
||||
.map(line => line.split('\t'))
|
||||
|
||||
// We are only interested in fully booted devices & emulators. These
|
||||
// have a state of `device`. For a list of all the other possible states
|
||||
// see https://github.com/aosp-mirror/platform_system_core/blob/2abdb1eb5b83c8f39874644af576c869815f5c5b/adb/transport.cpp#L1129
|
||||
.filter(([, state]) => state === 'device')
|
||||
|
||||
.map(([id]) => id);
|
||||
};
|
||||
|
||||
Adb.install = function (target, packagePath, { replace = false, execOptions = {} } = {}) {
|
||||
|
5
bin/templates/cordova/lib/device.js
vendored
5
bin/templates/cordova/lib/device.js
vendored
@ -27,8 +27,9 @@ var events = require('cordova-common').events;
|
||||
/**
|
||||
* Returns a promise for the list of the device ID's found
|
||||
*/
|
||||
module.exports.list = function () {
|
||||
return Adb.devices();
|
||||
module.exports.list = async () => {
|
||||
return (await Adb.devices())
|
||||
.filter(id => !id.startsWith('emulator-'));
|
||||
};
|
||||
|
||||
module.exports.resolveTarget = function (target) {
|
||||
|
6
bin/templates/cordova/lib/emulator.js
vendored
6
bin/templates/cordova/lib/emulator.js
vendored
@ -214,9 +214,9 @@ module.exports.best_image = function () {
|
||||
});
|
||||
};
|
||||
|
||||
// Returns a promise.
|
||||
module.exports.list_started = function () {
|
||||
return Adb.devices({ emulators: true });
|
||||
exports.list_started = async () => {
|
||||
return (await Adb.devices())
|
||||
.filter(id => id.startsWith('emulator-'));
|
||||
};
|
||||
|
||||
/*
|
||||
|
@ -20,13 +20,15 @@
|
||||
const CordovaError = require('cordova-common').CordovaError;
|
||||
const rewire = require('rewire');
|
||||
|
||||
describe('Adb', () => {
|
||||
const adbOutput = `List of devices attached
|
||||
const adbOutput = `List of devices attached
|
||||
emulator-5554\tdevice
|
||||
123a76565509e124\tdevice`;
|
||||
const [, emulatorLine, deviceLine] = adbOutput.split('\n');
|
||||
const emulatorId = emulatorLine.split('\t')[0];
|
||||
const deviceId = deviceLine.split('\t')[0];
|
||||
emulator-5556\toffline
|
||||
123a76565509e124\tdevice
|
||||
123a76565509e123\tbootloader
|
||||
`;
|
||||
|
||||
describe('Adb', () => {
|
||||
const deviceId = '123a76565509e124';
|
||||
|
||||
const alreadyExistsError = 'adb: failed to install app.apk: Failure[INSTALL_FAILED_ALREADY_EXISTS]';
|
||||
const certificateError = 'adb: failed to install app.apk: Failure[INSTALL_PARSE_FAILED_NO_CERTIFICATES]';
|
||||
@ -41,40 +43,15 @@ emulator-5554\tdevice
|
||||
Adb.__set__('execa', execaSpy);
|
||||
});
|
||||
|
||||
describe('isDevice', () => {
|
||||
it('should return true for a real device', () => {
|
||||
const isDevice = Adb.__get__('isDevice');
|
||||
|
||||
expect(isDevice(deviceLine)).toBeTruthy();
|
||||
expect(isDevice(emulatorLine)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isEmulator', () => {
|
||||
it('should return true for an emulator', () => {
|
||||
const isEmulator = Adb.__get__('isEmulator');
|
||||
|
||||
expect(isEmulator(emulatorLine)).toBeTruthy();
|
||||
expect(isEmulator(deviceLine)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('devices', () => {
|
||||
beforeEach(() => {
|
||||
execaSpy.and.returnValue(Promise.resolve({ stdout: adbOutput }));
|
||||
});
|
||||
it('should return the IDs of all fully booted devices & emulators', () => {
|
||||
execaSpy.and.resolveTo({ stdout: adbOutput });
|
||||
|
||||
it('should return only devices if no options are specified', () => {
|
||||
return Adb.devices().then(devices => {
|
||||
expect(devices.length).toBe(1);
|
||||
expect(devices[0]).toBe(deviceId);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return only emulators if opts.emulators is true', () => {
|
||||
return Adb.devices({ emulators: true }).then(devices => {
|
||||
expect(devices.length).toBe(1);
|
||||
expect(devices[0]).toBe(emulatorId);
|
||||
expect(devices).toEqual([
|
||||
'emulator-5554',
|
||||
'123a76565509e124'
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -33,11 +33,11 @@ describe('device', () => {
|
||||
});
|
||||
|
||||
describe('list', () => {
|
||||
it('should return the list from adb devices', () => {
|
||||
AdbSpy.devices.and.returnValue(Promise.resolve(DEVICE_LIST));
|
||||
it('should return a list of all connected devices', () => {
|
||||
AdbSpy.devices.and.resolveTo(['emulator-5556', '123a76565509e124']);
|
||||
|
||||
return device.list().then(list => {
|
||||
expect(list).toEqual(DEVICE_LIST);
|
||||
expect(list).toEqual(['123a76565509e124']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -205,13 +205,13 @@ describe('emulator', () => {
|
||||
});
|
||||
|
||||
describe('list_started', () => {
|
||||
it('should call adb devices with the emulators flag', () => {
|
||||
it('should return a list of all online emulators', () => {
|
||||
const AdbSpy = jasmine.createSpyObj('Adb', ['devices']);
|
||||
AdbSpy.devices.and.returnValue(Promise.resolve());
|
||||
AdbSpy.devices.and.resolveTo(['emulator-5556', '123a76565509e124']);
|
||||
emu.__set__('Adb', AdbSpy);
|
||||
|
||||
return emu.list_started().then(() => {
|
||||
expect(AdbSpy.devices).toHaveBeenCalledWith({ emulators: true });
|
||||
return emu.list_started().then(emus => {
|
||||
expect(emus).toEqual(['emulator-5556']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user