Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 93 lines (73 sloc) 2.65 kb
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
#import <UIKit/UIKit.h>
#import "CDROTestIPhoneRunner.h"
#import "CDROTestHelper.h"
#import "CedarApplicationDelegate.h"
#import "HeadlessSimulatorWorkaround.h"
#import "CDRFunctions.h"
#import <objc/runtime.h>

extern int *_NSGetArgc(void);
extern char ***_NSGetArgv(void);

bool CDRIsXCTest() {
    return objc_getClass("XCTestProbe");
}
bool CDRIsOCTest() {
    return objc_getClass("SenTestProbe");
}

@implementation NSBundle (MainBundleHijack)
static NSBundle *mainBundle__ = nil;

NSBundle *CDRMainBundle(id self, SEL _cmd) {
    return mainBundle__;
}

+ (void)load {
    setUpFakeWorkspaceIfRequired();

    NSString *extension = nil;;

    if (CDRIsXCTest()) {
        extension = @".xctest";
    } else if (CDRIsOCTest()) {
        extension = @".octest";
    }

    if (!extension)
        return;

    BOOL mainBundleIsApp = [[[NSBundle mainBundle] bundlePath] hasSuffix:@".app"];
    BOOL mainBundleIsTestBundle = [[[NSBundle mainBundle] bundlePath] hasSuffix:extension];

    if (!mainBundleIsApp && !mainBundleIsTestBundle) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        for (NSBundle *bundle in [NSBundle allBundles]) {
            if ([[bundle bundlePath] hasSuffix:extension]) {
                mainBundle__ = [bundle retain];
                Class nsBundleMetaClass = objc_getMetaClass("NSBundle");
                class_replaceMethod(nsBundleMetaClass, @selector(mainBundle), (IMP)CDRMainBundle, "v@:");
            }
        }
        [pool drain];
    }
}

@end

@implementation CDROTestIPhoneRunner

void CDRRunTests(id self, SEL _cmd, id ignored) {
    int exitStatus = 0;
    if (CDRIsXCTest()) {
        exitStatus = CDRRunXCUnitTests(self, _cmd, ignored);
    } else {
        exitStatus = CDRRunOCUnitTests(self, _cmd, ignored);
    }

    if ([UIApplication sharedApplication]) {
        BOOL isCedarApp = [[UIApplication sharedApplication] isKindOfClass:[CedarApplication class]];
        BOOL isCedarDelegate = [[[UIApplication sharedApplication] delegate] isKindOfClass:[CedarApplicationDelegate class]];

        if (!isCedarApp && !isCedarDelegate) {
            exitStatus |= runSpecsWithinUIApplication();
            exitWithStatusFromUIApplication(exitStatus);
        }
    } else {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        const char* argv[] = { "executable", "-RegisterForSystemEvents" };
        exitStatus |= UIApplicationMain(2, (char **)argv, @"CedarApplication", nil);

        [pool release];
        exit(exitStatus);
    }
}

+ (void)load {
    CDRHijackOCUnitRun((IMP)CDRRunTests);
    CDRHijackXCUnitRun((IMP)CDRRunTests);
}

@end
Something went wrong with that request. Please try again.