Skip to content

Commit

Permalink
chore: Only fetch resource paths when they are requested (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jan 14, 2023
1 parent fed30f6 commit 64dabb1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
45 changes: 37 additions & 8 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { system, fs, zip, util, tempDir, node } from '@appium/support';
import { system, fs, zip, util, tempDir } from '@appium/support';
import log from './logger.js';
import _ from 'lodash';
import B from 'bluebird';
Expand All @@ -16,17 +16,46 @@ const LAUNCHER_CATEGORY = 'android.intent.category.LAUNCHER';
const MODULE_NAME = 'appium-adb';

/**
* Calculates the path to the current module's root folder
* Calculates the absolute path to the current module's root folder
*
* @returns {string} The full path to module root
* @returns {Promise<string>} The full path to module root
* @throws {Error} If the current module root folder cannot be determined
*/
const getModuleRoot = _.memoize(function getModuleRoot () {
const root = node.getModuleRootSync(MODULE_NAME, __filename);
if (!root) {
const getModuleRoot = _.memoize(async function getModuleRoot () {
let moduleRoot = path.dirname(path.resolve(__filename));
let isAtFsRoot = false;
while (!isAtFsRoot) {
const manifestPath = path.join(moduleRoot, 'package.json');
try {
if (await fs.exists(manifestPath) &&
JSON.parse(await fs.readFile(manifestPath, 'utf8')).name === MODULE_NAME) {
return moduleRoot;
}
} catch (ign) {}
moduleRoot = path.dirname(moduleRoot);
isAtFsRoot = moduleRoot.length <= path.dirname(moduleRoot).length;
}
if (isAtFsRoot) {
throw new Error(`Cannot find the root folder of the ${MODULE_NAME} Node.js module`);
}
return root;
return moduleRoot;
});

/**
* Calculates the absolsute path to the given resource
*
* @param {string} relPath Relative path to the resource starting from the current module root
* @returns {Promise<string>} The full path to the resource
* @throws {Error} If the absolute resource path cannot be determined
*/
const getResourcePath = _.memoize(async function getResourcePath (relPath) {
const moduleRoot = await getModuleRoot();
const resultPath = path.resolve(moduleRoot, relPath);
if (!await fs.exists(resultPath)) {
throw new Error(`Cannot find the resource '${relPath}' under the '${moduleRoot}' ` +
`folder of ${MODULE_NAME} Node.js module`);
}
return resultPath;
});

/**
Expand Down Expand Up @@ -1049,5 +1078,5 @@ export {
DEFAULT_ADB_EXEC_TIMEOUT, parseManifest, parseAaptStrings, parseAapt2Strings,
formatConfigMarker, parseJsonData, unsignApk, toAvdLocaleArgs, requireSdkRoot,
getSdkRootFromEnv, getAndroidPrefsRoot, dirExists, escapeShellArg,
parseLaunchableActivityNames, matchComponentName, getModuleRoot
parseLaunchableActivityNames, matchComponentName, getResourcePath
};
10 changes: 5 additions & 5 deletions lib/tools/apk-signing.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import log from '../logger.js';
import { tempDir, system, mkdirp, fs, util } from '@appium/support';
import LRU from 'lru-cache';
import {
getJavaForOs, getApksignerForOs, getJavaHome, getModuleRoot, APKS_EXTENSION, unsignApk,
getJavaForOs, getApksignerForOs, getJavaHome, getResourcePath, APKS_EXTENSION, unsignApk,
} from '../helpers.js';

const DEFAULT_PRIVATE_KEY = path.resolve(getModuleRoot(), 'keys', 'testkey.pk8');
const DEFAULT_CERTIFICATE = path.resolve(getModuleRoot(), 'keys', 'testkey.x509.pem');
const DEFAULT_PRIVATE_KEY = path.join('keys', 'testkey.pk8');
const DEFAULT_CERTIFICATE = path.join('keys', 'testkey.x509.pem');
const BUNDLETOOL_TUTORIAL = 'https://developer.android.com/studio/command-line/bundletool';
const APKSIGNER_VERIFY_FAIL = 'DOES NOT VERIFY';
const SHA1 = 'sha1';
Expand Down Expand Up @@ -78,8 +78,8 @@ apkSigningMethods.signWithDefaultCert = async function signWithDefaultCert (apk)

const args = [
'sign',
'--key', DEFAULT_PRIVATE_KEY,
'--cert', DEFAULT_CERTIFICATE,
'--key', await getResourcePath(DEFAULT_PRIVATE_KEY),
'--cert', await getResourcePath(DEFAULT_CERTIFICATE),
apk,
];
try {
Expand Down
6 changes: 3 additions & 3 deletions test/functional/syscalls-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import chaiAsPromised from 'chai-as-promised';
import ADB from '../../lib/adb';
import { apiLevel, avdName, MOCHA_TIMEOUT, MOCHA_LONG_TIMEOUT } from './setup';
import path from 'path';
import { getModuleRoot } from '../../lib/helpers.js';
import { getResourcePath } from '../../lib/helpers.js';
import { fs } from '@appium/support';
import _ from 'lodash';

const DEFAULT_CERTIFICATE = path.resolve(getModuleRoot(), 'keys', 'testkey.x509.pem');
const DEFAULT_CERTIFICATE = path.join('keys', 'testkey.x509.pem');

chai.use(chaiAsPromised);

Expand Down Expand Up @@ -88,7 +88,7 @@ describe('System calls', function () {
(await adb.ls('/data/local/tmp')).should.contain('test');
});
it('should check if the given certificate is already installed', async function () {
const certBuffer = await fs.readFile(DEFAULT_CERTIFICATE);
const certBuffer = await fs.readFile(await getResourcePath(DEFAULT_CERTIFICATE));
(await adb.isMitmCertificateInstalled(certBuffer)).should.be.false;
});
it('should return version', async function () {
Expand Down

0 comments on commit 64dabb1

Please sign in to comment.