Skip to content

Commit

Permalink
feat: get user installed bundleids by bundlename (#242)
Browse files Browse the repository at this point in the history
* get user installed bundle ids

* return {} if no the path, add tests

* continue if no infoplist file path

* move some tests

* catch exception
  • Loading branch information
KazuCocoa committed Sep 1, 2019
1 parent 58ee01a commit c1eb716
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
40 changes: 39 additions & 1 deletion lib/simulator-xcode-6.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import * as simctl from 'node-simctl';
import { default as xcode, getPath as getXcodePath } from 'appium-xcode';
import log from './logger';
import { fs, tempDir, mkdirp } from 'appium-support';
import { fs, tempDir, mkdirp, plist } from 'appium-support';
import B from 'bluebird';
import _ from 'lodash';
import AsyncLock from 'async-lock';
Expand Down Expand Up @@ -185,6 +185,39 @@ class SimulatorXcode6 extends EventEmitter {
return appDirs.length !== 0;
}

/**
* Returns user installed bundle ids which has 'bundleName' in their Info.Plist as 'CFBundleName'
* @param {string} bundleId - The bundle id of the application to be checked.
* @return {array<string>} - The list of bundle ids which have 'bundleName'
*/
async getUserInstalledBundleIdsByBundleName (bundleName) {
const rootUserAppDir = await this.buildBundlePathMap('Bundle');
const bundleIds = [];
if (_.isEmpty(rootUserAppDir)) {
return bundleIds;
}

for (const [bundleId, userAppDirPath] of Object.entries(rootUserAppDir)) {
const appFile = (await fs.readdir(userAppDirPath)).find(
(file) => path.extname(file).toLowerCase() === '.app');
const infoPlistPath = path.resolve(userAppDirPath, appFile, 'Info.plist');
if (!await fs.exists(infoPlistPath)) {
continue;
}
try {
const infoPlist = await plist.parsePlistFile(infoPlistPath, false);
if (infoPlist.CFBundleName === bundleName) {
bundleIds.push(bundleId);
}
} catch (err) {
log.warn(`Failed to read plist ${infoPlistPath}. Original error '${err.message}'`);
continue;
}
}
log.debug(`The simulator has '${bundleIds}' which have '${bundleName}' as their 'CFBundleName'`);
return bundleIds;
}

/**
* Retrieve the directory for a particular application's data.
*
Expand Down Expand Up @@ -245,6 +278,11 @@ class SimulatorXcode6 extends EventEmitter {
};
}

if (!await fs.exists(applicationList)) {
log.warn(`No directory path '${applicationList}'`);
return {};
}

let bundlePathDirs = await fs.readdir(applicationList);
let bundlePathPairs = await asyncmap(bundlePathDirs, async (dir) => {
return await pathBundlePair(dir);
Expand Down
5 changes: 5 additions & 0 deletions test/functional/simulator-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ function runTests (deviceType) {
dirs.should.have.length(2);
dirs[0].should.contain('/Data/');
dirs[1].should.contain('/Bundle/');

await sim.getUserInstalledBundleIdsByBundleName('TestApp').should.eventually.not.empty;
});

it('should be able to delete an app', async function () {
Expand Down Expand Up @@ -482,6 +484,9 @@ function runTests (deviceType) {
await B.map(simulators, (sim) => verifyStates(sim, false, false));
});

// Should be called before launching simulator
await simulators[0].getUserInstalledBundleIdsByBundleName('UICatalog').should.eventually.eql([]);

for (const sim of _.values(simulatorsMapping)) {
await sim.run({startupTimeout: LONG_TIMEOUT});
}
Expand Down

0 comments on commit c1eb716

Please sign in to comment.