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 authored Jul 25, 2018
1 parent 78575f0 commit 550b531
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
1 change: 1 addition & 0 deletions detox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"src/utils/environment.js",
"src/utils/logger.js",
"src/utils/onTerminate.js",
"src/utils/pipeCommands.js",
"src/utils/sleep.js",
"AAPT.js",
"ADB.js",
Expand Down
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
41 changes: 41 additions & 0 deletions detox/src/utils/pipeCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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,
},
};
}

module.exports = process.platform === 'win32'
? win32Implementation()
: nixImplementation();
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 550b531

Please sign in to comment.