From f6b6188eafc30e5b23129577eee686c7d948a207 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 25 Jan 2024 17:34:08 +0100 Subject: [PATCH] chore: Update getStrings implementation to avoid FS usage (#907) --- lib/commands/resources.js | 52 +++++++++++++++++---------------------- lib/driver.ts | 3 --- package.json | 2 +- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/lib/commands/resources.js b/lib/commands/resources.js index a8e1c390..2f10422c 100644 --- a/lib/commands/resources.js +++ b/lib/commands/resources.js @@ -1,6 +1,5 @@ -import path from 'node:path'; import _ from 'lodash'; -import {fs, util} from '@appium/support'; +import {fs, tempDir} from '@appium/support'; /** * @this {import('../driver').AndroidDriver} @@ -25,11 +24,7 @@ export async function getStrings(language = null) { return result; }; - if (!this.apkStrings[language]) { - this.apkStrings[language] = await extractStringsFromResources.bind(this)(language); - } - const mapping = JSON.parse(await fs.readFile(this.apkStrings[language], 'utf-8')); - return preprocessStringsMap(mapping); + return preprocessStringsMap(await extractStringsFromResources.bind(this)(language)); } /** @@ -56,36 +51,35 @@ export async function ensureDeviceLocale(language, country, script) { * @this {import('../driver').AndroidDriver} * @param {string?} [language] * @param {import('../driver').AndroidDriverOpts?} [opts=null] - * @returns {Promise}; + * @returns {Promise}; */ async function extractStringsFromResources(language, opts = null) { const caps = opts ?? this.opts; /** @type {string|undefined} */ - let app; + let app = caps.app; + let tmpRoot; try { - app = - caps.app || - (caps.appPackage && caps.tmpDir && (await this.adb.pullApk(caps.appPackage, caps.tmpDir))); - } catch (err) { - throw new Error( - `Failed to pull an apk from '${caps.appPackage}' to '${caps.tmpDir}'. Original error: ${err.message}`, - ); - } + if (!app && caps.appPackage) { + tmpRoot = await tempDir.openDir(); + try { + app = await this.adb.pullApk(caps.appPackage, tmpRoot); + } catch (e) { + throw new Error( + `Could not extract app strings, failed to pull an apk from '${caps.appPackage}'. Original error: ${e.message}` + ); + } + } - if (!app || !(await fs.exists(app))) { - throw new Error(`Could not extract app strings, no app or package specified`); - } + if (!app || !(await fs.exists(app))) { + throw new Error(`Could not extract app strings, no app or package specified`); + } - const stringsTmpDir = path.resolve(String(caps.tmpDir), util.uuidV4()); - try { - this.log.debug( - `Extracting strings from '${app}' for the language '${language || 'default'}' into '${stringsTmpDir}'`, - ); - const {localPath} = await this.adb.extractStringsFromApk(app, language ?? null, stringsTmpDir); - return localPath; - } catch (err) { - throw new Error(`Could not extract app strings. Original error: ${err.message}`); + return (await this.adb.extractStringsFromApk(app, language ?? null)).apkStrings; + } finally { + if (tmpRoot) { + await fs.rimraf(tmpRoot); + } } } diff --git a/lib/driver.ts b/lib/driver.ts index 3f8c0261..3ca7f0d8 100644 --- a/lib/driver.ts +++ b/lib/driver.ts @@ -223,8 +223,6 @@ class AndroidDriver _settingsApp: SettingsApp; - apkStrings: StringRecord; - proxyReqRes?: (...args: any) => any; contexts?: string[]; @@ -266,7 +264,6 @@ class AndroidDriver this.desiredCapConstraints = _.cloneDeep(ANDROID_DRIVER_CONSTRAINTS); this.sessionChromedrivers = {}; this.jwpProxyActive = false; - this.apkStrings = {}; this.curContext = this.defaultContextName(); this.opts = opts as AndroidDriverOpts; diff --git a/package.json b/package.json index 1b0247f5..5fff0ab1 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "dependencies": { "@appium/support": "^4.2.0", "@colors/colors": "^1.6.0", - "appium-adb": "^12.0.0", + "appium-adb": "^12.0.2", "appium-chromedriver": "^5.5.1", "asyncbox": "^3.0.0", "axios": "^1.x",