-
Notifications
You must be signed in to change notification settings - Fork 432
/
FBJSONTestReporter.m
344 lines (297 loc) · 11 KB
/
FBJSONTestReporter.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
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
/*
* 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 "FBJSONTestReporter.h"
#import "XCTestBootstrapError.h"
static inline NSString *FBFullyFormattedXCTestName(NSString *className, NSString *methodName);
@interface FBJSONTestReporter ()
@property (nonatomic, strong, readonly) id<FBDataConsumer> dataConsumer;
@property (nonatomic, strong, readonly) id<FBControlCoreLogger> logger;
@property (nonatomic, copy, readonly) NSString *testBundlePath;
@property (nonatomic, copy, readonly) NSString *testType;
@property (nonatomic, copy, readonly) NSMutableArray<NSDictionary<NSString *, id> *> *events;
@property (nonatomic, copy, readonly) NSMutableDictionary<NSString *, NSMutableArray<NSDictionary<NSString *, id> *> *> *xctestNameExceptionsMapping;
@property (nonatomic, copy, readonly) NSMutableArray<NSString *> *pendingTestOutput;
@property (nonatomic, copy, readwrite) NSString *currentTestName;
@property (nonatomic, copy, readwrite) NSError *crashError;
@property (nonatomic, assign, readwrite) BOOL started;
@property (nonatomic, assign, readwrite) BOOL finished;
@end
@implementation FBJSONTestReporter
- (instancetype)initWithTestBundlePath:(NSString *)testBundlePath testType:(NSString *)testType logger:(id<FBControlCoreLogger>)logger dataConsumer:(id<FBDataConsumer>)dataConsumer
{
self = [super init];
if (!self) {
return nil;
}
_dataConsumer = dataConsumer;
_logger = logger;
_testBundlePath = testBundlePath;
_testType = testType;
_xctestNameExceptionsMapping = [NSMutableDictionary dictionary];
_pendingTestOutput = [NSMutableArray array];
_currentTestName = nil;
_started = NO;
_finished = NO;
return self;
}
#pragma mark FBXCTestReporter
- (BOOL)printReportWithError:(NSError **)error
{
if (!_started) {
return [[FBXCTestError describe:[self noStartOfTestPlanErrorMessage]] failBool:error];
}
if (!_finished) {
NSError *crashError = nil;
NSString *errorMessage = @"No didFinishExecutingTestPlan event was received, the test bundle has likely crashed.";
if (crashError) {
errorMessage = crashError.localizedDescription;
}
if (_currentTestName) {
errorMessage = [errorMessage stringByAppendingString:@". Crash occurred while this test was running: "];
errorMessage = [errorMessage stringByAppendingString:_currentTestName];
}
[self printEvent:[FBJSONTestReporter createOCUnitEndEvent:self.testType testBundlePath:self.testBundlePath message:errorMessage success:NO]];
return [[FBXCTestError describe:errorMessage] failBool:error];
}
[self.dataConsumer consumeEndOfFile];
return YES;
}
- (void)processWaitingForDebuggerWithProcessIdentifier:(pid_t)pid
{
[self printEvent:[FBJSONTestReporter waitingForDebuggerEvent:pid]];
}
- (void)debuggerAttached
{
[self printEvent:[FBJSONTestReporter debuggerAttachedEvent]];
}
- (void)didBeginExecutingTestPlan
{
_started = YES;
[self printEvent:[FBJSONTestReporter createOCUnitBeginEvent:self.testType testBundlePath:self.testBundlePath]];
}
- (void)didFinishExecutingTestPlan
{
if (_started) {
[self printEvent:[FBJSONTestReporter createOCUnitEndEvent:self.testType testBundlePath:self.testBundlePath message:nil success:YES]];
} else {
[self printEvent:[FBJSONTestReporter createOCUnitBeginEvent:self.testType testBundlePath:self.testBundlePath]];
NSString *errorMessage = [self noStartOfTestPlanErrorMessage];
[self printEvent:[FBJSONTestReporter createOCUnitEndEvent:self.testType testBundlePath:self.testBundlePath message:errorMessage success:NO]];
}
_finished = YES;
}
- (void)testSuite:(NSString *)testSuite didStartAt:(NSString *)startTime
{
[self printEvent:[FBJSONTestReporter beginTestSuiteEvent:testSuite]];
}
- (void)testCaseDidStartForTestClass:(NSString *)testClass method:(NSString *)method
{
NSString *xctestName = FBFullyFormattedXCTestName(testClass, method);
_currentTestName = xctestName;
self.xctestNameExceptionsMapping[xctestName] = [NSMutableArray array];
[self printEvent:[FBJSONTestReporter beginTestCaseEvent:testClass testMethod:method]];
}
- (void)testCaseDidFailForTestClass:(NSString *)testClass method:(NSString *)method withMessage:(NSString *)message file:(NSString *)file line:(NSUInteger)line
{
NSString *xctestName = FBFullyFormattedXCTestName(testClass, method);
[self.xctestNameExceptionsMapping[xctestName] addObject:[FBJSONTestReporter exceptionEvent:message file:file line:line]];
}
- (void)testCaseDidFinishForTestClass:(NSString *)testClass method:(NSString *)method withStatus:(FBTestReportStatus)status duration:(NSTimeInterval)duration logs:(NSArray<NSString *> *)logs
{
_currentTestName = nil;
NSDictionary<NSString *, id> *event = [FBJSONTestReporter
testCaseDidFinishForTestClass:testClass
method:method
status:status
duration:duration
pendingTestOutput:self.pendingTestOutput
xctestNameExceptionsMapping:self.xctestNameExceptionsMapping];
[self printEvent:event];
[self.pendingTestOutput removeAllObjects];
}
- (void)finishedWithSummary:(FBTestManagerResultSummary *)summary
{
[self printEvent:[FBJSONTestReporter finishedEventFromSummary:summary]];
}
- (void)didRecordVideoAtPath:(nonnull NSString *)videoRecordingPath
{
[self printEvent:@{
@"event" : @"video-recording-finished",
@"videoRecordingPath" : videoRecordingPath,
}];
}
- (void)didSaveOSLogAtPath:(nonnull NSString *)osLogPath
{
[self printEvent:@{
@"event" : @"os-log-saved",
@"osLogPath" : osLogPath,
}];
}
- (void)didCopiedTestArtifact:(nonnull NSString *)testArtifactFilename toPath:(nonnull NSString *)path
{
[self printEvent:@{
@"event" : @"copy-test-artifact",
@"test_artifact_file_name" : testArtifactFilename,
@"path" : path,
}];
}
- (void)testHadOutput:(NSString *)output
{
[self.pendingTestOutput addObject:output];
[self printEvent:[FBJSONTestReporter testOutputEvent:output]];
}
- (void)handleExternalEvent:(NSString *)line
{
if (line.length == 0) {
return;
}
NSError *error = nil;
NSDictionary *event = [NSJSONSerialization JSONObjectWithData:[line dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];
if (event == nil) {
[self.logger logFormat:@"Received unexpected output from otest-shim:\n%@", line];
}
if ([event[@"event"] isEqualToString:@"end-test"]) {
NSMutableDictionary *mutableEvent = event.mutableCopy;
mutableEvent[@"output"] = [self.pendingTestOutput componentsJoinedByString:@""];
event = mutableEvent.copy;
[self.pendingTestOutput removeAllObjects];
}
[self.events addObject:event];
}
- (void)appUnderTestExited
{
}
- (void)didCrashDuringTest:(NSError *)error
{
self.crashError = error;
}
#pragma mark Private
- (void)printEvent:(NSDictionary *)event
{
NSMutableDictionary *timestamped = event.mutableCopy;
if (!timestamped[@"timestamp"]) {
timestamped[@"timestamp"] = @(NSDate.date.timeIntervalSince1970);
}
NSData *data = [NSJSONSerialization dataWithJSONObject:timestamped options:0 error:nil];
[self.dataConsumer consumeData:data];
[self.dataConsumer consumeData:[NSData dataWithBytes:"\n" length:1]];
}
- (NSString *)noStartOfTestPlanErrorMessage
{
NSString *errorMessage = @"No didBeginExecutingTestPlan event was received.";
if (_currentTestName) {
errorMessage = [errorMessage stringByAppendingString:@". However a test was running: "];
errorMessage = [errorMessage stringByAppendingString:_currentTestName];
}
return errorMessage;
}
+ (NSDictionary<NSString *, id> *)exceptionEvent:(NSString *)reason file:(NSString *)file line:(NSUInteger)line
{
return @{
@"lineNumber" : @(line),
@"filePathInProject" : file,
@"reason" : reason,
};
}
+ (NSDictionary<NSString *, id> *)beginTestCaseEvent:(NSString *)testClass testMethod:(NSString *)method
{
return @{
@"event" : @"begin-test",
@"className" : testClass,
@"methodName" : method,
@"test" : FBFullyFormattedXCTestName(testClass, method),
};
}
+ (NSDictionary<NSString *, id> *)beginTestSuiteEvent:(NSString *)testSuite
{
return @{
@"event" : @"begin-test-suite",
@"suite" : testSuite,
};
}
+ (NSDictionary<NSString *, id> *)testOutputEvent:(NSString *)output
{
return @{
@"event": @"test-output",
@"output": output,
};
}
+ (NSDictionary<NSString *, id> *)waitingForDebuggerEvent:(pid_t)pid
{
return @{
@"event": @"begin-status",
@"pid": @(pid),
@"level": @"Info",
@"message": [NSString stringWithFormat:@"Tests waiting for debugger. To debug run: lldb -p %@", @(pid)],
};
}
+ (NSDictionary<NSString *, id> *)debuggerAttachedEvent
{
return @{
@"event": @"end-status",
@"level": @"Info",
@"message": @"Debugger attached",
};
}
+ (NSDictionary<NSString *, id> *)createOCUnitBeginEvent:(NSString *)testType testBundlePath:(NSString *)testBundlePath
{
return @{
@"event" : @"begin-ocunit",
@"testType" : testType,
@"bundleName" : [testBundlePath lastPathComponent],
@"targetName" : testBundlePath,
};
}
+ (NSDictionary<NSString *, id> *)createOCUnitEndEvent:(NSString *)testType testBundlePath:(NSString *)testBundlePath message:(NSString *)message success:(BOOL)success
{
NSMutableDictionary<NSString *, id> *event = [NSMutableDictionary dictionaryWithDictionary:@{
@"event" : @"end-ocunit",
@"testType" : testType,
@"bundleName" : [testBundlePath lastPathComponent],
@"targetName" : testBundlePath,
@"succeeded" : success ? @YES : @NO,
}];
if (message) {
event[@"message"] = message;
}
return [event copy];
}
+ (NSDictionary<NSString *, id> *)finishedEventFromSummary:(FBTestManagerResultSummary *)summary
{
return @{
@"event" : @"end-test-suite",
@"suite" : summary.testSuite,
@"testCaseCount" : @(summary.runCount),
@"totalFailureCount" : @(summary.failureCount),
@"totalDuration" : @(summary.totalDuration),
@"unexpectedExceptionCount" : @(summary.unexpected),
@"testDuration" : @(summary.testDuration)
};
}
+ (NSDictionary<NSString *, id> *)testCaseDidFinishForTestClass:(NSString *)testClass method:(NSString *)method status:(FBTestReportStatus)status duration:(NSTimeInterval)duration pendingTestOutput:(NSArray<NSString *> *)pendingTestOutput xctestNameExceptionsMapping:(NSDictionary<NSString *, NSArray<NSDictionary *> *> *)xctestNameExceptionsMapping
{
NSString *xctestName = FBFullyFormattedXCTestName(testClass, method);
return @{
@"event" : @"end-test",
@"result" : (status == FBTestReportStatusPassed ? @"success" : @"failure"),
@"output" : [pendingTestOutput componentsJoinedByString:@""],
@"test" : xctestName,
@"className" : testClass,
@"methodName" : method,
@"succeeded" : (status == FBTestReportStatusPassed ? @YES : @NO),
@"exceptions" : xctestNameExceptionsMapping[xctestName] ?: @[],
@"totalDuration" : @(duration),
};
}
- (void)setLogDirectoryPath:(NSString *)logDirectoryPath
{
}
@end
static inline NSString *FBFullyFormattedXCTestName(NSString *className, NSString *methodName)
{
return [NSString stringWithFormat:@"-[%@ %@]", className, methodName];
}