-
Notifications
You must be signed in to change notification settings - Fork 231
/
SimulatorHelper.m
184 lines (161 loc) · 9.83 KB
/
SimulatorHelper.m
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
// Copyright 2016 LinkedIn Corporation
// Licensed under the BSD 2-Clause License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://opensource.org/licenses/BSD-2-Clause
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
#import "SimulatorHelper.h"
#import "BPConfiguration.h"
#import "BPUtils.h"
#import "BPXCTestFile.h"
#import "PrivateHeaders/XCTest/XCTestConfiguration.h"
#import "PrivateHeaders/XCTest/XCTTestIdentifier.h"
#import "PrivateHeaders/XCTest/XCTTestIdentifierSet.h"
@implementation SimulatorHelper
+ (BOOL)loadFrameworksWithXcodePath:(NSString *)xcodePath {
// Check the availablity of frameworks
NSString *sharedFrameworksPath = [xcodePath stringByDeletingLastPathComponent];
NSArray *ar = @[
[NSString stringWithFormat:@"%@/DVTFoundation.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/DVTAnalyticsClient.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/DVTAnalytics.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/DVTPortal.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/DVTSourceControl.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/SourceKit.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/DVTSourceControl.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/DVTAnalytics.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/IDEFoundation.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/DVTFoundation.framework", sharedFrameworksPath],
[NSString stringWithFormat:@"%@/SimulatorKit.framework", sharedFrameworksPath]
];
for (NSString *address in ar) {
NSBundle *bl = [NSBundle bundleWithPath:address];
NSError *error;
[bl loadAndReturnError:&error];
if (error) {
NSLog(@"Failed to load framework %@, with error %@",address, error);
return NO;
}
}
NSArray *requiredClasses = @[@"SimDevice",
@"SimDeviceFramebufferService",
@"DTXConnection",
@"DTXRemoteInvocationReceipt",
@"DVTDevice",
@"IDEFoundationTestInitializer",
@"XCTestConfiguration"];
for (NSString *rc in requiredClasses) {
if (NSClassFromString(rc)) {
[BPUtils printInfo:DEBUGINFO withString:@"%@ is loaded..", rc];
} else {
return NO;
}
}
return YES;
}
+ (NSDictionary *)appLaunchEnvironmentWithBundleID:(NSString *)hostBundleID
device:(SimDevice *)device
config:(BPConfiguration *)config {
NSString *hostAppExecPath = [SimulatorHelper executablePathforPath:config.appBundlePath];
NSString *hostAppPath = [hostAppExecPath stringByDeletingLastPathComponent];
NSString *testSimulatorFrameworkPath = [hostAppPath stringByDeletingLastPathComponent];
NSString *libXCTestBundleInjectPath = [[hostAppPath stringByAppendingPathComponent:@"Frameworks"] stringByAppendingPathComponent:@"libXCTestBundleInject.dylib"];
NSString *libXCTestBundleInjectValue = libXCTestBundleInjectPath;
if (![NSFileManager.defaultManager fileExistsAtPath:libXCTestBundleInjectPath]) {
[BPUtils printInfo:DEBUGINFO withString:@"Not injecting libXCTestBundleInject dylib because it was not found in the app host bundle at path: %@", libXCTestBundleInjectValue];
libXCTestBundleInjectValue = @"";
}
NSMutableDictionary<NSString *, NSString *> *environment = [[NSMutableDictionary alloc] init];
environment[@"DYLD_FALLBACK_FRAMEWORK_PATH"] = [NSString stringWithFormat:@"%@/Library/Frameworks:%@/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks", config.xcodePath, config.xcodePath];
environment[@"DYLD_FALLBACK_LIBRARY_PATH"] = [NSString stringWithFormat:@"%@/Platforms/iPhoneSimulator.platform/Developer/usr/lib", config.xcodePath];
environment[@"DYLD_INSERT_LIBRARIES"] = libXCTestBundleInjectValue;
environment[@"DYLD_LIBRARY_PATH"] = [NSString stringWithFormat:@"%@/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks", config.xcodePath];
environment[@"DYLD_ROOT_PATH"] = [NSString stringWithFormat:@"%@/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot", config.xcodePath];
environment[@"NSUnbufferedIO"] = @"1";
environment[@"OS_ACTIVITY_DT_MODE"] = @"1";
environment[@"XCODE_DBG_XPC_EXCLUSIONS"] = @"com.apple.dt.xctestSymbolicator";
environment[@"XPC_FLAGS"] = @"0x0";
environment[@"XCTestConfigurationFilePath"] = [SimulatorHelper testEnvironmentWithConfiguration:config];
environment[@"__XCODE_BUILT_PRODUCTS_DIR_PATHS"] = testSimulatorFrameworkPath;
environment[@"__XPC_DYLD_FRAMEWORK_PATH"] = testSimulatorFrameworkPath;
environment[@"__XPC_DYLD_LIBRARY_PATH"] = testSimulatorFrameworkPath;
if (config.outputDirectory) {
NSString *coveragePath = [config.outputDirectory stringByAppendingPathComponent:@"%p.profraw"];
environment[@"LLVM_PROFILE_FILE"] = coveragePath;
environment[@"__XPC_LLVM_PROFILE_FILE"] = coveragePath;
}
return environment;
}
+ (NSString *)testEnvironmentWithConfiguration:(BPConfiguration *)config {
XCTestConfiguration *xctConfig = [[XCTestConfiguration alloc] init];
NSString *testBundlePath = config.testBundlePath;
NSString *appName = [self appNameForPath:testBundlePath];
NSString *testHostPath;
xctConfig.productModuleName = appName;
xctConfig.testBundleURL = [NSURL fileURLWithPath:testBundlePath];
xctConfig.sessionIdentifier = config.sessionIdentifier;
xctConfig.treatMissingBaselinesAsFailures = NO;
xctConfig.targetApplicationPath = config.appBundlePath;
xctConfig.reportResultsToIDE = YES;
xctConfig.automationFrameworkPath = [NSString stringWithFormat:@"%@/Platforms/iPhoneSimulator.platform/Developer/Library/PrivateFrameworks/XCTAutomationSupport.framework", config.xcodePath];
testHostPath = config.appBundlePath;
NSString *bundleID = [self bundleIdForPath:config.appBundlePath];
xctConfig.testApplicationDependencies = config.dependencies.count > 0 ? config.dependencies : @{bundleID: config.appBundlePath};
if (config.testRunnerAppPath) {
xctConfig.targetApplicationBundleID = bundleID;
xctConfig.initializeForUITesting = YES;
xctConfig.disablePerformanceMetrics = NO;
xctConfig.reportActivities = YES;
xctConfig.testsMustRunOnMainThread = YES;
testHostPath = config.testRunnerAppPath;
}
if (config.testCasesToSkip) {
NSMutableArray <XCTTestIdentifier*> *xctTests = [[NSMutableArray alloc] init];
for (NSString *test in config.testCasesToSkip) {
[xctTests addObject:[[XCTTestIdentifier alloc] initWithStringRepresentation:test]];
}
[xctConfig setTestsToSkip:[[XCTTestIdentifierSet alloc] initWithArray:xctTests]];
}
if (config.testCasesToRun) {
NSMutableArray <XCTTestIdentifier *> *xctTests = [[NSMutableArray alloc] init];
for (NSString *test in config.testCasesToRun) {
[xctTests addObject:[[XCTTestIdentifier alloc] initWithStringRepresentation:test]];
}
[xctConfig setTestsToRun:[[XCTTestIdentifierSet alloc] initWithSet:xctTests]];
}
NSString *XCTestConfigurationFilename = [NSString stringWithFormat:@"%@/%@-%@",
NSTemporaryDirectory(),
appName,
[xctConfig.sessionIdentifier UUIDString]];
assert(XCTestConfigurationFilename != nil);
NSString *XCTestConfigurationFilePath = [XCTestConfigurationFilename stringByAppendingPathExtension:@"xctestconfiguration"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:xctConfig requiringSecureCoding:TRUE error:nil];
[data writeToFile:XCTestConfigurationFilePath atomically:TRUE];
return XCTestConfigurationFilePath;
}
+ (NSString *)bundleIdForPath:(NSString *)path {
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[path stringByAppendingPathComponent:@"Info.plist"]];
NSString *platform = [dic objectForKey:@"DTPlatformName"];
if (platform && ![platform isEqualToString:@"iphonesimulator"]) {
[BPUtils printInfo:ERROR withString:@"Wrong platform in %@. Expected 'iphonesimulator', found '%@'", path, platform];
return nil;
}
NSString *bundleId = [dic objectForKey:(NSString *)kCFBundleIdentifierKey];
if (!bundleId) {
[BPUtils printInfo:ERROR withString:@"Could not extract bundleID: %@", dic];
}
return bundleId;
}
+ (NSString *)executablePathforPath:(NSString *)path {
NSDictionary *appDic = [NSDictionary dictionaryWithContentsOfFile:[path stringByAppendingPathComponent:@"Info.plist"]];
NSString *appExecutable = [appDic objectForKey:(NSString *)kCFBundleExecutableKey];
return [path stringByAppendingPathComponent:appExecutable];
}
+ (NSString *)appNameForPath:(NSString *)path {
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[path stringByAppendingPathComponent:@"Info.plist"]];
NSString *bundleId = [dic objectForKey:(NSString *)kCFBundleNameKey];
return bundleId;
}
@end