Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra step to find config file in case emulator has been renamed #1816

Merged
merged 4 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions detox/src/devices/drivers/EmulatorDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ class EmulatorDriver extends AndroidDriver {
}

async _fixEmulatorConfigIniSkinNameIfNeeded(avdName) {
const avdHome = environment.getAvdHome();
const configFile = path.join(avdHome, `${avdName}.avd`, 'config.ini');
const avdPath = environment.getAvdDir(avdName);
const configFile = path.join(avdPath, 'config.ini');
const config = ini.parse(fs.readFileSync(configFile, 'utf-8'));

if (!config['skin.name']) {
Expand Down
29 changes: 28 additions & 1 deletion detox/src/utils/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const _ = require('lodash');
const fs = require('fs');
const os = require('os');
const path = require('path');
const ini = require('ini');
const _which = require('which');
const exec = require('child-process-promise').exec;
const appdatapath = require('./appdatapath');
Expand Down Expand Up @@ -33,6 +34,20 @@ function getAvdHome() {
return process.env['ANDROID_AVD_HOME'] || path.join(getEmulatorHome(), 'avd');
}

function getAvdDir(avdName) {
const avdIniPath = path.join(getAvdHome(), `${avdName}.ini`);
if (!fs.existsSync(avdIniPath)) {
throwMissingAvdINIError(avdName, avdIniPath)
}

const avdIni = ini.parse(fs.readFileSync(avdIniPath, 'utf-8'));
if (!fs.existsSync(avdIni.path)) {
throwMissingAvdError(avdName, avdIni.path, avdIniPath)
}

return avdIni.path;
}

function getAndroidEmulatorPath() {
const sdkRoot = getAndroidSDKPath();
if (!sdkRoot) {
Expand Down Expand Up @@ -95,7 +110,18 @@ function getAdbPath() {
}

function throwMissingSdkError() {
throw new Error(MISSING_SDK_ERROR);
throw new Error(MISSING_SDK_ERROR);
}

function throwMissingAvdINIError(avdName, avdIniPath) {
throw new Error(`Failed to find INI file for ${avdName} at path: ${avdIniPath}`);
}

function throwMissingAvdError(avdName, avdPath, avdIniPath) {
throw new Error(
`Failed to find AVD ${avdName} directory at path: ${avdPath}\n` +
`Please verify "path" property in the INI file: ${avdIniPath}`
);
}

function throwSdkIntegrityError(sdkRoot, relativeExecutablePath) {
Expand Down Expand Up @@ -139,6 +165,7 @@ module.exports = {
getAaptPath,
getAdbPath,
getAvdHome,
getAvdDir,
getDetoxVersion,
getFrameworkPath,
getAndroidSDKPath,
Expand Down
27 changes: 27 additions & 0 deletions detox/src/utils/environment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ describe('Environment', () => {
});
});

describe('getAvdDir', () => {
let avdHome;

beforeEach(async () => {
avdHome = process.env['ANDROID_AVD_HOME'] = tempfile();
await fs.mkdir(avdHome);
});

afterEach(async () => {
await fs.remove(avdHome);
});

it('should throw error if ${avdHome}/${avdName}.ini does not exist', () => {
expect(() => Environment.getAvdDir('nonExistent')).toThrow(/Failed.*INI.*at path:/);
});

it('should throw error if path specified in INI file does not exist', () => {
fs.writeFileSync(path.join(avdHome, 'MyAVD.ini'), `path=randomPath${Math.random()}`)
expect(() => Environment.getAvdDir('MyAVD')).toThrow(/Failed to find.*randomPath0\./);
});

it('should return path specified in INI file if it exists', () => {
fs.writeFileSync(path.join(avdHome, 'MyAVD.ini'), `path=${avdHome}`)
expect(Environment.getAvdDir('MyAVD')).toBe(avdHome);
});
});

describe('getAndroidSDKPath', () => {
it(`should return empty string if $ANDROID_SDK_ROOT and $ANDROID_HOME both are not set`, () => {
delete process.env.ANDROID_SDK_ROOT;
Expand Down