Skip to content

Commit

Permalink
fix(android): handle \r\n for adb output on Windows during --list
Browse files Browse the repository at this point in the history
fixes #24
  • Loading branch information
imhoffd committed May 30, 2019
1 parent 58c8b75 commit 50bfa73
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
42 changes: 41 additions & 1 deletion src/android/utils/__tests__/adb.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import * as adbUtils from '../adb';
import * as os from 'os';

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

describe('parseAdbDevices', () => {

let adbUtils: typeof import('../adb');

beforeEach(async () => {
jest.resetModules();
adbUtils = await import('../adb');
});

it('should parse emulator-5554 device', async () => {
const output = `
List of devices attached
Expand Down Expand Up @@ -80,6 +87,39 @@ List of devices attached
]);
});

describe('windows', () => {

let adbUtils: typeof import('../adb');

beforeEach(async () => {
jest.resetModules();
jest.mock('os', () => ({ ...os, EOL: '\r\n' }));

adbUtils = await import('../adb');
});

it('should parse hardware device (MWS0216B24001482)', async () => {
const output = `\r\nList of devices attached\r\nMWS0216B24001482 offline transport_id:3\r\n\r\n`;
const devices = await adbUtils.parseAdbDevices(output);

expect(devices).toEqual([
{
serial: 'MWS0216B24001482',
state: 'offline',
type: 'emulator',
manufacturer: '',
model: '',
product: '',
sdkVersion: '',
properties: {
transport_id: '3',
},
},
]);
});

});

});

});
5 changes: 3 additions & 2 deletions src/android/utils/adb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawn } from 'child_process';
import * as Debug from 'debug';
import * as os from 'os';
import * as path from 'path';
import * as split2 from 'split2';
import * as through2 from 'through2';
Expand Down Expand Up @@ -93,7 +94,7 @@ export async function getDeviceProperties(sdk: SDK, device: Device): Promise<Dev
const { stdout } = await execFile(adbBin, args, { env: supplementProcessEnv(sdk) });
const properties: DeviceProperties = {};

for (const line of stdout.split('\n')) {
for (const line of stdout.split(os.EOL)) {
const m = line.match(re);

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

const devices: Device[] = [];

Expand Down
8 changes: 4 additions & 4 deletions src/android/utils/sdk/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ describe('android/utils/sdk', () => {

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

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

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

describe('resolveSDKRoot', () => {

beforeEach(() => {
sdkUtils = require('../');
beforeEach(async () => {
sdkUtils = await import('../');
});

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

0 comments on commit 50bfa73

Please sign in to comment.