Skip to content

Commit

Permalink
fix: adds grep fallback for win32
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Jul 25, 2018
1 parent 78575f0 commit 289bde7
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 33 deletions.
7 changes: 6 additions & 1 deletion detox/src/devices/android/AAPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const _ = require('lodash');
const Environment = require('../../utils/environment');
const path = require('path');
const exec = require('../../utils/exec').execWithRetriesAndLogs;
const egrep = require('../../utils/pipeCommands').search.fragment;
const fsext = require('../../utils/fsext');

class AAPT {
Expand All @@ -21,7 +22,11 @@ class AAPT {

async getPackageName(apkPath) {
await this._prepare();
const process = await exec(`${this.aaptBin} dump badging "${apkPath}" | grep package:.name=`);
const process = await exec(
`${this.aaptBin} dump badging "${apkPath}" | ${egrep("package: name=")}`,
undefined, undefined, 1
);

const packageName = new RegExp(/package: name='([^']+)'/g).exec(process.stdout);
return packageName[1];
}
Expand Down
7 changes: 5 additions & 2 deletions detox/src/devices/android/ADB.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const path = require('path');
const {execWithRetriesAndLogs, spawnAndLog} = require('../../utils/exec');
const regexEscape = require('../../utils/regexEscape');
const pipeCommands = require('../../utils/pipeCommands');
const EmulatorTelnet = require('./EmulatorTelnet');
const Environment = require('../../utils/environment');

Expand Down Expand Up @@ -75,7 +75,10 @@ class ADB {
}

async pidof(deviceId, bundleId) {
const processes = await this.shell(deviceId, `ps | grep "${regexEscape(bundleId)}\\s*$"`).catch(() => '');
const bundleIdRegex = pipeCommands.escape.inQuotedRegexp(bundleId) + '\\s*$';
const grep = pipeCommands.search.regexp;

const processes = await this.shell(deviceId, `ps | ${grep(bundleIdRegex)}`).catch(() => '');
if (!processes) {
return NaN;
}
Expand Down
25 changes: 25 additions & 0 deletions detox/src/utils/__snapshots__/pipeCommands.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`pipeUtils escape in quoted regexp on *nix should escape [\\] 1`] = `"\\\\[kworker\\\\\\\\0:0\\\\]"`;

exports[`pipeUtils escape in quoted regexp on *nix should escape ^*$ 1`] = `"\\\\^ma\\\\*tch\\\\$"`;

exports[`pipeUtils escape in quoted regexp on *nix should escape dots 1`] = `"com\\\\.company\\\\.bundle"`;

exports[`pipeUtils escape in quoted regexp on *nix should escape double quotes with slash 1`] = `"\\\\\\"a"`;

exports[`pipeUtils escape in quoted regexp on *nix should not escape non-special characters 1`] = `"bundle_name"`;

exports[`pipeUtils escape in quoted regexp on win32 should escape only double quotes 1`] = `"^Hello\\\\s \\"\\".*\\"\\"$"`;

exports[`pipeUtils escape in quoted string on *nix should escape double quotes with slash 1`] = `"Says \\\\\\"Hello\\\\\\""`;

exports[`pipeUtils escape in quoted string on win32 should escape double quotes with double quotes 1`] = `"Says \\"\\"Hello\\"\\""`;

exports[`pipeUtils search by fragment pipe command on *nix should use grep -e "fragment" 1`] = `"grep -e \\"^Hello\\\\s \\\\\\".*\\\\\\"$\\""`;

exports[`pipeUtils search by fragment pipe command on win32 should use findstr /C:"fragment" 1`] = `"findstr /C:\\"^Hello\\\\s \\"\\".*\\"\\"$\\""`;

exports[`pipeUtils search by regexp pipe command on *nix should use grep "regexp" 1`] = `"grep \\"^Hello\\\\s \\\\\\".*\\\\\\"$\\""`;

exports[`pipeUtils search by regexp pipe command on win32 should use findstr /R /C:"regexp" 1`] = `"findstr /R /C:\\"^Hello\\\\s \\"\\".*\\"\\"$\\""`;
46 changes: 46 additions & 0 deletions detox/src/utils/pipeCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function win32Implementation() {
const escapeInQuotedStringWin32 = (fragment) => fragment.replace(/"/g, '""');
const escapeInQuotedRegexpWin32 = escapeInQuotedStringWin32;
const searchRegexpWin32 = (pattern) => `findstr /R /C:"${escapeInQuotedStringWin32(pattern)}"`;
const searchFragmentWin32 = (fragment) => `findstr /C:"${escapeInQuotedStringWin32(fragment)}"`;

return {
escape: {
inQuotedString: escapeInQuotedStringWin32,
inQuotedRegexp: escapeInQuotedRegexpWin32,
},
search: {
regexp: searchRegexpWin32,
fragment: searchFragmentWin32,
},
};
}

function nixImplementation() {
const SPECIAL_CHARS = /(["\^\$\[\]\*\.\\])/g;

const escapeInQuotedStringNix = (fragment) => fragment.replace(/"/g, '\\"');
const escapeInQuotedRegexpNix = (fragment) => fragment.replace(SPECIAL_CHARS, "\\$1");
const searchRegexpNix = (pattern) => `grep "${escapeInQuotedStringNix(pattern)}"`;
const searchFragmentNix = (fragment) => `grep -e "${escapeInQuotedStringNix(fragment)}"`;

return {
escape: {
inQuotedString: escapeInQuotedStringNix,
inQuotedRegexp: escapeInQuotedRegexpNix,
},
search: {
regexp: searchRegexpNix,
fragment: searchFragmentNix,
},
};
}

// istanbul ignore next
module.exports = {
...(process.platform === 'win32' ? win32Implementation() : nixImplementation()),

_win32: win32Implementation,
_nix: nixImplementation,
};

86 changes: 86 additions & 0 deletions detox/src/utils/pipeCommands.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const pipeCommands = require('./pipeCommands');

const _win32 = pipeCommands._win32();
const _nix = pipeCommands._nix();

describe('pipeUtils', () => {
describe('escape in quoted string', () => {
describe('on win32', () => {
const escape = _win32.escape.inQuotedString;

it('should escape double quotes with double quotes', () =>
expect(escape('Says "Hello"')).toMatchSnapshot());
});

describe('on *nix', () => {
const escape = _nix.escape.inQuotedString;

it('should escape double quotes with slash', () =>
expect(escape('Says "Hello"')).toMatchSnapshot());
});
});

describe('escape in quoted regexp', () => {
describe('on win32', () => {
const escape = _win32.escape.inQuotedRegexp;

it('should escape only double quotes', () =>
expect(escape('^Hello\\s ".*"$')).toMatchSnapshot());
});

describe('on *nix', () => {
const escape = _nix.escape.inQuotedRegexp;

it('should not escape non-special characters', () =>
expect(escape('bundle_name')).toMatchSnapshot());

it('should escape double quotes with slash', () =>
expect(escape('"a')).toMatchSnapshot());

it('should escape [\\]', () =>
expect(escape('[kworker\\0:0]')).toMatchSnapshot());

it('should escape ^*$', () =>
expect(escape('^ma*tch$')).toMatchSnapshot());

it('should escape dots', () =>
expect(escape('com.company.bundle')).toMatchSnapshot());
});
});

describe('search by regexp pipe command', () => {
const regexp = '^Hello\\s ".*"$';

describe('on win32', () => {
const search = _win32.search.regexp;

it('should use findstr /R /C:"regexp"', () =>
expect(search(regexp)).toMatchSnapshot());
});

describe('on *nix', () => {
const search = _nix.search.regexp;

it('should use grep "regexp"', () =>
expect(search(regexp)).toMatchSnapshot());
});
});

describe('search by fragment pipe command', () => {
const fragment = '^Hello\\s ".*"$';

describe('on win32', () => {
const search = _win32.search.fragment;

it('should use findstr /C:"fragment"', () =>
expect(search(fragment)).toMatchSnapshot());
});

describe('on *nix', () => {
const search = _nix.search.fragment;

it('should use grep -e "fragment"', () =>
expect(search(fragment)).toMatchSnapshot());
});
});
});
7 changes: 0 additions & 7 deletions detox/src/utils/regexEscape.js
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
const SPECIAL_CHARS = /([\^\$\[\]\*\.\\])/g;

function regexEscape(exactString) {
return exactString.replace(SPECIAL_CHARS, "\\$1");
}

module.exports = regexEscape;
23 changes: 0 additions & 23 deletions detox/src/utils/regexEscape.test.js

This file was deleted.

0 comments on commit 289bde7

Please sign in to comment.