All files / lib/extensions prepare-apps.js

21.05% Statements 4/19
0% Branches 0/4
0% Functions 0/3
22.22% Lines 4/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58          1x   1x       1x                                                           1x                                
import { fs } from 'appium-support';
import log from '../logger';
import path from 'path';
 
 
let extensions = {};
 
extensions.prepareSafari = async function (tmpDir, platformVersion) {
  await this.prepareBuiltInApp("MobileSafari", tmpDir, platformVersion);
};
 
extensions.prepareBuiltInApp = async function (appName, tmpDir, platformVersion) {
  log.debug(`Looking for built in app '${appName}'`);
  let newAppPath = path.resolve(tmpDir, `Appium-${appName}-${platformVersion}.app`);
 
  let stat, appPath;
  try {
    [stat, appPath] = await this.getBuiltInApp(appName);
  } catch (err) {
    try {
      stat = await fs.stat(newAppPath);
      if (stat.isDirectory()) {
        log.debug('Could not find original app, but found the temp ' +
                     'Appium one so using that: ${}');
        return [newAppPath, appPath];
      }
    } catch (err) {
      log.warn(`App is also not at '${newAppPath}'`);
      throw new Error(`Could not find built in app '${appName}' in its home ` +
                      `or temp dir!`);
    }
  }
 
  if (!stat.isDirectory()) {
    throw new Error(`App found but it is not a directory: '${appPath}'`);
  }
 
  log.debug(`Found app, trying to move '${appPath}' to tmp dir '${tmpDir}'`);
  await this.moveBuiltInApp(appName, appPath, newAppPath);
};
 
extensions.getBuiltInApp = async function (appName) {
  let appDir = await this.getAppDir(appName);
  let appPath = path.resolve(appDir, `${appName}.app`);
  log.debug(`Found path for '${appName}': ${appPath}`);
  try {
    let stat = await fs.stat(appPath);
    return [stat, appPath];
  } catch (err) {
    if (err && err.message.indexOf('ENOENT') !== -1) {
      log.errorAndThrow(`App '${appName}' is not at '${appPath}'`);
    }
  }
};
 
 
export default extensions;