feat(Adb): list devices _and_ emulators in one go (#1125)

This commit is contained in:
Raphael von der Grün 2020-11-19 21:30:56 +01:00 committed by GitHub
parent 0e8234abfd
commit aa679ea1d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 70 deletions

View File

@ -24,33 +24,27 @@ var CordovaError = require('cordova-common').CordovaError;
var Adb = {}; 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 * 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 * @return {Promise<String[]>} list of available/connected
* devices/emulators * devices/emulators
*/ */
Adb.devices = function (opts) { Adb.devices = async function () {
return execa('adb', ['devices'], { cwd: os.tmpdir() }).then(({ stdout: output }) => { const { stdout } = await execa('adb', ['devices'], { cwd: os.tmpdir() });
return output.split('\n').filter(function (line) {
// Filter out either real devices or emulators, depending on options // Split into lines & drop first one (header)
return (line && opts && opts.emulators) ? isEmulator(line) : isDevice(line); const rawDeviceLines = stdout.trim().split(/\r?\n/).slice(1);
}).map(function (line) {
return line.replace(/\tdevice/, '').replace('\r', ''); 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 = {} } = {}) { Adb.install = function (target, packagePath, { replace = false, execOptions = {} } = {}) {

View File

@ -27,8 +27,9 @@ var events = require('cordova-common').events;
/** /**
* Returns a promise for the list of the device ID's found * Returns a promise for the list of the device ID's found
*/ */
module.exports.list = function () { module.exports.list = async () => {
return Adb.devices(); return (await Adb.devices())
.filter(id => !id.startsWith('emulator-'));
}; };
module.exports.resolveTarget = function (target) { module.exports.resolveTarget = function (target) {

View File

@ -214,9 +214,9 @@ module.exports.best_image = function () {
}); });
}; };
// Returns a promise. exports.list_started = async () => {
module.exports.list_started = function () { return (await Adb.devices())
return Adb.devices({ emulators: true }); .filter(id => id.startsWith('emulator-'));
}; };
/* /*

View File

@ -20,13 +20,15 @@
const CordovaError = require('cordova-common').CordovaError; const CordovaError = require('cordova-common').CordovaError;
const rewire = require('rewire'); const rewire = require('rewire');
describe('Adb', () => { const adbOutput = `List of devices attached
const adbOutput = `List of devices attached
emulator-5554\tdevice emulator-5554\tdevice
123a76565509e124\tdevice`; emulator-5556\toffline
const [, emulatorLine, deviceLine] = adbOutput.split('\n'); 123a76565509e124\tdevice
const emulatorId = emulatorLine.split('\t')[0]; 123a76565509e123\tbootloader
const deviceId = deviceLine.split('\t')[0]; `;
describe('Adb', () => {
const deviceId = '123a76565509e124';
const alreadyExistsError = 'adb: failed to install app.apk: Failure[INSTALL_FAILED_ALREADY_EXISTS]'; 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]'; 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); 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', () => { describe('devices', () => {
beforeEach(() => { it('should return the IDs of all fully booted devices & emulators', () => {
execaSpy.and.returnValue(Promise.resolve({ stdout: adbOutput })); execaSpy.and.resolveTo({ stdout: adbOutput });
});
it('should return only devices if no options are specified', () => {
return Adb.devices().then(devices => { return Adb.devices().then(devices => {
expect(devices.length).toBe(1); expect(devices).toEqual([
expect(devices[0]).toBe(deviceId); 'emulator-5554',
}); '123a76565509e124'
}); ]);
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);
}); });
}); });
}); });

View File

@ -33,11 +33,11 @@ describe('device', () => {
}); });
describe('list', () => { describe('list', () => {
it('should return the list from adb devices', () => { it('should return a list of all connected devices', () => {
AdbSpy.devices.and.returnValue(Promise.resolve(DEVICE_LIST)); AdbSpy.devices.and.resolveTo(['emulator-5556', '123a76565509e124']);
return device.list().then(list => { return device.list().then(list => {
expect(list).toEqual(DEVICE_LIST); expect(list).toEqual(['123a76565509e124']);
}); });
}); });
}); });

View File

@ -205,13 +205,13 @@ describe('emulator', () => {
}); });
describe('list_started', () => { 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']); const AdbSpy = jasmine.createSpyObj('Adb', ['devices']);
AdbSpy.devices.and.returnValue(Promise.resolve()); AdbSpy.devices.and.resolveTo(['emulator-5556', '123a76565509e124']);
emu.__set__('Adb', AdbSpy); emu.__set__('Adb', AdbSpy);
return emu.list_started().then(() => { return emu.list_started().then(emus => {
expect(AdbSpy.devices).toHaveBeenCalledWith({ emulators: true }); expect(emus).toEqual(['emulator-5556']);
}); });
}); });
}); });