Skip to content

Commit

Permalink
tests: Fix tests after the recent node-simctl update (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Sep 23, 2023
1 parent 41601cc commit 09f39f5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
9 changes: 9 additions & 0 deletions lib/device-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Simctl from 'node-simctl';

/**
* @param {Record<string, any>} [simctlOpts]
* @returns {Promise<any[]>}
*/
export async function getDevices(simctlOpts) {
return await new Simctl(simctlOpts).getDevices();
}
42 changes: 29 additions & 13 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import _ from 'lodash';
import { exec } from 'teen_process';
import { waitForCondition } from 'asyncbox';
import { getVersion } from 'appium-xcode';
import Simctl from 'node-simctl';
import path from 'path';

import { getDevices } from './device-utils';

const DEFAULT_SIM_SHUTDOWN_TIMEOUT_MS = 30000;
const SAFARI_STARTUP_TIMEOUT_MS = 25 * 1000;
Expand Down Expand Up @@ -34,25 +33,34 @@ const BIOMETRICS = {
faceId: 'pearl',
};

/**
* @param {string} name
* @returns {string}
*/
function toBiometricDomainComponent (name) {
if (!BIOMETRICS[name]) {
throw new Error(`'${name}' is not a valid biometric. Use one of: ${JSON.stringify(_.keys(BIOMETRICS))}`);
}
return BIOMETRICS[name];
}

// pgrep/pkill exit codes:
// 0 One or more processes were matched.
// 1 No processes were matched.
// 2 Invalid options were specified on the command line.
// 3 An internal error occurred.
/**
* @param {string} appName
* @param {boolean} [forceKill=false]
* @returns {Promise<number>}
*/
async function pkill (appName, forceKill = false) {
let args = forceKill ? ['-9'] : [];
args.push('-x', appName);
try {
await exec('pkill', args);
return 0;
} catch (err) {
// pgrep/pkill exit codes:
// 0 One or more processes were matched.
// 1 No processes were matched.
// 2 Invalid options were specified on the command line.
// 3 An internal error occurred.
if (!_.isUndefined(err.code)) {
throw new Error(`Cannot forcefully terminate ${appName}. pkill error code: ${err.code}`);
}
Expand All @@ -61,6 +69,10 @@ async function pkill (appName, forceKill = false) {
}
}

/**
* @param {number} [timeout=DEFAULT_SIM_SHUTDOWN_TIMEOUT_MS]
* @returns {Promise<void>}
*/
async function killAllSimulators (timeout = DEFAULT_SIM_SHUTDOWN_TIMEOUT_MS) {
log.debug('Killing all iOS Simulators');
const xcodeVersion = await getVersion(true);
Expand Down Expand Up @@ -108,7 +120,7 @@ async function killAllSimulators (timeout = DEFAULT_SIM_SHUTDOWN_TIMEOUT_MS) {
let remainingDevices = [];
async function allSimsAreDown () {
remainingDevices = [];
let devices = await new Simctl().getDevices();
let devices = await getDevices();
devices = _.flatten(_.values(devices));
return _.every(devices, (sim) => {
const state = sim.state.toLowerCase();
Expand All @@ -135,16 +147,17 @@ async function killAllSimulators (timeout = DEFAULT_SIM_SHUTDOWN_TIMEOUT_MS) {
}
}

/**
* @param {string} udid
* @param {Record<string, any>} [opts={}]
* @returns {Promise<any>}
*/
async function getSimulatorInfo (udid, opts = {}) {
const {
devicesSetPath
} = opts;
// see the README for github.com/appium/node-simctl for example output of getDevices()
let devices = await new Simctl({
devicesSetPath
}).getDevices();

devices = _.toPairs(devices)
const devices = _.toPairs(await getDevices({devicesSetPath}))
.map((pair) => pair[1])
.reduce((a, b) => a.concat(b), []);
return _.find(devices, (sim) => sim.udid === udid);
Expand All @@ -158,6 +171,9 @@ async function simExists (udid) {
return !!(await getSimulatorInfo(udid));
}

/**
* @returns {Promise<string>}
*/
async function getDeveloperRoot () {
const {stdout} = await exec('xcode-select', ['-p']);
return stdout.trim();
Expand Down
23 changes: 10 additions & 13 deletions test/unit/simulator-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { getSimulator } from '../../lib/simulator';
import * as teenProcess from 'teen_process';
import Simctl from 'node-simctl';
import * as deviceUtils from '../../lib/device-utils';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
Expand All @@ -28,12 +28,12 @@ describe('simulator', function () {

beforeEach(function () {
xcodeMock = sinon.mock(xcode);
getDevicesStub = sinon.stub(Simctl.prototype, 'getDevices');
getDevicesStub = sinon.stub(deviceUtils, 'getDevices');
getDevicesStub.returns(B.resolve(devices));
});
afterEach(function () {
xcodeMock.restore();
Simctl.prototype.getDevices.restore();
getDevicesStub.restore();
});

describe('getSimulator', function () {
Expand Down Expand Up @@ -79,18 +79,15 @@ describe('simulator', function () {
let xcodeVersion = {major: 8, versionString: '8.0.0'};
xcodeMock.expects('getVersion').atLeast(1).returns(B.resolve(xcodeVersion));

let sims = [
getSimulator('F33783B2-9EE9-4A99-866E-E126ADBAD410'),
getSimulator('DFBC2970-9455-4FD9-BB62-9E4AE5AA6954'),
];

let stats = sims.map(function (simProm) {
// eslint-disable-next-line promise/prefer-await-to-then
return simProm.then((sim) => sim.stat());
const sims = (await B.all([
'F33783B2-9EE9-4A99-866E-E126ADBAD410',
'DFBC2970-9455-4FD9-BB62-9E4AE5AA6954',
].map(getSimulator))).map((sim) => {
sinon.stub(sim.simctl, 'getDevices').returns(B.resolve(devices));
return sim;
});

stats = await B.all(stats);

const stats = await B.all(sims.map((sim) => sim.stat()));
stats[0].state.should.equal('Shutdown');
stats[0].name.should.equal('Resizable iPhone');
stats[1].state.should.equal('Shutdown');
Expand Down
6 changes: 3 additions & 3 deletions test/unit/utils-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import sinon from 'sinon';
import B from 'bluebird';
import * as TeenProcess from 'teen_process';
import xcode from 'appium-xcode';
import Simctl from 'node-simctl';
import {
toBiometricDomainComponent, killAllSimulators, simExists,
} from '../../lib/utils';
import * as deviceUtils from '../../lib/device-utils';
import { devices } from '../assets/deviceList';
import SimulatorXcode9 from '../../lib/simulator-xcode-9';

Expand Down Expand Up @@ -47,13 +47,13 @@ describe('util', function () {
beforeEach(function () {
execStub = sinon.stub(TeenProcess, 'exec');
xcodeMock = sinon.mock(xcode);
getDevicesStub = sinon.stub(Simctl.prototype, 'getDevices');
getDevicesStub = sinon.stub(deviceUtils, 'getDevices');
getDevicesStub.returns(B.resolve(devices));
});
afterEach(function () {
execStub.restore();
xcodeMock.restore();
Simctl.prototype.getDevices.restore();
getDevicesStub.restore();
});

describe('killAllSimulators', function () {
Expand Down

0 comments on commit 09f39f5

Please sign in to comment.