1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Foundation/Foundation.h>
#include <getopt.h>
#include <string>
namespace {
void PrintUsage() {
fprintf(
stderr,
"Usage: iossim [options] <app_path> <xctest_path>\n"
" where <app_path> is the path to the .app directory and <xctest_path> "
"is the path to an optional xctest bundle. The -i option is disallowed.\n"
"Simplified usage: iossim -i [options] <app_path>\n"
" where <app_path> is the path to the .app directory. The -t option is "
"disallowed.\n"
"\n"
"The -x option allows choosing between iOS and tvOS simulators.\n"
"\n"
"Options:\n"
" -h Print this help message and exit.\n"
" -l Print list of supported devices and runtimes and exit.\n"
" -p Print the device's home directory and exit.\n"
" -w Wipe the device's contents and settings and exit.\n"
"\n"
" -v Be more verbose, showing all the xcrun commands we call.\n"
" -i Use iossim instead of xcodebuild (disables all xctest "
"features). This is incompatible with -t.\n"
" -t Specifies a test or test suite that should be included in the "
"test run. All other tests will be excluded from this run. This is "
"incompatible with -i.\n"
" -e Specifies an environment key=value pair that will be"
" set in the simulated application's environment.\n"
" -c Specifies command line flags to pass to application.\n"
" -k When to kill the iOS Simulator : before, after, both, never "
"(default: both)\n"
"\n"
" -u Specifies the device udid to use. Will use -d, -s values to get "
"devices if not specified.\n"
" -d Specifies the device (must be one of the values from the iOS "
"Simulator's Hardware -> Device menu. Defaults to 'iPhone 6s'.\n"
" -s Specifies the SDK version to use (e.g '9.3'). Will use system "
"default if not specified.\n"
" -x Specifies the desired platform for simulator selection: ios or "
"tvos (default: ios)\n");
}
// Exit status codes.
const int kExitSuccess = EXIT_SUCCESS;
const int kExitInvalidArguments = 2;
// As of XCode 16.2, passing --console causes `xcode simctl launch` to block
// indefinitely if there is stderr output longer than 8192 bytes (e.g. a long
// stack trace if a (D)CHECK is hit).
// Apple's documentation is very vague about the differences between --console
// and --console-pty, but the latter seems to work fine including in the case
// above.
constexpr NSString* kSimCtlLaunchConsoleArg = @"--console-pty";
void LogError(NSString* format, ...) {
va_list list;
va_start(list, format);
NSString* message = [[NSString alloc] initWithFormat:format arguments:list];
NSLog(@"ERROR: %@", message);
va_end(list);
}
} // namespace
typedef enum {
PLATFORM_TYPE_IOS,
PLATFORM_TYPE_TVOS,
} PlatformType;
typedef enum {
KILL_NEVER = 0,
KILL_BEFORE = 1 << 0,
KILL_AFTER = 1 << 1,
KILL_BOTH = KILL_BEFORE | KILL_AFTER,
} SimulatorKill;
// See https://stackoverflow.com/a/51895129 and
// https://github.com/facebook/xctool/pull/159/files.
@interface NSTask (PrivateAPI)
- (void)setStartsNewProcessGroup:(BOOL)startsNewProcessGroup;
@end
// Wrap boiler plate calls to xcrun NSTasks.
@interface XCRunTask : NSObject
- (instancetype)initWithArguments:(NSArray*)arguments;
- (void)run:(bool)verbose;
- (void)launch:(bool)verbose;
- (void)setStandardOutput:(id)output;
- (void)setStandardError:(id)error;
- (int)terminationStatus;
@end
@implementation XCRunTask {
NSTask* __strong _task;
}
- (instancetype)initWithArguments:(NSArray*)arguments {
self = [super init];
if (self) {
_task = [[NSTask alloc] init];
[_task setStartsNewProcessGroup:NO];
_task.launchPath = @"/usr/bin/xcrun";
_task.arguments = arguments;
}
return self;
}
- (void)setStandardOutput:(id)output {
_task.standardOutput = output;
}
- (void)setStandardError:(id)error {
_task.standardError = error;
}
- (int)terminationStatus {
return _task.terminationStatus;
}
- (void)run:(bool)verbose {
if (verbose) {
NSLog(@"Running xcrun %@", [_task.arguments componentsJoinedByString:@" "]);
}
[_task launch];
[_task waitUntilExit];
}
- (void)launch:(bool)verbose {
if (verbose) {
NSLog(@"Running xcrun %@", [_task.arguments componentsJoinedByString:@" "]);
}
[_task launch];
}
- (void)waitUntilExit {
[_task waitUntilExit];
}
@end
// Return array of available iOS and tvOS runtime dictionaries. Unavailable
// (old Xcode versions) or other runtimes (e.g. watchOS) are removed.
NSArray* Runtimes(NSDictionary* simctl_list, PlatformType platform_type) {
NSString* runtime_prefix;
switch (platform_type) {
case PLATFORM_TYPE_IOS:
runtime_prefix = @"com.apple.CoreSimulator.SimRuntime.iOS";
break;
case PLATFORM_TYPE_TVOS:
runtime_prefix = @"com.apple.CoreSimulator.SimRuntime.tvOS";
break;
}
NSMutableArray* runtimes = [simctl_list[@"runtimes"] mutableCopy];
for (NSDictionary* runtime in simctl_list[@"runtimes"]) {
BOOL available =
[runtime[@"availability"] isEqualToString:@"(available)"] ||
runtime[@"isAvailable"];
if (![runtime[@"identifier"] hasPrefix:runtime_prefix] || !available) {
[runtimes removeObject:runtime];
}
}
return runtimes;
}
// Return array of device dictionaries.
NSArray* Devices(NSDictionary* simctl_list, PlatformType platform_type) {
NSSet* product_families;
switch (platform_type) {
case PLATFORM_TYPE_IOS:
product_families = [NSSet setWithArray:@[ @"iPad", @"iPhone" ]];
break;
case PLATFORM_TYPE_TVOS:
product_families = [NSSet setWithObject:@"Apple TV"];
break;
}
NSMutableArray* devicetypes = [simctl_list[@"devicetypes"] mutableCopy];
for (NSDictionary* devicetype in simctl_list[@"devicetypes"]) {
if (![product_families containsObject:devicetype[@"productFamily"]]) {
[devicetypes removeObject:devicetype];
}
}
return devicetypes;
}
// Get list of devices, runtimes, etc from simctl.
NSDictionary* GetSimulatorList(bool verbose) {
XCRunTask* task =
[[XCRunTask alloc] initWithArguments:@[ @"simctl", @"list", @"-j" ]];
NSPipe* out = [NSPipe pipe];
task.standardOutput = out;
// In the rest of the this file we read from the pipe after -waitUntilExit
// (We normally wrap -launch and -waitUntilExit in one -run method). However,
// on some swarming slaves this led to a hang on simctl's pipe. Since the
// output of simctl is so instant, reading it before exit seems to work, and
// seems to avoid the hang.
[task launch:verbose];
NSData* data = [out.fileHandleForReading readDataToEndOfFile];
[task waitUntilExit];
NSError* error = nil;
return [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
}
// List supported runtimes and devices.
void PrintSupportedDevices(NSDictionary* simctl_list,
PlatformType platform_type) {
printf("\ndevices:\n");
for (NSDictionary* type in Devices(simctl_list, platform_type)) {
printf("%s\n", [type[@"name"] UTF8String]);
}
printf("\nruntimes:\n");
for (NSDictionary* runtime in Runtimes(simctl_list, platform_type)) {
printf("%s\n", [runtime[@"version"] UTF8String]);
}
}
// Expand path to absolute path.
NSString* ResolvePath(NSString* path) {
path = path.stringByExpandingTildeInPath;
path = path.stringByStandardizingPath;
const char* cpath = path.UTF8String;
char* resolved_name = nullptr;
char* abs_path = realpath(cpath, resolved_name);
if (abs_path == nullptr) {
return nil;
}
return @(abs_path);
}
// Search |simctl_list| for a udid matching |device_name| and |sdk_version|.
NSString* GetDeviceBySDKAndName(NSDictionary* simctl_list,
NSString* device_name,
NSString* sdk_version,
PlatformType platform_type) {
BOOL found_runtime_with_version = NO;
// Get runtime identifier based on version property to handle
// cases when version and identifier are not the same,
// e.g. below identifer is *13-2 but version is 13.2.2
// {
// "version" : "13.2.2",
// "bundlePath" : "path"
// "identifier" : "com.apple.CoreSimulator.SimRuntime.iOS-13-2",
// "buildversion" : "17K90"
// }
for (NSDictionary* runtime in Runtimes(simctl_list, platform_type)) {
if ([runtime[@"version"] isEqualToString:sdk_version]) {
found_runtime_with_version = YES;
NSString* sdk = runtime[@"identifier"];
NSArray* devices = [simctl_list[@"devices"] objectForKey:sdk];
for (NSDictionary* device in devices) {
if ([device[@"name"] isEqualToString:device_name]) {
return device[@"udid"];
}
}
}
}
if (!found_runtime_with_version) {
printf("\nDid not find runtime with specified version.\n");
PrintSupportedDevices(simctl_list, platform_type);
exit(kExitInvalidArguments);
}
// In this case we did find a runtime matching `sdk_version`, but no device
// matching it.
return nil;
}
// Create and return a device udid of |device| and |sdk_version|.
NSString* CreateDeviceBySDKAndName(NSString* device,
NSString* sdk_version,
PlatformType platform_type,
bool verbose) {
NSString* sdk;
switch (platform_type) {
case PLATFORM_TYPE_IOS:
sdk = [@"iOS" stringByAppendingString:sdk_version];
break;
case PLATFORM_TYPE_TVOS:
sdk = [@"tvOS" stringByAppendingString:sdk_version];
break;
}
XCRunTask* create = [[XCRunTask alloc]
initWithArguments:@[ @"simctl", @"create", device, device, sdk ]];
[create run:verbose];
NSDictionary* simctl_list = GetSimulatorList(verbose);
return GetDeviceBySDKAndName(simctl_list, device, sdk_version, platform_type);
}
bool FindDeviceByUDID(NSDictionary* simctl_list, NSString* udid) {
NSDictionary* devices_table = simctl_list[@"devices"];
for (id runtimes in devices_table) {
NSArray* devices = devices_table[runtimes];
for (NSDictionary* device in devices) {
if ([device[@"udid"] isEqualToString:udid]) {
return true;
}
}
}
return false;
}
// Prints the HOME environment variable for a device. Used by the bots to
// package up all the test data.
void PrintDeviceHome(NSString* udid, bool verbose) {
XCRunTask* task = [[XCRunTask alloc]
initWithArguments:@[ @"simctl", @"getenv", udid, @"HOME" ]];
[task run:verbose];
}
// Erase a device, used by the bots before a clean test run.
void WipeDevice(NSString* udid, bool verbose) {
XCRunTask* shutdown =
[[XCRunTask alloc] initWithArguments:@[ @"simctl", @"shutdown", udid ]];
shutdown.standardOutput = nil;
shutdown.standardError = nil;
[shutdown run:verbose];
XCRunTask* erase =
[[XCRunTask alloc] initWithArguments:@[ @"simctl", @"erase", udid ]];
[erase run:verbose];
}
void KillSimulator(bool verbose) {
XCRunTask* task =
[[XCRunTask alloc] initWithArguments:@[ @"killall", @"Simulator" ]];
task.standardOutput = nil;
task.standardError = nil;
[task run:verbose];
}
NSString* GetBundleIdentifierFromPath(NSString* app_path) {
NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* info_plist_path =
[app_path stringByAppendingPathComponent:@"Info.plist"];
if (![file_manager fileExistsAtPath:info_plist_path]) {
return nil;
}
NSDictionary* info_dictionary =
[NSDictionary dictionaryWithContentsOfFile:info_plist_path];
NSString* bundle_identifier = info_dictionary[@"CFBundleIdentifier"];
return bundle_identifier;
}
int RunSimCtl(NSArray* arguments, bool verbose) {
XCRunTask* task = [[XCRunTask alloc]
initWithArguments:[@[ @"simctl" ]
arrayByAddingObjectsFromArray:arguments]];
[task run:verbose];
int ret = [task terminationStatus];
if (ret) {
NSLog(@"Warning: the following command failed: xcrun simctl %@",
[arguments componentsJoinedByString:@" "]);
}
return ret;
}
void PrepareWebTests(NSString* udid, NSString* app_path, bool verbose) {
NSString* bundle_identifier = GetBundleIdentifierFromPath(app_path);
RunSimCtl(@[ @"uninstall", udid, bundle_identifier ], verbose);
RunSimCtl(@[ @"install", udid, app_path ], verbose);
}
int RunWebTest(NSString* app_path,
NSString* udid,
NSMutableArray* cmd_args,
bool verbose) {
NSMutableArray* arguments = [NSMutableArray array];
[arguments addObject:@"simctl"];
[arguments addObject:@"launch"];
[arguments addObject:kSimCtlLaunchConsoleArg];
[arguments addObject:@"--terminate-running-process"];
[arguments addObject:udid];
[arguments addObject:GetBundleIdentifierFromPath(app_path)];
if (cmd_args.count == 1) {
for (NSString* arg in [cmd_args[0] componentsSeparatedByString:@" "]) {
[arguments addObject:arg];
}
}
[arguments addObject:@"-"];
XCRunTask* task = [[XCRunTask alloc] initWithArguments:arguments];
// The following stderr message causes a lot of test faiures on the web
// tests. Strip the message here.
NSArray* ignore_strings = @[ @"Class SwapLayerEAGL" ];
NSPipe* stderr_pipe = [NSPipe pipe];
stderr_pipe.fileHandleForReading.readabilityHandler =
^(NSFileHandle* handle) {
NSString* log = [[NSString alloc] initWithData:handle.availableData
encoding:NSUTF8StringEncoding];
for (NSString* ignore_string in ignore_strings) {
if ([log rangeOfString:ignore_string].location != NSNotFound) {
return;
}
}
fprintf(stderr, "%s", log.UTF8String);
};
task.standardError = stderr_pipe;
[task run:verbose];
return [task terminationStatus];
}
bool isSimDeviceBooted(NSDictionary* simctl_list, NSString* udid) {
for (NSString* sdk in simctl_list[@"devices"]) {
for (NSDictionary* device in simctl_list[@"devices"][sdk]) {
if ([device[@"udid"] isEqualToString:udid]) {
if ([device[@"state"] isEqualToString:@"Booted"]) {
return true;
}
}
}
}
return false;
}
int SimpleRunApplication(NSString* app_path,
NSString* udid,
NSMutableArray* cmd_args,
bool verbose) {
NSString* bundle_id = GetBundleIdentifierFromPath(app_path);
RunSimCtl(@[ @"uninstall", udid, bundle_id ], verbose);
RunSimCtl(@[ @"install", udid, app_path ], verbose);
NSArray* command = [@[
@"launch", kSimCtlLaunchConsoleArg, @"--terminate-running-process", udid,
bundle_id
] arrayByAddingObjectsFromArray:cmd_args];
return RunSimCtl(command, verbose);
}
int RunApplication(NSString* app_path,
NSString* xctest_path,
NSString* udid,
PlatformType platform_type,
NSMutableDictionary* app_env,
NSMutableArray* cmd_args,
NSMutableArray* tests_filter,
bool verbose) {
NSString* filename =
[NSUUID.UUID.UUIDString stringByAppendingString:@".xctestrun"];
NSString* tempFilePath =
[NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[NSFileManager.defaultManager createFileAtPath:tempFilePath
contents:nil
attributes:nil];
NSMutableDictionary* xctestrun = [NSMutableDictionary dictionary];
NSMutableDictionary* testTargetName = [NSMutableDictionary dictionary];
NSMutableDictionary* testingEnvironmentVariables =
[NSMutableDictionary dictionary];
testingEnvironmentVariables[@"IDEiPhoneInternalTestBundleName"] =
app_path.lastPathComponent;
NSString* out_dir_name;
NSString* platform_dir_name;
NSString* simulator_platform_name;
switch (platform_type) {
case PLATFORM_TYPE_IOS:
out_dir_name = @"Debug-iphonesimulator";
platform_dir_name = @"iPhoneSimulator.platform";
simulator_platform_name = @"iOS Simulator";
break;
case PLATFORM_TYPE_TVOS:
out_dir_name = @"Debug-appletvsimulator";
platform_dir_name = @"AppleTVSimulator.platform";
simulator_platform_name = @"tvOS Simulator";
break;
}
testingEnvironmentVariables[@"DYLD_FRAMEWORK_PATH"] = [NSString
stringWithFormat:
@"__TESTROOT__/%@:__PLATFORMS__/%@/Developer/Library/Frameworks",
out_dir_name, platform_dir_name];
testingEnvironmentVariables[@"DYLD_LIBRARY_PATH"] = [NSString
stringWithFormat:@"__TESTROOT__/%@:__PLATFORMS__/%@/Developer/Library",
out_dir_name, platform_dir_name];
if (xctest_path) {
testTargetName[@"TestBundlePath"] = xctest_path;
testingEnvironmentVariables[@"DYLD_INSERT_LIBRARIES"] =
[NSString stringWithFormat:@"__PLATFORMS__/%@/Developer/"
@"usr/lib/libXCTestBundleInject.dylib",
platform_dir_name];
testingEnvironmentVariables[@"XCInjectBundleInto"] =
[NSString stringWithFormat:@"__TESTHOST__/%@",
app_path.lastPathComponent
.stringByDeletingPathExtension];
} else {
testTargetName[@"TestBundlePath"] = app_path;
}
testTargetName[@"TestHostPath"] = app_path;
if (app_env.count) {
testTargetName[@"EnvironmentVariables"] = app_env;
}
if (cmd_args.count > 0) {
testTargetName[@"CommandLineArguments"] = cmd_args;
}
if (tests_filter.count > 0) {
testTargetName[@"OnlyTestIdentifiers"] = tests_filter;
}
testTargetName[@"TestingEnvironmentVariables"] = testingEnvironmentVariables;
xctestrun[@"TestTargetName"] = testTargetName;
NSData* data = [NSPropertyListSerialization
dataWithPropertyList:xctestrun
format:NSPropertyListXMLFormat_v1_0
options:0
error:nil];
[data writeToFile:tempFilePath atomically:YES];
XCRunTask* task = [[XCRunTask alloc] initWithArguments:@[
@"xcodebuild", @"-xctestrun", tempFilePath, @"-destination",
[NSString
stringWithFormat:@"platform=%@,id=%@", simulator_platform_name, udid],
@"test-without-building"
]];
if (!xctest_path) {
// The following stderr messages are meaningless on iossim when not running
// xctests and can be safely stripped.
NSArray* ignore_strings = @[
@"IDETestOperationsObserverErrorDomain", @"** TEST EXECUTE FAILED **"
];
NSPipe* stderr_pipe = [NSPipe pipe];
stderr_pipe.fileHandleForReading.readabilityHandler =
^(NSFileHandle* handle) {
NSString* log = [[NSString alloc] initWithData:handle.availableData
encoding:NSUTF8StringEncoding];
for (NSString* ignore_string in ignore_strings) {
if ([log rangeOfString:ignore_string].location != NSNotFound) {
return;
}
}
printf("%s", log.UTF8String);
};
task.standardError = stderr_pipe;
}
[task run:verbose];
return [task terminationStatus];
}
int main(int argc, char* const argv[]) {
NSString* app_path = nil;
NSString* xctest_path = nil;
NSString* udid = nil;
NSString* device_name = @"iPhone 6s";
bool wants_wipe = false;
bool wants_print_home = false;
bool wants_print_supported_devices = false;
bool run_web_test = false;
bool prepare_web_test = false;
NSString* sdk_version = nil;
NSMutableDictionary* app_env = [NSMutableDictionary dictionary];
NSMutableArray* cmd_args = [NSMutableArray array];
NSMutableArray* tests_filter = [NSMutableArray array];
bool verbose_commands = false;
SimulatorKill kill_simulator = KILL_BOTH;
bool wants_simple_iossim = false;
PlatformType platform_type = PLATFORM_TYPE_IOS;
int c;
while ((c = getopt(argc, argv, "hs:d:u:t:e:c:pwlvk:ix:")) != -1) {
switch (c) {
case 's':
sdk_version = @(optarg);
break;
case 'd':
device_name = @(optarg);
break;
case 'u':
udid = @(optarg);
break;
case 'w':
wants_wipe = true;
break;
case 'c': {
NSString* cmd_arg = @(optarg);
[cmd_args addObject:cmd_arg];
} break;
case 't': {
NSString* test = @(optarg);
[tests_filter addObject:test];
} break;
case 'e': {
NSString* envLine = @(optarg);
NSRange range = [envLine rangeOfString:@"="];
if (range.location == NSNotFound) {
LogError(@"Invalid key=value argument for -e.");
PrintUsage();
exit(kExitInvalidArguments);
}
NSString* key = [envLine substringToIndex:range.location];
NSString* value = [envLine substringFromIndex:(range.location + 1)];
[app_env setObject:value forKey:key];
} break;
case 'p':
wants_print_home = true;
break;
case 'l':
wants_print_supported_devices = true;
break;
case 'v':
verbose_commands = true;
break;
case 'k': {
NSString* cmd_arg = @(optarg);
if ([cmd_arg isEqualToString:@"before"]) {
kill_simulator = KILL_BEFORE;
} else if ([cmd_arg isEqualToString:@"after"]) {
kill_simulator = KILL_AFTER;
} else if ([cmd_arg isEqualToString:@"both"]) {
kill_simulator = KILL_BOTH;
} else if ([cmd_arg isEqualToString:@"never"]) {
kill_simulator = KILL_NEVER;
} else {
PrintUsage();
exit(kExitInvalidArguments);
}
} break;
case 'i':
wants_simple_iossim = true;
break;
case 'x': {
NSString* cmd_arg = @(optarg);
if ([cmd_arg isEqualToString:@"ios"]) {
platform_type = PLATFORM_TYPE_IOS;
} else if ([cmd_arg isEqualToString:@"tvos"]) {
platform_type = PLATFORM_TYPE_TVOS;
} else {
LogError(@"Invalid value for -x.");
PrintUsage();
exit(kExitInvalidArguments);
}
} break;
case 'h':
PrintUsage();
exit(kExitSuccess);
default:
PrintUsage();
exit(kExitInvalidArguments);
}
}
if (wants_simple_iossim && [tests_filter count]) {
LogError(@"Cannot specify tests with -t when using -i.");
exit(kExitInvalidArguments);
}
NSDictionary* simctl_list = GetSimulatorList(verbose_commands);
if (wants_print_supported_devices) {
PrintSupportedDevices(simctl_list, platform_type);
exit(kExitSuccess);
}
if (!sdk_version) {
NSString* highest_sdk = @"0";
for (NSDictionary* runtime in Runtimes(simctl_list, platform_type)) {
NSString* runtime_version = runtime[@"version"];
if (!highest_sdk ||
[runtime_version compare:highest_sdk
options:NSNumericSearch] == NSOrderedDescending) {
highest_sdk = runtime_version;
}
}
sdk_version = highest_sdk;
}
NSRange range;
for (NSString* cmd_arg in cmd_args) {
range = [cmd_arg rangeOfString:@"--run-web-tests"];
if (range.location != NSNotFound) {
run_web_test = true;
break;
}
}
for (NSString* cmd_arg in cmd_args) {
range = [cmd_arg rangeOfString:@"--prepare-web-tests"];
if (range.location != NSNotFound) {
prepare_web_test = true;
break;
}
}
if (udid == nil) {
udid = GetDeviceBySDKAndName(simctl_list, device_name, sdk_version,
platform_type);
if (udid == nil) {
udid = CreateDeviceBySDKAndName(device_name, sdk_version, platform_type,
verbose_commands);
if (udid == nil) {
LogError(@"Unable to find a device %@ with SDK %@.", device_name,
sdk_version);
PrintSupportedDevices(simctl_list, platform_type);
exit(kExitInvalidArguments);
}
}
} else {
if (!FindDeviceByUDID(simctl_list, udid)) {
LogError(
@"Unable to find a device with udid %@. Use 'xcrun simctl list' to "
@"see valid device udids.",
udid);
exit(kExitInvalidArguments);
}
}
if (wants_print_home) {
PrintDeviceHome(udid, verbose_commands);
exit(kExitSuccess);
}
if (kill_simulator & KILL_BEFORE) {
KillSimulator(verbose_commands);
}
if (wants_wipe) {
WipeDevice(udid, verbose_commands);
printf("Device wiped.\n");
exit(kExitSuccess);
}
// There should be at least one arg left, specifying the app path. Any
// additional args are passed as arguments to the app.
if (optind < argc) {
NSString* unresolved_app_path = [NSFileManager.defaultManager
stringWithFileSystemRepresentation:argv[optind]
length:strlen(argv[optind])];
app_path = ResolvePath(unresolved_app_path);
if (!app_path) {
LogError(@"Unable to resolve app_path %@", unresolved_app_path);
exit(kExitInvalidArguments);
}
if (++optind < argc) {
if (wants_simple_iossim) {
fprintf(stderr, "Warning: xctest_path ignored when using -i");
} else {
NSString* unresolved_xctest_path = [NSFileManager.defaultManager
stringWithFileSystemRepresentation:argv[optind]
length:strlen(argv[optind])];
xctest_path = ResolvePath(unresolved_xctest_path);
if (!xctest_path) {
LogError(@"Unable to resolve xctest_path %@", unresolved_xctest_path);
exit(kExitInvalidArguments);
}
}
}
} else {
LogError(@"Unable to parse command line arguments.");
PrintUsage();
exit(kExitInvalidArguments);
}
if ((prepare_web_test || run_web_test || wants_simple_iossim) &&
!isSimDeviceBooted(simctl_list, udid)) {
RunSimCtl(@[ @"boot", udid ], verbose_commands);
}
int return_code = -1;
if (prepare_web_test) {
PrepareWebTests(udid, app_path, verbose_commands);
return_code = kExitSuccess;
} else if (run_web_test) {
return_code = RunWebTest(app_path, udid, cmd_args, verbose_commands);
} else if (wants_simple_iossim) {
return_code =
SimpleRunApplication(app_path, udid, cmd_args, verbose_commands);
} else {
return_code =
RunApplication(app_path, xctest_path, udid, platform_type, app_env,
cmd_args, tests_filter, verbose_commands);
}
if (kill_simulator & KILL_AFTER) {
KillSimulator(verbose_commands);
}
return return_code;
}
|