Skip to content

Commit

Permalink
fix: Enforce '--user 0' argument if cmd package list packages throw…
Browse files Browse the repository at this point in the history
…s an access error (#761)
  • Loading branch information
mykola-mokhnach authored Aug 6, 2024
1 parent 5555823 commit 89b3348
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ const RESOLVER_ACTIVITY_NAME = 'android/com.android.internal.app.ResolverActivit
*/
apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg, opts = {}) {
const {
user
user,
} = opts;

log.debug(`Getting install status for ${pkg}`);
/** @type {boolean} */
let isInstalled;
if (await this.getApiLevel() < 26) {
try {
Expand All @@ -66,7 +67,18 @@ apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg, opts = {})
if (util.hasValue(user)) {
cmd.push('--user', user);
}
const stdout = await this.shell(cmd);
/** @type {string} */
let stdout;
try {
stdout = await this.shell(cmd);
} catch (e) {
// https://github.com/appium/appium-uiautomator2-driver/issues/810
if (_.includes(e.stderr || e.stdout || e.message, 'access user') && _.isEmpty(user)) {
stdout = await this.shell([...cmd, '--user', '0']);
} else {
throw e;
}
}
isInstalled = new RegExp(`^package:${_.escapeRegExp(pkg)}$`, 'm').test(stdout);
}
log.debug(`'${pkg}' is${!isInstalled ? ' not' : ''} installed`);
Expand Down

0 comments on commit 89b3348

Please sign in to comment.