Skip to content

Commit

Permalink
chore: Update getStrings implementation to avoid FS usage (#907)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jan 25, 2024
1 parent 64a174b commit f6b6188
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
52 changes: 23 additions & 29 deletions lib/commands/resources.js
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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));
}

/**
Expand All @@ -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<string>};
* @returns {Promise<import('@appium/types').StringRecord>};
*/
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);
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions lib/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ class AndroidDriver

_settingsApp: SettingsApp;

apkStrings: StringRecord;

proxyReqRes?: (...args: any) => any;

contexts?: string[];
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f6b6188

Please sign in to comment.