-
Notifications
You must be signed in to change notification settings - Fork 432
/
FBXCTestConfiguration.m
285 lines (227 loc) · 11.7 KB
/
FBXCTestConfiguration.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
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "FBXCTestConfiguration.h"
#import <FBControlCore/FBControlCore.h>
#import "FBXCTestConfiguration.h"
#import "FBXCTestProcess.h"
#import "FBXCTestProcessExecutor.h"
#import "XCTestBootstrapError.h"
FBXCTestType const FBXCTestTypeApplicationTest = FBXCTestTypeApplicationTestValue;
FBXCTestType const FBXCTestTypeLogicTest = @"logic-test";
FBXCTestType const FBXCTestTypeListTest = @"list-test";
FBXCTestType const FBXCTestTypeUITest = @"ui-test";
@implementation FBXCTestConfiguration
#pragma mark Initializers
- (instancetype)initWithShims:(FBXCTestShimConfiguration *)shims environment:(NSDictionary<NSString *, NSString *> *)environment workingDirectory:(NSString *)workingDirectory testBundlePath:(NSString *)testBundlePath waitForDebugger:(BOOL)waitForDebugger timeout:(NSTimeInterval)timeout
{
self = [super init];
if (!self) {
return nil;
}
_shims = shims;
_processUnderTestEnvironment = environment ?: @{};
_workingDirectory = workingDirectory;
_testBundlePath = testBundlePath;
_waitForDebugger = waitForDebugger;
NSString *timeoutFromEnv = NSProcessInfo.processInfo.environment[@"FB_TEST_TIMEOUT"];
if (timeoutFromEnv) {
_testTimeout = timeoutFromEnv.intValue;
} else {
_testTimeout = timeout > 0 ? timeout : [self defaultTimeout];
}
return self;
}
#pragma mark Public
- (NSTimeInterval)defaultTimeout
{
// TSAN is known to slow down all tests from running, bump the default test timeout to 1800s (instead of 500s).
#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
return 1800.0;
#endif
return 500;
}
- (NSString *)testType
{
NSAssert(NO, @"-[%@ %@] is abstract and should be overridden", NSStringFromClass(self.class), NSStringFromSelector(_cmd));
return nil;
}
- (NSDictionary<NSString *, NSString *> *)buildEnvironmentWithEntries:(NSDictionary<NSString *, NSString *> *)entries
{
NSMutableDictionary<NSString *, NSString *> *parentEnvironment = NSProcessInfo.processInfo.environment.mutableCopy;
[parentEnvironment removeObjectsForKeys:@[
@"XCTestConfigurationFilePath",
]];
NSMutableDictionary<NSString *, NSString *> *environmentOverrides = [NSMutableDictionary dictionary];
NSString *xctoolTestEnvPrefix = @"XCTOOL_TEST_ENV_";
for (NSString *key in parentEnvironment) {
if ([key hasPrefix:xctoolTestEnvPrefix]) {
NSString *childKey = [key substringFromIndex:xctoolTestEnvPrefix.length];
environmentOverrides[childKey] = parentEnvironment[key];
}
}
[environmentOverrides addEntriesFromDictionary:entries];
NSMutableDictionary<NSString *, NSString *> *environment = parentEnvironment.mutableCopy;
for (NSString *key in environmentOverrides) {
NSString *childKey = key;
environment[childKey] = environmentOverrides[key];
}
return environment.copy;
}
#pragma mark NSObject
- (NSString *)description
{
return [FBCollectionInformation oneLineDescriptionFromDictionary:self.jsonSerializableRepresentation];
}
- (BOOL)isEqual:(FBXCTestConfiguration *)object
{
// Class must match exactly in the class-cluster
if (![object isMemberOfClass:self.class]) {
return NO;
}
return (self.shims == object.shims || [self.shims isEqual:object.shims])
&& (self.processUnderTestEnvironment == object.processUnderTestEnvironment || [self.processUnderTestEnvironment isEqualToDictionary:object.processUnderTestEnvironment])
&& (self.workingDirectory == object.workingDirectory || [self.workingDirectory isEqualToString:object.workingDirectory])
&& (self.testBundlePath == object.testBundlePath || [self.testBundlePath isEqualToString:object.testBundlePath])
&& (self.testType == object.testType || [self.testType isEqualToString:object.testType])
&& (self.waitForDebugger == object.waitForDebugger)
&& (self.testTimeout == object.testTimeout);
}
- (NSUInteger)hash
{
return self.shims.hash ^ self.processUnderTestEnvironment.hash ^ self.workingDirectory.hash ^ self.testBundlePath.hash ^ self.testType.hash ^ ((NSUInteger) self.waitForDebugger) ^ ((NSUInteger) self.testTimeout);
}
#pragma mark JSON
static NSString *const KeyEnvironment = @"environment";
static NSString *const KeyListTestsOnly = @"list_only";
static NSString *const KeyOSLogPath = @"os_log_path";
static NSString *const KeyRunnerAppPath = @"test_host_path";
static NSString *const KeyRunnerTargetPath = @"test_target_path";
static NSString *const KeyShims = @"shims";
static NSString *const KeyTestArtifactsFilenameGlobs = @"test_artifacts_filename_globs";
static NSString *const KeyTestBundlePath = @"test_bundle_path";
static NSString *const KeyTestFilter = @"test_filter";
static NSString *const KeyTestMirror = @"test_mirror";
static NSString *const KeyTestTimeout = @"test_timeout";
static NSString *const KeyTestType = @"test_type";
static NSString *const KeyVideoRecordingPath = @"video_recording_path";
static NSString *const KeyWaitForDebugger = @"wait_for_debugger";
static NSString *const KeyWorkingDirectory = @"working_directory";
- (id)jsonSerializableRepresentation
{
return @{
KeyEnvironment: self.processUnderTestEnvironment,
KeyWorkingDirectory: self.workingDirectory,
KeyTestBundlePath: self.testBundlePath,
KeyTestType: self.testType,
KeyListTestsOnly: @NO,
KeyWaitForDebugger: @(self.waitForDebugger),
KeyTestTimeout: @(self.testTimeout),
};
}
#pragma mark NSCopying
- (instancetype)copyWithZone:(NSZone *)zone
{
return self;
}
@end
@implementation FBListTestConfiguration
#pragma mark Initializers
+ (instancetype)configurationWithShims:(FBXCTestShimConfiguration *)shims environment:(NSDictionary<NSString *, NSString *> *)environment workingDirectory:(NSString *)workingDirectory testBundlePath:(NSString *)testBundlePath runnerAppPath:(nullable NSString *)runnerAppPath waitForDebugger:(BOOL)waitForDebugger timeout:(NSTimeInterval)timeout
{
return [[FBListTestConfiguration alloc] initWithShims:shims environment:environment workingDirectory:workingDirectory testBundlePath:testBundlePath runnerAppPath:runnerAppPath waitForDebugger:waitForDebugger timeout:timeout];
}
- (instancetype)initWithShims:(FBXCTestShimConfiguration *)shims environment:(NSDictionary<NSString *, NSString *> *)environment workingDirectory:(NSString *)workingDirectory testBundlePath:(NSString *)testBundlePath runnerAppPath:(nullable NSString *)runnerAppPath waitForDebugger:(BOOL)waitForDebugger timeout:(NSTimeInterval)timeout
{
self = [super initWithShims:shims environment:environment workingDirectory:workingDirectory testBundlePath:testBundlePath waitForDebugger:waitForDebugger timeout:timeout];
if (!self) {
return nil;
}
_runnerAppPath = runnerAppPath;
return self;
}
#pragma mark Public
- (NSString *)testType
{
return FBXCTestTypeListTest;
}
#pragma mark JSON
- (id)jsonSerializableRepresentation
{
NSMutableDictionary<NSString *, id> *json = [NSMutableDictionary dictionaryWithDictionary:[super jsonSerializableRepresentation]];
json[KeyListTestsOnly] = @YES;
json[KeyRunnerAppPath] = _runnerAppPath ?: NSNull.null;
return [json copy];
}
@end
@implementation FBTestManagerTestConfiguration
#pragma mark Initializers
+ (instancetype)configurationWithShims:(FBXCTestShimConfiguration *)shims environment:(NSDictionary<NSString *, NSString *> *)environment workingDirectory:(NSString *)workingDirectory testBundlePath:(NSString *)testBundlePath waitForDebugger:(BOOL)waitForDebugger timeout:(NSTimeInterval)timeout runnerAppPath:(NSString *)runnerAppPath testTargetAppPath:(NSString *)testTargetAppPath testFilter:(NSString *)testFilter videoRecordingPath:(NSString *)videoRecordingPath testArtifactsFilenameGlobs:(nullable NSArray<NSString *> *)testArtifactsFilenameGlobs osLogPath:(nullable NSString *)osLogPath
{
return [[FBTestManagerTestConfiguration alloc] initWithShims:shims environment:environment workingDirectory:workingDirectory testBundlePath:testBundlePath waitForDebugger:waitForDebugger timeout:timeout runnerAppPath:runnerAppPath testTargetAppPath:testTargetAppPath testFilter:testFilter videoRecordingPath:videoRecordingPath testArtifactsFilenameGlobs:testArtifactsFilenameGlobs osLogPath:osLogPath];
}
- (instancetype)initWithShims:(FBXCTestShimConfiguration *)shims environment:(NSDictionary<NSString *, NSString *> *)environment workingDirectory:(NSString *)workingDirectory testBundlePath:(NSString *)testBundlePath waitForDebugger:(BOOL)waitForDebugger timeout:(NSTimeInterval)timeout runnerAppPath:(NSString *)runnerAppPath testTargetAppPath:(NSString *)testTargetAppPath testFilter:(NSString *)testFilter videoRecordingPath:(NSString *)videoRecordingPath testArtifactsFilenameGlobs:(NSArray<NSString *> *)testArtifactsFilenameGlobs osLogPath:(nullable NSString *)osLogPath
{
self = [super initWithShims:shims environment:environment workingDirectory:workingDirectory testBundlePath:testBundlePath waitForDebugger:waitForDebugger timeout:timeout];
if (!self) {
return nil;
}
_runnerAppPath = runnerAppPath;
_testTargetAppPath = testTargetAppPath;
_testFilter = testFilter;
_videoRecordingPath = videoRecordingPath;
_testArtifactsFilenameGlobs = testArtifactsFilenameGlobs;
_osLogPath = osLogPath;
return self;
}
#pragma mark Public
- (NSString *)testType
{
return _testTargetAppPath != nil ? FBXCTestTypeUITest : FBXCTestTypeApplicationTest;
}
#pragma mark JSON
- (id)jsonSerializableRepresentation
{
NSMutableDictionary<NSString *, id> *json = [NSMutableDictionary dictionaryWithDictionary:[super jsonSerializableRepresentation]];
json[KeyRunnerAppPath] = self.runnerAppPath;
json[KeyRunnerTargetPath] = self.testTargetAppPath;
json[KeyTestFilter] = self.testFilter ?: NSNull.null;
json[KeyVideoRecordingPath] = self.videoRecordingPath ?: NSNull.null;
json[KeyTestArtifactsFilenameGlobs] = self.testArtifactsFilenameGlobs ?: NSNull.null;
json[KeyOSLogPath] = self.osLogPath ?: NSNull.null;
return [json copy];
}
@end
@implementation FBLogicTestConfiguration
#pragma mark Initializers
+ (instancetype)configurationWithShims:(FBXCTestShimConfiguration *)shims environment:(NSDictionary<NSString *, NSString *> *)environment workingDirectory:(NSString *)workingDirectory logDirectoryPath:(nullable NSString *)logDirectoryPath testBundlePath:(NSString *)testBundlePath waitForDebugger:(BOOL)waitForDebugger timeout:(NSTimeInterval)timeout testFilter:(NSString *)testFilter mirroring:(FBLogicTestMirrorLogs)mirroring
{
return [[FBLogicTestConfiguration alloc] initWithShims:shims environment:environment workingDirectory:workingDirectory logDirectoryPath:logDirectoryPath testBundlePath:testBundlePath waitForDebugger:waitForDebugger timeout:timeout testFilter:testFilter mirroring:mirroring];
}
- (instancetype)initWithShims:(FBXCTestShimConfiguration *)shims environment:(NSDictionary<NSString *, NSString *> *)environment workingDirectory:(NSString *)workingDirectory logDirectoryPath:(NSString *)logDirectoryPath testBundlePath:(NSString *)testBundlePath waitForDebugger:(BOOL)waitForDebugger timeout:(NSTimeInterval)timeout testFilter:(NSString *)testFilter mirroring:(FBLogicTestMirrorLogs)mirroring
{
self = [super initWithShims:shims environment:environment workingDirectory:workingDirectory testBundlePath:testBundlePath waitForDebugger:waitForDebugger timeout:timeout];
if (!self) {
return nil;
}
_testFilter = testFilter;
_mirroring = mirroring;
_logDirectoryPath = logDirectoryPath;
return self;
}
#pragma mark Public
- (NSString *)testType
{
return FBXCTestTypeLogicTest;
}
#pragma mark JSON
- (id)jsonSerializableRepresentation
{
NSMutableDictionary<NSString *, id> *json = [NSMutableDictionary dictionaryWithDictionary:[super jsonSerializableRepresentation]];
json[KeyTestFilter] = self.testFilter ?: NSNull.null;
return [json copy];
}
@end