From 550b531a4f12cdde264ae2dee4f26cdb1ec02b82 Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Wed, 25 Jul 2018 22:36:06 +0300 Subject: [PATCH] fix: adds grep fallback for win32 --- detox/package.json | 1 + detox/src/devices/android/AAPT.js | 7 ++++- detox/src/devices/android/ADB.js | 7 +++-- detox/src/utils/pipeCommands.js | 41 +++++++++++++++++++++++++++++ detox/src/utils/regexEscape.js | 7 ----- detox/src/utils/regexEscape.test.js | 23 ---------------- 6 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 detox/src/utils/pipeCommands.js delete mode 100644 detox/src/utils/regexEscape.test.js diff --git a/detox/package.json b/detox/package.json index 52488997d4..c308e928f7 100644 --- a/detox/package.json +++ b/detox/package.json @@ -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", diff --git a/detox/src/devices/android/AAPT.js b/detox/src/devices/android/AAPT.js index 6e05e34bcf..86ad4730b8 100644 --- a/detox/src/devices/android/AAPT.js +++ b/detox/src/devices/android/AAPT.js @@ -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 { @@ -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]; } diff --git a/detox/src/devices/android/ADB.js b/detox/src/devices/android/ADB.js index 4232760a40..a1f47431d2 100644 --- a/detox/src/devices/android/ADB.js +++ b/detox/src/devices/android/ADB.js @@ -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'); @@ -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; } diff --git a/detox/src/utils/pipeCommands.js b/detox/src/utils/pipeCommands.js new file mode 100644 index 0000000000..0653c7f1dc --- /dev/null +++ b/detox/src/utils/pipeCommands.js @@ -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(); diff --git a/detox/src/utils/regexEscape.js b/detox/src/utils/regexEscape.js index 0dd0bf93e1..e69de29bb2 100644 --- a/detox/src/utils/regexEscape.js +++ b/detox/src/utils/regexEscape.js @@ -1,7 +0,0 @@ -const SPECIAL_CHARS = /([\^\$\[\]\*\.\\])/g; - -function regexEscape(exactString) { - return exactString.replace(SPECIAL_CHARS, "\\$1"); -} - -module.exports = regexEscape; \ No newline at end of file diff --git a/detox/src/utils/regexEscape.test.js b/detox/src/utils/regexEscape.test.js deleted file mode 100644 index 97cb44b798..0000000000 --- a/detox/src/utils/regexEscape.test.js +++ /dev/null @@ -1,23 +0,0 @@ -const regexEscape = require('./regexEscape'); - -describe(regexEscape.name, () => { - it('should not escape non-special characters', () => { - const nonspecial = 'bundle_name'; - expect(regexEscape(nonspecial)).toBe(nonspecial); - }); - - it('should escape [\\]', () => { - const bundleId = '[kworker\\0:0]'; - expect(regexEscape(bundleId)).toBe('\\[kworker\\\\0:0\\]'); - }); - - it('should escape ^*$', () => { - const bundleId = '^ma*tch$'; - expect(regexEscape(bundleId)).toBe('\\^ma\\*tch\\$'); - }); - - it('should escape dots', () => { - const bundleId = 'com.company.bundle'; - expect(regexEscape(bundleId)).toBe('com\\.company\\.bundle'); - }); -});