You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2020/11/19 20:31:02 UTC

[cordova-android] branch master updated: feat(Adb): list devices _and_ emulators in one go (#1125)

This is an automated email from the ASF dual-hosted git repository.

raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git


The following commit(s) were added to refs/heads/master by this push:
     new aa679ea  feat(Adb): list devices _and_ emulators in one go (#1125)
aa679ea is described below

commit aa679ea1d6ddd63ce4a8d64925637e75482d5374
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Thu Nov 19 21:30:56 2020 +0100

    feat(Adb): list devices _and_ emulators in one go (#1125)
---
 bin/templates/cordova/lib/Adb.js      | 36 +++++++++++--------------
 bin/templates/cordova/lib/device.js   |  5 ++--
 bin/templates/cordova/lib/emulator.js |  6 ++---
 spec/unit/Adb.spec.js                 | 51 ++++++++++-------------------------
 spec/unit/device.spec.js              |  6 ++---
 spec/unit/emulator.spec.js            |  8 +++---
 6 files changed, 42 insertions(+), 70 deletions(-)

diff --git a/bin/templates/cordova/lib/Adb.js b/bin/templates/cordova/lib/Adb.js
index a78a6ab..9326269 100644
--- a/bin/templates/cordova/lib/Adb.js
+++ b/bin/templates/cordova/lib/Adb.js
@@ -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 = {} } = {}) {
diff --git a/bin/templates/cordova/lib/device.js b/bin/templates/cordova/lib/device.js
index dedaf71..ede9151 100644
--- a/bin/templates/cordova/lib/device.js
+++ b/bin/templates/cordova/lib/device.js
@@ -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) {
diff --git a/bin/templates/cordova/lib/emulator.js b/bin/templates/cordova/lib/emulator.js
index e7b2491..3ae0288 100644
--- a/bin/templates/cordova/lib/emulator.js
+++ b/bin/templates/cordova/lib/emulator.js
@@ -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-'));
 };
 
 /*
diff --git a/spec/unit/Adb.spec.js b/spec/unit/Adb.spec.js
index ac681e4..aafe977 100644
--- a/spec/unit/Adb.spec.js
+++ b/spec/unit/Adb.spec.js
@@ -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'
+                ]);
             });
         });
     });
diff --git a/spec/unit/device.spec.js b/spec/unit/device.spec.js
index 6340d86..5eb166c 100644
--- a/spec/unit/device.spec.js
+++ b/spec/unit/device.spec.js
@@ -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']);
             });
         });
     });
diff --git a/spec/unit/emulator.spec.js b/spec/unit/emulator.spec.js
index 70ae5c2..943525a 100644
--- a/spec/unit/emulator.spec.js
+++ b/spec/unit/emulator.spec.js
@@ -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']);
             });
         });
     });


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org