From 28b066e4cc2ce615b93a47512630e7ea0e1e4afa Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sat, 4 Jan 2025 21:17:12 +0100 Subject: [PATCH] Fix tests --- lib/simulator.js | 21 ++------------------- lib/utils.js | 16 ++++++++++++++++ test/unit/simulator-specs.js | 29 ++++++++++++++--------------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/lib/simulator.js b/lib/simulator.js index b048ed0..819924c 100644 --- a/lib/simulator.js +++ b/lib/simulator.js @@ -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. * @@ -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) { diff --git a/lib/utils.js b/lib/utils.js index 58e9f70..309abdb 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 @@ -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; +} diff --git a/test/unit/simulator-specs.js b/test/unit/simulator-specs.js index f78bca4..4fa349f 100644 --- a/test/unit/simulator-specs.js +++ b/test/unit/simulator-specs.js @@ -1,10 +1,10 @@ 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'; @@ -12,7 +12,7 @@ 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; @@ -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); @@ -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 () { @@ -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', @@ -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(); @@ -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'); });