Skip to content

Commit 0fab27c

Browse files
elyalvaradofacebook-github-bot
authored andcommitted
Allow specifying iOS version for run-ios with simulator option (#19079)
Summary: Fixes #19069 The --simulator option for the run-ios command now can take an optional iOS version between parenthesis to further match the desired simulator. This is useful if you have installed simulators for different iOS versions and you want to run the app in an especific one. Example: react-native run-ios --simulator "iPhone 6s (9.3)" Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. Updated tests for the findMatchingSimulator function to include test cases specifying iOS version, and tested on the command line in my app to make sure it has the expected behavior. [CLI] [ENHANCEMENT] [{/runIOS/findMatchingSimulator.js}] - run-ios command with the --simulator option now allows specifying the iOS version to run an specific simulator if you have multiple versions of the simulator installed. Example: `react-native run-ios --simulator "iPhone 6s (9.3)"`. Pull Request resolved: #19079 Differential Revision: D10432487 Pulled By: hramos fbshipit-source-id: efa50d798b79d83bfe357ee17967a56c7c003bee
1 parent 1c240ae commit 0fab27c

File tree

3 files changed

+154
-5
lines changed

3 files changed

+154
-5
lines changed

local-cli/runIOS/__tests__/findMatchingSimulator-test.js

+133
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,139 @@ describe('findMatchingSimulator', () => {
539539
});
540540
});
541541

542+
it('should return the simulator with the specified version (multi ios versions)', () => {
543+
expect(findMatchingSimulator({
544+
'devices': {
545+
'iOS 9.2': [
546+
{
547+
'state': 'Shutdown',
548+
'availability': '(unavailable, runtime profile not found)',
549+
'name': 'iPhone 4s',
550+
'udid': 'B9B5E161-416B-43C4-A78F-729CB96CC8C6'
551+
},
552+
{
553+
'state': 'Shutdown',
554+
'availability': '(available)',
555+
'name': 'iPhone 5',
556+
'udid': '1CCBBF8B-5773-4EA6-BD6F-C308C87A1ADB'
557+
},
558+
{
559+
'state': 'Shutdown',
560+
'availability': '(available)',
561+
'name': 'iPhone 6',
562+
'udid': 'BA0D93BD-07E6-4182-9B0A-F60A2474139C'
563+
},
564+
{
565+
'state': 'Shutdown',
566+
'availability': '(available)',
567+
'name': 'iPhone 6 (Plus)',
568+
'udid': '9564ABEE-9EC2-4B4A-B443-D3710929A45A'
569+
},
570+
{
571+
'state': 'Shutdown',
572+
'availability': '(available)',
573+
'name': 'iPhone 6s',
574+
'udid': 'D0F29BE7-CC3C-4976-888D-C739B4F50508'
575+
}
576+
],
577+
'iOS 10.0': [
578+
{
579+
'state': 'Shutdown',
580+
'availability': '(available)',
581+
'name': 'iPhone 6',
582+
'udid': '2FF48AE5-CC3B-4C80-8D25-48966A6BE2C0'
583+
},
584+
{
585+
'state': 'Shutdown',
586+
'availability': '(available)',
587+
'name': 'iPhone 6 (Plus)',
588+
'udid': '841E33FE-E8A1-4B65-9FF8-6EAA6442A3FC'
589+
},
590+
{
591+
'state': 'Shutdown',
592+
'availability': '(available)',
593+
'name': 'iPhone 6s',
594+
'udid': 'CBBB8FB8-77AB-49A9-8297-4CCFE3189C22'
595+
},
596+
{
597+
'state': 'Booted',
598+
'availability': '(available)',
599+
'name': 'iPhone 7',
600+
'udid': '3A409DC5-5188-42A6-8598-3AA6F34607A5'
601+
}
602+
]
603+
}
604+
},
605+
'iPhone 6s (10.0)'
606+
)).toEqual({
607+
udid: 'CBBB8FB8-77AB-49A9-8297-4CCFE3189C22',
608+
name: 'iPhone 6s',
609+
booted: false,
610+
version: 'iOS 10.0'
611+
});
612+
});
613+
614+
it('should return null if the version is specified and no device with the exact version exists (multi ios versions)', () => {
615+
expect(findMatchingSimulator({
616+
'devices': {
617+
'iOS 9.2': [
618+
{
619+
'state': 'Shutdown',
620+
'availability': '(unavailable, runtime profile not found)',
621+
'name': 'iPhone 4s',
622+
'udid': 'B9B5E161-416B-43C4-A78F-729CB96CC8C6'
623+
},
624+
{
625+
'state': 'Shutdown',
626+
'availability': '(available)',
627+
'name': 'iPhone 5',
628+
'udid': '1CCBBF8B-5773-4EA6-BD6F-C308C87A1ADB'
629+
},
630+
{
631+
'state': 'Shutdown',
632+
'availability': '(available)',
633+
'name': 'iPhone 6',
634+
'udid': 'BA0D93BD-07E6-4182-9B0A-F60A2474139C'
635+
},
636+
{
637+
'state': 'Shutdown',
638+
'availability': '(available)',
639+
'name': 'iPhone 6 (Plus)',
640+
'udid': '9564ABEE-9EC2-4B4A-B443-D3710929A45A'
641+
},
642+
{
643+
'state': 'Shutdown',
644+
'availability': '(available)',
645+
'name': 'iPhone 6s',
646+
'udid': 'D0F29BE7-CC3C-4976-888D-C739B4F50508'
647+
}
648+
],
649+
'iOS 10.0': [
650+
{
651+
'state': 'Shutdown',
652+
'availability': '(available)',
653+
'name': 'iPhone 6',
654+
'udid': '2FF48AE5-CC3B-4C80-8D25-48966A6BE2C0'
655+
},
656+
{
657+
'state': 'Shutdown',
658+
'availability': '(available)',
659+
'name': 'iPhone 6 (Plus)',
660+
'udid': '841E33FE-E8A1-4B65-9FF8-6EAA6442A3FC'
661+
},
662+
{
663+
'state': 'Booted',
664+
'availability': '(available)',
665+
'name': 'iPhone 7',
666+
'udid': '3A409DC5-5188-42A6-8598-3AA6F34607A5'
667+
}
668+
]
669+
}
670+
},
671+
'iPhone 6s (10.0)'
672+
)).toEqual(null);
673+
});
674+
542675
it('should return AppleTV devices if in the list', () => {
543676
expect(
544677
findMatchingSimulator(

local-cli/runIOS/findMatchingSimulator.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,41 @@
1010
'use strict';
1111

1212
/**
13-
* Takes in a parsed simulator list and a desired name, and returns an object with the matching simulator.
13+
* Takes in a parsed simulator list and a desired name, and returns an object with the matching simulator. The desired
14+
* name can optionally include the iOS version in between parenthesis after the device name. Ex: "iPhone 6 (9.2)" in
15+
* which case it'll attempt to find a simulator with the exact version specified.
1416
*
15-
* If the simulatorName argument is null, we'll go into default mode and return the currently booted simulator, or if
17+
* If the simulatorString argument is null, we'll go into default mode and return the currently booted simulator, or if
1618
* none is booted, it will be the first in the list.
1719
*
1820
* @param Object simulators a parsed list from `xcrun simctl list --json devices` command
19-
* @param String|null simulatorName the string with the name of desired simulator. If null, it will use the currently
21+
* @param String|null simulatorString the string with the name of desired simulator. If null, it will use the currently
2022
* booted simulator, or if none are booted, the first in the list.
2123
* @returns {Object} {udid, name, version}
2224
*/
23-
function findMatchingSimulator(simulators, simulatorName) {
25+
function findMatchingSimulator(simulators, simulatorString) {
2426
if (!simulators.devices) {
2527
return null;
2628
}
2729
const devices = simulators.devices;
30+
31+
const parsedSimulatorName = simulatorString ? simulatorString.match(/(.*)? (?:\((.*)?\))?/) : [];
32+
if (parsedSimulatorName[2] !== undefined) {
33+
var simulatorVersion = parsedSimulatorName[2];
34+
var simulatorName = parsedSimulatorName[1];
35+
} else {
36+
simulatorName = simulatorString;
37+
}
38+
2839
var match;
2940
for (let version in devices) {
3041
// Making sure the version of the simulator is an iOS or tvOS (Removes Apple Watch, etc)
3142
if (!version.startsWith('iOS') && !version.startsWith('tvOS')) {
3243
continue;
3344
}
45+
if (simulatorVersion && !version.endsWith(simulatorVersion)) {
46+
continue;
47+
}
3448
for (let i in devices[version]) {
3549
let simulator = devices[version][i];
3650
// Skipping non-available simulator

local-cli/runIOS/runIOS.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ module.exports = {
409409
options: [
410410
{
411411
command: '--simulator [string]',
412-
description: 'Explicitly set simulator to use',
412+
description:
413+
'Explicitly set simulator to use. Optionally include iOS version between' +
414+
'parenthesis at the end to match an exact version: "iPhone 6 (10.0)"',
413415
default: 'iPhone X',
414416
},
415417
{

0 commit comments

Comments
 (0)