Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jan 4, 2025
1 parent 528622c commit 28b066e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
21 changes: 2 additions & 19 deletions lib/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@ import { SimulatorXcode11 } from './simulator-xcode-11';
import { SimulatorXcode11_4 } from './simulator-xcode-11.4';
import { SimulatorXcode14 } from './simulator-xcode-14';
import { SimulatorXcode15 } from './simulator-xcode-15';
import { getSimulatorInfo } from './utils';
import { getSimulatorInfo, assertXcodeVersion, MIN_SUPPORTED_XCODE_VERSION } from './utils';
import * as xcode from 'appium-xcode';
import { log } from './logger';

const MIN_SUPPORTED_XCODE_VERSION = 10;

/**
* @template {import('appium-xcode').XcodeVersion} V
* @param {V} xcodeVersion
* @returns {V}
*/
function handleUnsupportedXcode (xcodeVersion) {
if (xcodeVersion.major < MIN_SUPPORTED_XCODE_VERSION) {
throw new Error(
`Tried to use an iOS simulator with xcode version ${xcodeVersion.versionString} but only Xcode version ` +
`${MIN_SUPPORTED_XCODE_VERSION} and up are supported`
);
}
return xcodeVersion;
}

/**
* Finds and returns the corresponding Simulator instance for the given ID.
*
Expand All @@ -42,7 +25,7 @@ export async function getSimulator (udid, opts = {}) {
logger,
} = opts;

const xcodeVersion = handleUnsupportedXcode(
const xcodeVersion = assertXcodeVersion(
/** @type {import('appium-xcode').XcodeVersion} */ (await xcode.getVersion(true))
);
if (checkExistence) {
Expand Down
16 changes: 16 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DEFAULT_SIM_SHUTDOWN_TIMEOUT_MS = 30000;
export const SAFARI_STARTUP_TIMEOUT_MS = 25 * 1000;
export const MOBILE_SAFARI_BUNDLE_ID = 'com.apple.mobilesafari';
export const SIMULATOR_APP_NAME = 'Simulator.app';
export const MIN_SUPPORTED_XCODE_VERSION = 10;

/**
* @param {string} appName
Expand Down Expand Up @@ -145,3 +146,18 @@ export async function getDeveloperRoot () {
const {stdout} = await exec('xcode-select', ['-p']);
return stdout.trim();
}

/**
* @template {import('appium-xcode').XcodeVersion} V
* @param {V} xcodeVersion
* @returns {V}
*/
export function assertXcodeVersion (xcodeVersion) {
if (xcodeVersion.major < MIN_SUPPORTED_XCODE_VERSION) {
throw new Error(
`Tried to use an iOS simulator with xcode version ${xcodeVersion.versionString} but only Xcode version ` +
`${MIN_SUPPORTED_XCODE_VERSION} and up are supported`
);
}
return xcodeVersion;
}
29 changes: 14 additions & 15 deletions test/unit/simulator-specs.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { getSimulator } from '../../lib/simulator';
import * as teenProcess from 'teen_process';
import * as deviceUtils from '../../lib/device-utils';
import * as utils from '../../lib/utils';
import sinon from 'sinon';
import { devices } from '../assets/deviceList';
import B from 'bluebird';
import * as xcode from 'appium-xcode';
import { SimulatorXcode10 } from '../../lib/simulator-xcode-10';
import { SimulatorXcode11 } from '../../lib/simulator-xcode-11';
import { SimulatorXcode11_4 } from '../../lib/simulator-xcode-11.4';

const UDID = devices['10.0'][0].udid;

describe('simulator', function () {
let getVersionStub;
let assertXcodeVersionStub;
let getDevicesStub;
let chai;

Expand All @@ -25,19 +25,19 @@ describe('simulator', function () {
});

beforeEach(function () {
getVersionStub = sinon.stub(xcode, 'getVersion');
assertXcodeVersionStub = sinon.stub(utils, 'assertXcodeVersion');
getDevicesStub = sinon.stub(deviceUtils, 'getDevices');
getDevicesStub.returns(B.resolve(devices));
});
afterEach(function () {
getVersionStub.restore();
assertXcodeVersionStub.restore();
getDevicesStub.restore();
});

describe('getSimulator', function () {
it('should create a simulator with default xcode version', async function () {
let xcodeVersion = {major: 10, versionString: '10.0.0'};
getVersionStub.callsFake(() => B.resolve(xcodeVersion));
assertXcodeVersionStub.callsFake(() => xcodeVersion);

let sim = await getSimulator(UDID);
sim.xcodeVersion.should.equal(xcodeVersion);
Expand All @@ -52,18 +52,17 @@ describe('simulator', function () {

for (const [major, minor, versionString, expectedXcodeClass] of xcodeVersions) {
it(`should create an xcode ${major} simulator with xcode version ${versionString}`, async function () {
let xcodeVersion = {major, minor, versionString};
getVersionStub.callsFake(() => B.resolve(xcodeVersion));
let sim = await getSimulator(UDID);
const xcodeVersion = {major, minor, versionString};
assertXcodeVersionStub.callsFake(() => xcodeVersion);
const sim = await getSimulator(UDID);
sim.xcodeVersion.should.equal(xcodeVersion);
sim.constructor.name.should.be.eql(expectedXcodeClass.name);
});
}

it('should throw an error if xcode version less than 6', async function () {
let xcodeVersion = {major: 5, versionString: '5.4.0'};
getVersionStub.callsFake(() => B.resolve(xcodeVersion));
await getSimulator(UDID).should.eventually.be.rejectedWith('version');
it('should throw an error if xcode version does not match', async function () {
assertXcodeVersionStub.throws();
await getSimulator(UDID).should.eventually.be.rejected;
});

it('should throw an error if udid does not exist', async function () {
Expand All @@ -72,7 +71,7 @@ describe('simulator', function () {

it('should list stats for sim', async function () {
let xcodeVersion = {major: 10, versionString: '10.0.0'};
getVersionStub.callsFake(() => B.resolve(xcodeVersion));
assertXcodeVersionStub.callsFake(() => xcodeVersion);

const sims = (await B.all([
'F33783B2-9EE9-4A99-866E-E126ADBAD410',
Expand Down Expand Up @@ -114,7 +113,7 @@ launchd_s 35621 mwakizaka 16u unix 0x7b7dbedd6d62e84f 0t0 /private/
beforeEach(function () {
sinon.stub(teenProcess, 'exec').callsFake(() => ({ stdout }));
const xcodeVersion = {major: 10, versionString: '10.0.0'};
getVersionStub.callsFake(() => B.resolve(xcodeVersion));
assertXcodeVersionStub.callsFake(() => xcodeVersion);
});
afterEach(function () {
teenProcess.exec.restore();
Expand Down Expand Up @@ -146,7 +145,7 @@ launchd_s 35621 mwakizaka 16u unix 0x7b7dbedd6d62e84f 0t0 /private/
let spawnProcessSpy;
beforeEach(async function () {
const xcodeVersion = {major: 10, versionString: '10.0.0'};
getVersionStub.callsFake(() => B.resolve(xcodeVersion));
assertXcodeVersionStub.callsFake(() => xcodeVersion);
sim = await getSimulator(UDID);
spawnProcessSpy = sinon.stub(sim.simctl, 'spawnProcess');
});
Expand Down

0 comments on commit 28b066e

Please sign in to comment.