Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Stop using method swizzling for registering CurrentTestCaseTracker to XCTestObservationCenter #753

Merged
merged 2 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
matrix:
xcode: [11, 11.1, 11.2]
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- run: sudo xcode-select -s '/Applications/Xcode_${{ matrix.xcode }}.app'
- run: ./test macos
- run: ./test ios
Expand All @@ -29,6 +29,6 @@ jobs:
matrix:
xcode: [11, 11.1, 11.2]
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- run: sudo xcode-select -s '/Applications/Xcode_${{ matrix.xcode }}.app'
- run: ./test swiftpm
16 changes: 11 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ branches:
- 8.x-branch

language: generic
matrix:
jobs:
include:
- &cocoapods
name: CocoaPods Lint
Expand Down Expand Up @@ -34,8 +34,6 @@ matrix:
- &swiftpm_linux
name: SwiftPM / Linux / Swift 4.2.4
os: linux
dist: trusty
sudo: required
env:
- SWIFT_VERSION=4.2.4
install:
Expand All @@ -47,9 +45,17 @@ matrix:
env:
- SWIFT_VERSION=5.0.3
- <<: *swiftpm_linux
name: SwiftPM / Linux / Swift 5.1 Development
name: SwiftPM / Linux / Swift 5.1.5
env:
- SWIFT_VERSION=5.1-DEVELOPMENT-SNAPSHOT-2019-09-05-a
- SWIFT_VERSION=5.1.5
- <<: *swiftpm_linux
name: SwiftPM / Linux / Swift 5.2.2
env:
- SWIFT_VERSION=5.2.2
- <<: *swiftpm_linux
name: SwiftPM / Linux / Swift Development
env:
- SWIFT_VERSION=DEVELOPMENT-SNAPSHOT-2020-04-17-a
install: true
script:
- ./test $TYPE
Expand Down
77 changes: 3 additions & 74 deletions Sources/NimbleObjectiveC/XCTestObservationCenter+Register.m
Original file line number Diff line number Diff line change
@@ -1,83 +1,12 @@
#import <XCTest/XCTest.h>
#import <objc/runtime.h>

#if __has_include("Nimble-Swift.h")
#import "Nimble-Swift.h"
#else
#import <Nimble/Nimble-Swift.h>
#endif

#pragma mark - Method Swizzling

/// Swaps the implementations between two instance methods.
///
/// @param class The class containing `originalSelector`.
/// @param originalSelector Original method to replace.
/// @param replacementSelector Replacement method.
void swizzleSelectors(Class class, SEL originalSelector, SEL replacementSelector) {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method replacementMethod = class_getInstanceMethod(class, replacementSelector);

BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(replacementMethod),
method_getTypeEncoding(replacementMethod));

if (didAddMethod) {
class_replaceMethod(class,
replacementSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, replacementMethod);
}
}

#pragma mark - Private

@interface XCTestObservationCenter (Private)
- (void)_addLegacyTestObserver:(id)observer;
@end

@implementation XCTestObservationCenter (Register)

/// Uses objc method swizzling to register `CurrentTestCaseTracker` as a test observer. This is necessary
/// because Xcode 7.3 introduced timing issues where if a custom `XCTestObservation` is registered too early
/// it suppresses all console output (generated by `XCTestLog`), breaking any tools that depend on this output.
/// This approach waits to register our custom test observer until XCTest adds its first "legacy" observer,
/// falling back to registering after the first normal observer if this private method ever changes.
+ (void)load {
if (class_getInstanceMethod([self class], @selector(_addLegacyTestObserver:))) {
// Swizzle -_addLegacyTestObserver:
swizzleSelectors([self class], @selector(_addLegacyTestObserver:), @selector(NMB_original__addLegacyTestObserver:));
} else {
// Swizzle -addTestObserver:, only if -_addLegacyTestObserver: is not implemented
swizzleSelectors([self class], @selector(addTestObserver:), @selector(NMB_original_addTestObserver:));
}
__attribute__((constructor))
static void registerCurrentTestCaseTracker(void) {
[[XCTestObservationCenter sharedTestObservationCenter] addTestObserver:[CurrentTestCaseTracker sharedInstance]];
}

#pragma mark - Replacement Methods

/// Registers `CurrentTestCaseTracker` as a test observer after `XCTestLog` has been added.
- (void)NMB_original__addLegacyTestObserver:(id)observer {
[self NMB_original__addLegacyTestObserver:observer];

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self addTestObserver:[CurrentTestCaseTracker sharedInstance]];
});
}

/// Registers `CurrentTestCaseTracker` as a test observer after `XCTestLog` has been added.
/// This method is only used if `-_addLegacyTestObserver:` is not impelemented. (added in Xcode 7.3)
- (void)NMB_original_addTestObserver:(id<XCTestObservation>)observer {
[self NMB_original_addTestObserver:observer];

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self NMB_original_addTestObserver:[CurrentTestCaseTracker sharedInstance]];
});
}

@end