Skip to content

Commit 50bfa73

Browse files
committed
fix(android): handle \r\n for adb output on Windows during --list
fixes #24
1 parent 58c8b75 commit 50bfa73

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

src/android/utils/__tests__/adb.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import * as adbUtils from '../adb';
1+
import * as os from 'os';
22

33
describe('android/utils/adb', () => {
44

55
describe('parseAdbDevices', () => {
66

7+
let adbUtils: typeof import('../adb');
8+
9+
beforeEach(async () => {
10+
jest.resetModules();
11+
adbUtils = await import('../adb');
12+
});
13+
714
it('should parse emulator-5554 device', async () => {
815
const output = `
916
List of devices attached
@@ -80,6 +87,39 @@ List of devices attached
8087
]);
8188
});
8289

90+
describe('windows', () => {
91+
92+
let adbUtils: typeof import('../adb');
93+
94+
beforeEach(async () => {
95+
jest.resetModules();
96+
jest.mock('os', () => ({ ...os, EOL: '\r\n' }));
97+
98+
adbUtils = await import('../adb');
99+
});
100+
101+
it('should parse hardware device (MWS0216B24001482)', async () => {
102+
const output = `\r\nList of devices attached\r\nMWS0216B24001482 offline transport_id:3\r\n\r\n`;
103+
const devices = await adbUtils.parseAdbDevices(output);
104+
105+
expect(devices).toEqual([
106+
{
107+
serial: 'MWS0216B24001482',
108+
state: 'offline',
109+
type: 'emulator',
110+
manufacturer: '',
111+
model: '',
112+
product: '',
113+
sdkVersion: '',
114+
properties: {
115+
transport_id: '3',
116+
},
117+
},
118+
]);
119+
});
120+
121+
});
122+
83123
});
84124

85125
});

src/android/utils/adb.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { spawn } from 'child_process';
22
import * as Debug from 'debug';
3+
import * as os from 'os';
34
import * as path from 'path';
45
import * as split2 from 'split2';
56
import * as through2 from 'through2';
@@ -93,7 +94,7 @@ export async function getDeviceProperties(sdk: SDK, device: Device): Promise<Dev
9394
const { stdout } = await execFile(adbBin, args, { env: supplementProcessEnv(sdk) });
9495
const properties: DeviceProperties = {};
9596

96-
for (const line of stdout.split('\n')) {
97+
for (const line of stdout.split(os.EOL)) {
9798
const m = line.match(re);
9899

99100
if (m) {
@@ -253,7 +254,7 @@ export async function startActivity(sdk: SDK, device: Device, packageName: strin
253254
export function parseAdbDevices(output: string): Device[] {
254255
const debug = Debug(`${modulePrefix}:${parseAdbDevices.name}`);
255256
const re = /^([\S]+)\s+([a-z\s]+)\s+(.*)$/;
256-
const lines = output.split('\n');
257+
const lines = output.split(os.EOL);
257258

258259
const devices: Device[] = [];
259260

src/android/utils/sdk/__tests__/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ describe('android/utils/sdk', () => {
3737

3838
it('should default to windows 10 local app data directory', async () => {
3939
Object.defineProperty(process, 'env', { value: {} });
40-
sdkUtils = require('../');
40+
sdkUtils = await import('../');
4141
expect(sdkUtils.SDK_DIRECTORIES.get('win32')).toEqual([path.win32.join('C:\\Users\\me\\AppData\\Local\\Android\\Sdk')]);
4242
});
4343

4444
it('should use LOCALAPPDATA environment variable if present', async () => {
4545
Object.defineProperty(process, 'env', { value: { LOCALAPPDATA: path.win32.join('C:\\', 'Documents and Settings', 'me', 'Application Data') } });
46-
sdkUtils = require('../');
46+
sdkUtils = await import('../');
4747
expect(sdkUtils.SDK_DIRECTORIES.get('win32')).toEqual([path.win32.join('C:\\Documents and Settings\\me\\Application Data\\Android\\Sdk')]);
4848
});
4949

@@ -53,8 +53,8 @@ describe('android/utils/sdk', () => {
5353

5454
describe('resolveSDKRoot', () => {
5555

56-
beforeEach(() => {
57-
sdkUtils = require('../');
56+
beforeEach(async () => {
57+
sdkUtils = await import('../');
5858
});
5959

6060
it('should resolve with ANDROID_HOME if in environment', async () => {

0 commit comments

Comments
 (0)