From 68094b38eac2ea068341aa67a517096df4904ed8 Mon Sep 17 00:00:00 2001 From: Kevin Renskers Date: Tue, 22 Nov 2022 10:11:12 +0100 Subject: [PATCH] ref: Remove `- [SentryOptions initWithDict:didFailWithError:]` (#2404) --- CHANGELOG.md | 11 +- Sentry.xcodeproj/project.pbxproj | 4 - Sources/Sentry/Public/SentryOptions.h | 18 +- Sources/Sentry/Public/SentrySDK.h | 10 +- Sources/Sentry/SentryClient.m | 1 - Sources/Sentry/SentryOptions.m | 244 +---- Sources/Sentry/SentrySDK.m | 17 +- .../SentryCrashIntegrationTests.swift | 9 +- Tests/SentryTests/Networking/SentryDsnTests.m | 16 +- .../SentryTransportInitializerTests.swift | 2 +- Tests/SentryTests/SentryClientTests.swift | 4 +- Tests/SentryTests/SentryMetaTests.swift | 14 - Tests/SentryTests/SentryOptionsTest.m | 859 +----------------- Tests/SentryTests/SentryTests.m | 22 +- .../TestUtils/SentryTestObserver.m | 2 +- 15 files changed, 55 insertions(+), 1178 deletions(-) delete mode 100644 Tests/SentryTests/SentryMetaTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index bd915cf1116..8b23eea4cca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,14 +4,17 @@ This version adds a dependency on Swift. -### Breaking Changes - -- Mark `- [SpanProtocol setExtraValue:forKey:]` as deprecated (#2413) - ### Features - Properly demangle Swift class name (#2162) +### Breaking Changes + +- Remove `- [SentryOptions initWithDict:didFailWithError:]` (#2404) +- Rename `- [SentrySDK startWithOptionsObject:]` to `- [SentrySDK startWithOptions:]` (#2404) +- Remove `- [SentryOptions sdkInfo]` (#2404) +- Mark `- [SpanProtocol setExtraValue:forKey:]` as deprecated (#2413) + ## 7.31.2 ### Fixes diff --git a/Sentry.xcodeproj/project.pbxproj b/Sentry.xcodeproj/project.pbxproj index 13373ff7b36..8e62a0e3413 100644 --- a/Sentry.xcodeproj/project.pbxproj +++ b/Sentry.xcodeproj/project.pbxproj @@ -38,7 +38,6 @@ 03F84D3827DD4191008FE43F /* SentryBacktrace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D3127DD4191008FE43F /* SentryBacktrace.cpp */; }; 03F9D37C2819A65C00602916 /* SentryProfilerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 03F9D37B2819A65C00602916 /* SentryProfilerTests.mm */; }; 0A1B497328E597DD00D7BFA3 /* TestLogOutput.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A1B497228E597DD00D7BFA3 /* TestLogOutput.swift */; }; - 0A1C3592287D7107007D01E3 /* SentryMetaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C3591287D7107007D01E3 /* SentryMetaTests.swift */; }; 0A2690B72885C2E000E4432D /* TestSentryPermissionsObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AABE2EF2885C2120057ED69 /* TestSentryPermissionsObserver.swift */; }; 0A283E79291A67E000EF4126 /* SentryUIDeviceWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A283E78291A67E000EF4126 /* SentryUIDeviceWrapperTests.swift */; }; 0A2D7BBA29152CBF008727AF /* SentryOutOfMemoryScopeObserverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2D7BB929152CBF008727AF /* SentryOutOfMemoryScopeObserverTests.swift */; }; @@ -789,7 +788,6 @@ 03F84D3127DD4191008FE43F /* SentryBacktrace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentryBacktrace.cpp; path = Sources/Sentry/SentryBacktrace.cpp; sourceTree = SOURCE_ROOT; }; 03F9D37B2819A65C00602916 /* SentryProfilerTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryProfilerTests.mm; sourceTree = ""; }; 0A1B497228E597DD00D7BFA3 /* TestLogOutput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestLogOutput.swift; sourceTree = ""; }; - 0A1C3591287D7107007D01E3 /* SentryMetaTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMetaTests.swift; sourceTree = ""; }; 0A283E78291A67E000EF4126 /* SentryUIDeviceWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUIDeviceWrapperTests.swift; sourceTree = ""; }; 0A2D7BB929152CBF008727AF /* SentryOutOfMemoryScopeObserverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryOutOfMemoryScopeObserverTests.swift; sourceTree = ""; }; 0A2D8D5A289815C0008720F6 /* SentryBaseIntegration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBaseIntegration.m; sourceTree = ""; }; @@ -1971,7 +1969,6 @@ 630C01931EC3402C00C52CEF /* SentryKSCrashReportConverterTests.m */, 63B819131EC352A7002FDF4C /* SentryInterfacesTests.m */, 63EED6C22237989300E02400 /* SentryOptionsTest.m */, - 0A1C3591287D7107007D01E3 /* SentryMetaTests.swift */, 632331F52404FFA8008D91D6 /* SentryScopeTests.m */, 7B569DFE2590EEF600B653FC /* SentryScope+Equality.h */, 7B569DFF2590EEF600B653FC /* SentryScope+Equality.m */, @@ -3737,7 +3734,6 @@ 7BBD18992449DE9D00427C76 /* TestRateLimits.swift in Sources */, 8E4A038625F76A7600000D77 /* TypeMapping.swift in Sources */, 7B04A9AB24EA5F8D00E710B1 /* SentryUserTests.swift in Sources */, - 0A1C3592287D7107007D01E3 /* SentryMetaTests.swift in Sources */, 7BA61CCF247EB59500C130A8 /* SentryCrashUUIDConversionTests.swift in Sources */, 7BBD188D2448453600427C76 /* SentryHttpDateParserTests.swift in Sources */, 7B72D23A28D074BC0014798A /* TestExtensions.swift in Sources */, diff --git a/Sources/Sentry/Public/SentryOptions.h b/Sources/Sentry/Public/SentryOptions.h index d8ecbf86bc6..797b546ae93 100644 --- a/Sources/Sentry/Public/SentryOptions.h +++ b/Sources/Sentry/Public/SentryOptions.h @@ -3,18 +3,13 @@ NS_ASSUME_NONNULL_BEGIN -@class SentryDsn, SentrySdkInfo, SentryMeasurementValue, SentryHttpStatusCodeRange; +@class SentryDsn, SentryMeasurementValue, SentryHttpStatusCodeRange; NS_SWIFT_NAME(Options) @interface SentryOptions : NSObject -/** - * Init SentryOptions. - * @param options Options dictionary - * @return SentryOptions - */ -- (_Nullable instancetype)initWithDict:(NSDictionary *)options - didFailWithError:(NSError *_Nullable *_Nullable)error; +- (_Nullable instancetype)initWithDsn:(NSString *)dsn + didFailWithError:(NSError *_Nullable *_Nullable)error; /** * The DSN tells the SDK where to send the events to. If this value is not provided, the SDK will @@ -164,13 +159,6 @@ NS_SWIFT_NAME(Options) */ @property (nonatomic, assign) BOOL stitchAsyncCode; -/** - * Describes the Sentry SDK and its configuration used to capture and transmit an event. - * This is reserved for internal use, and will be removed in a future version of the SDK. - */ -@property (nonatomic, readonly, strong) SentrySdkInfo *sdkInfo DEPRECATED_MSG_ATTRIBUTE( - "This property will be removed in a future version of the SDK"); - /** * The maximum size for each attachment in bytes. Default is 20 MiB / 20 * 1024 * 1024 bytes. * diff --git a/Sources/Sentry/Public/SentrySDK.h b/Sources/Sentry/Public/SentrySDK.h index 88846ff4d3b..20f2caaa319 100644 --- a/Sources/Sentry/Public/SentrySDK.h +++ b/Sources/Sentry/Public/SentrySDK.h @@ -26,14 +26,10 @@ SENTRY_NO_INIT @property (class, nonatomic, readonly) BOOL isEnabled; /** - * Inits and configures Sentry (SentryHub, SentryClient) and sets up all integrations. - */ -+ (void)startWithOptions:(NSDictionary *)optionsDict NS_SWIFT_NAME(start(options:)); - -/** - * Inits and configures Sentry (SentryHub, SentryClient) and sets up all integrations. + * Inits and configures Sentry (SentryHub, SentryClient) and sets up all integrations. Make sure to + * set a valid DSN. */ -+ (void)startWithOptionsObject:(SentryOptions *)options NS_SWIFT_NAME(start(options:)); ++ (void)startWithOptions:(SentryOptions *)options NS_SWIFT_NAME(start(options:)); /** * Inits and configures Sentry (SentryHub, SentryClient) and sets up all integrations. Make sure to diff --git a/Sources/Sentry/SentryClient.m b/Sources/Sentry/SentryClient.m index d2ad4e79ff5..2907110eee8 100644 --- a/Sources/Sentry/SentryClient.m +++ b/Sources/Sentry/SentryClient.m @@ -33,7 +33,6 @@ #import "SentryPermissionsObserver.h" #import "SentrySDK+Private.h" #import "SentryScope+Private.h" -#import "SentrySdkInfo.h" #import "SentryStacktraceBuilder.h" #import "SentryThreadInspector.h" #import "SentryTraceContext.h" diff --git a/Sources/Sentry/SentryOptions.m b/Sources/Sentry/SentryOptions.m index d8fc016fadf..667d05a35bd 100644 --- a/Sources/Sentry/SentryOptions.m +++ b/Sources/Sentry/SentryOptions.m @@ -6,7 +6,6 @@ #import "SentryLog.h" #import "SentryMeta.h" #import "SentrySDK.h" -#import "SentrySdkInfo.h" @interface SentryOptions () @@ -131,14 +130,14 @@ - (instancetype)init return self; } -- (_Nullable instancetype)initWithDict:(NSDictionary *)options - didFailWithError:(NSError *_Nullable *_Nullable)error +- (_Nullable instancetype)initWithDsn:(NSString *)dsn + didFailWithError:(NSError *_Nullable *_Nullable)error { if (self = [self init]) { - if (![self validateOptions:options didFailWithError:error]) { - [SentryLog - logWithMessage:[NSString stringWithFormat:@"Failed to initialize: %@", *error] - andLevel:kSentryLevelError]; + self.parsedDsn = [[SentryDsn alloc] initWithString:dsn didFailWithError:error]; + self.dsn = dsn; + + if (error != nil && *error != nil) { return nil; } } @@ -191,237 +190,6 @@ - (void)setDsn:(NSString *)dsn } } -/** - * Populates all `SentryOptions` values from `options` dict using fallbacks/defaults if needed. - */ -- (BOOL)validateOptions:(NSDictionary *)options - didFailWithError:(NSError *_Nullable *_Nullable)error -{ - NSPredicate *isNSString = [NSPredicate predicateWithBlock:^BOOL( - id object, NSDictionary *bindings) { return [object isKindOfClass:[NSString class]]; }]; - - [self setBool:options[@"debug"] block:^(BOOL value) { self->_debug = value; }]; - - if ([options[@"diagnosticLevel"] isKindOfClass:[NSString class]]) { - for (SentryLevel level = 0; level <= kSentryLevelFatal; level++) { - if ([nameForSentryLevel(level) isEqualToString:options[@"diagnosticLevel"]]) { - self.diagnosticLevel = level; - break; - } - } - } - - NSString *dsn = @""; - if (nil != options[@"dsn"] && [options[@"dsn"] isKindOfClass:[NSString class]]) { - dsn = options[@"dsn"]; - } - - self.parsedDsn = [[SentryDsn alloc] initWithString:dsn didFailWithError:error]; - - if ([options[@"release"] isKindOfClass:[NSString class]]) { - self.releaseName = options[@"release"]; - } - - if ([options[@"environment"] isKindOfClass:[NSString class]]) { - self.environment = options[@"environment"]; - } - - if ([options[@"dist"] isKindOfClass:[NSString class]]) { - self.dist = options[@"dist"]; - } - - [self setBool:options[@"enabled"] block:^(BOOL value) { self->_enabled = value; }]; - - [self setBool:options[@"enableCrashHandler"] - block:^(BOOL value) { self->_enableCrashHandler = value; }]; - - if ([options[@"maxBreadcrumbs"] isKindOfClass:[NSNumber class]]) { - self.maxBreadcrumbs = [options[@"maxBreadcrumbs"] unsignedIntValue]; - } - - [self setBool:options[@"enableNetworkBreadcrumbs"] - block:^(BOOL value) { self->_enableNetworkBreadcrumbs = value; }]; - - if ([options[@"maxCacheItems"] isKindOfClass:[NSNumber class]]) { - self.maxCacheItems = [options[@"maxCacheItems"] unsignedIntValue]; - } - - if ([self isBlock:options[@"beforeSend"]]) { - self.beforeSend = options[@"beforeSend"]; - } - - if ([self isBlock:options[@"beforeBreadcrumb"]]) { - self.beforeBreadcrumb = options[@"beforeBreadcrumb"]; - } - - if ([self isBlock:options[@"onCrashedLastRun"]]) { - self.onCrashedLastRun = options[@"onCrashedLastRun"]; - } - - if ([options[@"integrations"] isKindOfClass:[NSArray class]]) { - self.integrations = [options[@"integrations"] filteredArrayUsingPredicate:isNSString]; - } - - if ([options[@"sampleRate"] isKindOfClass:[NSNumber class]]) { - self.sampleRate = options[@"sampleRate"]; - } - - [self setBool:options[@"enableAutoSessionTracking"] - block:^(BOOL value) { self->_enableAutoSessionTracking = value; }]; - - [self setBool:options[@"enableOutOfMemoryTracking"] - block:^(BOOL value) { self->_enableOutOfMemoryTracking = value; }]; - - if ([options[@"sessionTrackingIntervalMillis"] isKindOfClass:[NSNumber class]]) { - self.sessionTrackingIntervalMillis = - [options[@"sessionTrackingIntervalMillis"] unsignedIntValue]; - } - - [self setBool:options[@"attachStacktrace"] - block:^(BOOL value) { self->_attachStacktrace = value; }]; - - [self setBool:options[@"stitchAsyncCode"] - block:^(BOOL value) { self->_stitchAsyncCode = value; }]; - - if ([options[@"maxAttachmentSize"] isKindOfClass:[NSNumber class]]) { - self.maxAttachmentSize = [options[@"maxAttachmentSize"] unsignedIntValue]; - } - - [self setBool:options[@"sendDefaultPii"] - block:^(BOOL value) { self->_sendDefaultPii = value; }]; - - [self setBool:options[@"enableAutoPerformanceTracking"] - block:^(BOOL value) { self->_enableAutoPerformanceTracking = value; }]; - - [self setBool:options[@"enableCaptureFailedRequests"] - block:^(BOOL value) { self->_enableCaptureFailedRequests = value; }]; - -#if SENTRY_HAS_UIKIT - [self setBool:options[@"enableUIViewControllerTracking"] - block:^(BOOL value) { self->_enableUIViewControllerTracking = value; }]; - - [self setBool:options[@"attachScreenshot"] - block:^(BOOL value) { self->_attachScreenshot = value; }]; - - [self setBool:options[@"attachViewHierarchy"] - block:^(BOOL value) { self->_attachViewHierarchy = value; }]; - - [self setBool:options[@"enableUserInteractionTracing"] - block:^(BOOL value) { self->_enableUserInteractionTracing = value; }]; - - if ([options[@"idleTimeout"] isKindOfClass:[NSNumber class]]) { - self.idleTimeout = [options[@"idleTimeout"] doubleValue]; - } - - [self setBool:options[@"enablePreWarmedAppStartTracking"] - block:^(BOOL value) { self->_enablePreWarmedAppStartTracking = value; }]; -#endif - - [self setBool:options[@"enableAppHangTracking"] - block:^(BOOL value) { self->_enableAppHangTracking = value; }]; - - if ([options[@"appHangTimeoutInterval"] isKindOfClass:[NSNumber class]]) { - self.appHangTimeoutInterval = [options[@"appHangTimeoutInterval"] doubleValue]; - } - - [self setBool:options[@"enableNetworkTracking"] - block:^(BOOL value) { self->_enableNetworkTracking = value; }]; - - [self setBool:options[@"enableFileIOTracking"] - block:^(BOOL value) { self->_enableFileIOTracking = value; }]; - - if ([options[@"tracesSampleRate"] isKindOfClass:[NSNumber class]]) { - self.tracesSampleRate = options[@"tracesSampleRate"]; - } - - if ([self isBlock:options[@"tracesSampler"]]) { - self.tracesSampler = options[@"tracesSampler"]; - } - - if ([options[@"inAppIncludes"] isKindOfClass:[NSArray class]]) { - NSArray *inAppIncludes = - [options[@"inAppIncludes"] filteredArrayUsingPredicate:isNSString]; - _inAppIncludes = [_inAppIncludes arrayByAddingObjectsFromArray:inAppIncludes]; - } - - if ([options[@"inAppExcludes"] isKindOfClass:[NSArray class]]) { - _inAppExcludes = [options[@"inAppExcludes"] filteredArrayUsingPredicate:isNSString]; - } - - if ([options[@"urlSessionDelegate"] conformsToProtocol:@protocol(NSURLSessionDelegate)]) { - self.urlSessionDelegate = options[@"urlSessionDelegate"]; - } - - [self setBool:options[@"enableSwizzling"] - block:^(BOOL value) { self->_enableSwizzling = value; }]; - - [self setBool:options[@"enableCoreDataTracking"] - block:^(BOOL value) { self->_enableCoreDataTracking = value; }]; - -#if SENTRY_TARGET_PROFILING_SUPPORTED - if ([options[@"profilesSampleRate"] isKindOfClass:[NSNumber class]]) { - self.profilesSampleRate = options[@"profilesSampleRate"]; - } - - if ([self isBlock:options[@"profilesSampler"]]) { - self.profilesSampler = options[@"profilesSampler"]; - } - - [self setBool:options[@"enableProfiling"] - block:^(BOOL value) { self->_enableProfiling = value; }]; -#endif - - [self setBool:options[@"sendClientReports"] - block:^(BOOL value) { self->_sendClientReports = value; }]; - - [self setBool:options[@"enableAutoBreadcrumbTracking"] - block:^(BOOL value) { self->_enableAutoBreadcrumbTracking = value; }]; - - if ([options[@"tracePropagationTargets"] isKindOfClass:[NSArray class]]) { - self.tracePropagationTargets = options[@"tracePropagationTargets"]; - } - - if ([options[@"failedRequestStatusCodes"] isKindOfClass:[NSArray class]]) { - self.failedRequestStatusCodes = options[@"failedRequestStatusCodes"]; - } - - if ([options[@"failedRequestTargets"] isKindOfClass:[NSArray class]]) { - self.failedRequestTargets = options[@"failedRequestTargets"]; - } - - // SentrySdkInfo already expects a dictionary with {"sdk": {"name": ..., "value": ...}} - // so we're passing the whole options object. - // Note: we should remove this code once the hybrid SDKs move over to the new - // PrivateSentrySDKOnly setter functions. - if ([options[@"sdk"] isKindOfClass:[NSDictionary class]]) { - SentrySdkInfo *defaults = [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName - andVersion:SentryMeta.versionString]; - SentrySdkInfo *sdkInfo = [[SentrySdkInfo alloc] initWithDict:options orDefaults:defaults]; - SentryMeta.versionString = sdkInfo.version; - SentryMeta.sdkName = sdkInfo.name; - } - - if (nil != error && nil != *error) { - return NO; - } else { - return YES; - } -} - -- (SentrySdkInfo *)sdkInfo -{ - return [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName - andVersion:SentryMeta.versionString]; -} - -- (void)setBool:(id)value block:(void (^)(BOOL))block -{ - // Entries in the dictionary can be NSNull. Especially, on React-Native, this can happen. - if (value != nil && ![value isEqual:[NSNull null]]) { - block([value boolValue]); - } -} - - (void)addInAppInclude:(NSString *)inAppInclude { _inAppIncludes = [self.inAppIncludes arrayByAddingObject:inAppInclude]; diff --git a/Sources/Sentry/SentrySDK.m b/Sources/Sentry/SentrySDK.m index df3fc7af53b..2d036a9bb16 100644 --- a/Sources/Sentry/SentrySDK.m +++ b/Sources/Sentry/SentrySDK.m @@ -128,20 +128,7 @@ + (void)setStartInvocations:(NSUInteger)value startInvocations = value; } -+ (void)startWithOptions:(NSDictionary *)optionsDict -{ - NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:optionsDict - didFailWithError:&error]; - if (nil != error) { - SENTRY_LOG_ERROR(@"Error while initializing the SDK"); - SENTRY_LOG_ERROR(@"%@", error); - } else { - [SentrySDK startWithOptionsObject:options]; - } -} - -+ (void)startWithOptionsObject:(SentryOptions *)options ++ (void)startWithOptions:(SentryOptions *)options { startInvocations++; @@ -162,7 +149,7 @@ + (void)startWithConfigureOptions:(void (^)(SentryOptions *options))configureOpt { SentryOptions *options = [[SentryOptions alloc] init]; configureOptions(options); - [SentrySDK startWithOptionsObject:options]; + [SentrySDK startWithOptions:options]; } + (void)captureCrashEvent:(SentryEvent *)event diff --git a/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift b/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift index 1e531017325..ba1cd5a62df 100644 --- a/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift @@ -77,10 +77,11 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase { let releaseName = "1.0.0" let dist = "14G60" // The start of the SDK installs all integrations - SentrySDK.start(options: ["dsn": SentryCrashIntegrationTests.dsnAsString, - "release": releaseName, - "dist": dist] - ) + SentrySDK.start { options in + options.dsn = SentryCrashIntegrationTests.dsnAsString + options.releaseName = releaseName + options.dist = dist + } // To test this properly we need SentryCrash and SentryCrashIntegration installed and registered on the current hub of the SDK. diff --git a/Tests/SentryTests/Networking/SentryDsnTests.m b/Tests/SentryTests/Networking/SentryDsnTests.m index bd6f7b4d4e5..0d6fed5e14b 100644 --- a/Tests/SentryTests/Networking/SentryDsnTests.m +++ b/Tests/SentryTests/Networking/SentryDsnTests.m @@ -14,8 +14,8 @@ @implementation SentryDsnTests - (void)testMissingUsernamePassword { NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"https://sentry.io" } - didFailWithError:&error]; + SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"https://sentry.io" + didFailWithError:&error]; XCTAssertEqual(kSentryErrorInvalidDsnError, error.code); XCTAssertNil(options); } @@ -62,8 +62,8 @@ - (void)testDsnHeaderUsername - (void)testMissingScheme { NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"sentry.io" } - didFailWithError:&error]; + SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"sentry.io" + didFailWithError:&error]; XCTAssertEqual(kSentryErrorInvalidDsnError, error.code); XCTAssertNil(options); } @@ -71,8 +71,8 @@ - (void)testMissingScheme - (void)testMissingHost { NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"http:///1" } - didFailWithError:&error]; + SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"http:///1" + didFailWithError:&error]; XCTAssertEqual(kSentryErrorInvalidDsnError, error.code); XCTAssertNil(options); } @@ -80,8 +80,8 @@ - (void)testMissingHost - (void)testUnsupportedProtocol { NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"ftp://sentry.io/1" } - didFailWithError:&error]; + SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"ftp://sentry.io/1" + didFailWithError:&error]; XCTAssertEqual(kSentryErrorInvalidDsnError, error.code); XCTAssertNil(options); } diff --git a/Tests/SentryTests/Networking/SentryTransportInitializerTests.swift b/Tests/SentryTests/Networking/SentryTransportInitializerTests.swift index 6651dcc3e64..d99e307382a 100644 --- a/Tests/SentryTests/Networking/SentryTransportInitializerTests.swift +++ b/Tests/SentryTests/Networking/SentryTransportInitializerTests.swift @@ -20,7 +20,7 @@ class SentryTransportInitializerTests: XCTestCase { } func testDefault() throws { - let options = try Options(dict: ["dsn": SentryTransportInitializerTests.dsnAsString]) + let options = try Options(dsn: SentryTransportInitializerTests.dsnAsString) let result = TransportInitializer.initTransport(options, sentryFileManager: fileManager) diff --git a/Tests/SentryTests/SentryClientTests.swift b/Tests/SentryTests/SentryClientTests.swift index 188bdd0fb50..7a7d613aa30 100644 --- a/Tests/SentryTests/SentryClientTests.swift +++ b/Tests/SentryTests/SentryClientTests.swift @@ -60,9 +60,7 @@ class SentryClientTest: XCTestCase { func getSut(configureOptions: (Options) -> Void = { _ in }) -> Client { var client: Client! do { - let options = try Options(dict: [ - "dsn": SentryClientTest.dsn - ]) + let options = try Options(dsn: SentryClientTest.dsn) configureOptions(options) client = Client( diff --git a/Tests/SentryTests/SentryMetaTests.swift b/Tests/SentryTests/SentryMetaTests.swift deleted file mode 100644 index 6150f240060..00000000000 --- a/Tests/SentryTests/SentryMetaTests.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Sentry -import XCTest - -class SentryMetaTest: XCTestCase { - func testChangeVersion() { - SentryMeta.versionString = "0.0.1" - XCTAssertEqual(SentryMeta.versionString, "0.0.1") - } - - func testChangeName() { - SentryMeta.sdkName = "test" - XCTAssertEqual(SentryMeta.sdkName, "test") - } -} diff --git a/Tests/SentryTests/SentryOptionsTest.m b/Tests/SentryTests/SentryOptionsTest.m index 4ba6a70abcc..fe8a8677231 100644 --- a/Tests/SentryTests/SentryOptionsTest.m +++ b/Tests/SentryTests/SentryOptionsTest.m @@ -1,7 +1,6 @@ #import "SentryOptions.h" #import "SentryError.h" #import "SentrySDK.h" -#import "SentrySdkInfo.h" #import "SentryTests-Swift.h" #import @@ -14,22 +13,8 @@ @implementation SentryOptionsTest - (void)testEmptyDsn { NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{} didFailWithError:&error]; + SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"" didFailWithError:&error]; - [self assertDsnNil:options andError:error]; -} - -- (void)testInvalidDsnBoolean -{ - NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @YES } - didFailWithError:&error]; - - [self assertDsnNil:options andError:error]; -} - -- (void)assertDsnNil:(SentryOptions *)options andError:(NSError *)error -{ XCTAssertNil(options.parsedDsn); XCTAssertEqual(NO, options.debug); XCTAssertEqual(kSentryErrorInvalidDsnError, error.code); @@ -38,353 +23,12 @@ - (void)assertDsnNil:(SentryOptions *)options andError:(NSError *)error - (void)testInvalidDsn { NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ @"dsn" : @"https://sentry.io" } - didFailWithError:&error]; + SentryOptions *options = [[SentryOptions alloc] initWithDsn:@"https://sentry.io" + didFailWithError:&error]; XCTAssertEqual(kSentryErrorInvalidDsnError, error.code); XCTAssertNil(options); } -- (void)testRelease -{ - SentryOptions *options = [self getValidOptions:@{ @"release" : @"abc" }]; - XCTAssertEqualObjects(options.releaseName, @"abc"); -} - -- (void)testSetEmptyRelease -{ - SentryOptions *options = [self getValidOptions:@{ @"release" : @"" }]; - XCTAssertEqualObjects(options.releaseName, @""); -} - -- (void)testSetReleaseToNonString -{ - SentryOptions *options = [self getValidOptions:@{ @"release" : @2 }]; - XCTAssertEqualObjects(options.releaseName, [self buildDefaultReleaseName]); -} - -- (void)testNoReleaseSetUsesDefault -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertEqualObjects(options.releaseName, [self buildDefaultReleaseName]); -} - -- (NSString *)buildDefaultReleaseName -{ - NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; - return [NSString stringWithFormat:@"%@@%@+%@", infoDict[@"CFBundleIdentifier"], - infoDict[@"CFBundleShortVersionString"], infoDict[@"CFBundleVersion"]]; -} - -- (void)testEnvironment -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertNil(options.environment); - - options = [self getValidOptions:@{ @"environment" : @"xxx" }]; - XCTAssertEqualObjects(options.environment, @"xxx"); -} - -- (void)testDist -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertNil(options.dist); - - options = [self getValidOptions:@{ @"dist" : @"hhh" }]; - XCTAssertEqualObjects(options.dist, @"hhh"); -} - -- (void)testValidDebug -{ - [self testDebugWith:@YES expected:YES]; - [self testDebugWith:@"YES" expected:YES]; - [self testDebugWith:@(YES) expected:YES]; -} - -- (void)testInvalidDebug -{ - [self testDebugWith:@"Invalid" expected:NO]; - [self testDebugWith:@NO expected:NO]; - [self testDebugWith:@(NO) expected:NO]; -} - -- (void)testDebugWith:(NSObject *)debugValue expected:(BOOL)expectedDebugValue -{ - NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ - @"dsn" : @"https://username:password@sentry.io/1", - @"debug" : debugValue - } - didFailWithError:&error]; - - XCTAssertNil(error); - XCTAssertEqual(expectedDebugValue, options.debug); -} - -- (void)testValidDiagnosticLevel -{ - [self testDiagnosticlevelWith:@"none" expected:kSentryLevelNone]; - [self testDiagnosticlevelWith:@"debug" expected:kSentryLevelDebug]; - [self testDiagnosticlevelWith:@"info" expected:kSentryLevelInfo]; - [self testDiagnosticlevelWith:@"warning" expected:kSentryLevelWarning]; - [self testDiagnosticlevelWith:@"error" expected:kSentryLevelError]; - [self testDiagnosticlevelWith:@"fatal" expected:kSentryLevelFatal]; -} - -- (void)testInvalidDiagnosticLevel -{ - [self testDiagnosticlevelWith:@"fatala" expected:kSentryLevelDebug]; - [self testDiagnosticlevelWith:@(YES) expected:kSentryLevelDebug]; -} - -- (void)testDiagnosticlevelWith:(NSObject *)level expected:(SentryLevel)expected -{ - SentryOptions *options = [self getValidOptions:@{ @"diagnosticLevel" : level }]; - - XCTAssertEqual(expected, options.diagnosticLevel); -} - -- (void)testValidEnabled -{ - [self testEnabledWith:@YES expected:YES]; - [self testEnabledWith:@"YES" expected:YES]; - [self testEnabledWith:@(YES) expected:YES]; -} - -- (void)testInvalidEnabled -{ - [self testEnabledWith:@"Invalid" expected:NO]; - [self testEnabledWith:@NO expected:NO]; - [self testEnabledWith:@(NO) expected:NO]; -} - -- (void)testEnabledWith:(NSObject *)enabledValue expected:(BOOL)expectedValue -{ - SentryOptions *options = [self getValidOptions:@{ @"enabled" : enabledValue }]; - - XCTAssertEqual(expectedValue, options.enabled); -} - -- (void)testMaxBreadcrumbs -{ - NSNumber *maxBreadcrumbs = @20; - - SentryOptions *options = [self getValidOptions:@{ @"maxBreadcrumbs" : maxBreadcrumbs }]; - - XCTAssertEqual([maxBreadcrumbs unsignedIntValue], options.maxBreadcrumbs); -} - -- (void)testEnableNetworkBreadcrumbs -{ - [self testBooleanField:@"enableNetworkBreadcrumbs"]; -} - -- (void)testEnableAutoBreadcrumbTracking -{ - [self testBooleanField:@"enableAutoBreadcrumbTracking"]; -} - -- (void)testEnableCoreDataTracking -{ - [self testBooleanField:@"enableCoreDataTracking" defaultValue:NO]; -} - -- (void)testSendClientReports -{ - [self testBooleanField:@"sendClientReports" defaultValue:YES]; -} - -- (void)testDefaultMaxBreadcrumbs -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertEqual([@100 unsignedIntValue], options.maxBreadcrumbs); -} - -- (void)testMaxBreadcrumbsGarbage -{ - SentryOptions *options = [self getValidOptions:@{ @"maxBreadcrumbs" : self }]; - - XCTAssertEqual(100, options.maxBreadcrumbs); -} - -- (void)testMaxCacheItems -{ - NSNumber *maxCacheItems = @20; - - SentryOptions *options = [self getValidOptions:@{ @"maxCacheItems" : maxCacheItems }]; - - XCTAssertEqual([maxCacheItems unsignedIntValue], options.maxCacheItems); -} - -- (void)testMaxCacheItemsGarbage -{ - SentryOptions *options = [self getValidOptions:@{ @"maxCacheItems" : self }]; - - XCTAssertEqual(30, options.maxCacheItems); -} - -- (void)testDefaultMaxCacheItems -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertEqual([@30 unsignedIntValue], options.maxCacheItems); -} - -- (void)testBeforeSend -{ - SentryBeforeSendEventCallback callback = ^(SentryEvent *event) { return event; }; - SentryOptions *options = [self getValidOptions:@{ @"beforeSend" : callback }]; - - XCTAssertEqual(callback, options.beforeSend); -} - -- (void)testDefaultBeforeSend -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertNil(options.beforeSend); -} - -- (void)testGarbageBeforeSend_ReturnsNil -{ - SentryOptions *options = [self getValidOptions:@{ @"beforeSend" : @"fault" }]; - - XCTAssertNil(options.beforeSend); -} - -- (void)testNSNullBeforeSend_ReturnsNil -{ - SentryOptions *options = [self getValidOptions:@{ @"beforeSend" : [NSNull null] }]; - - XCTAssertFalse([options.beforeSend isEqual:[NSNull null]]); -} - -- (void)testBeforeBreadcrumb -{ - SentryBeforeBreadcrumbCallback callback - = ^(SentryBreadcrumb *breadcrumb) { return breadcrumb; }; - SentryOptions *options = [self getValidOptions:@{ @"beforeBreadcrumb" : callback }]; - - XCTAssertEqual(callback, options.beforeBreadcrumb); -} - -- (void)testDefaultBeforeBreadcrumb -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertNil(options.beforeBreadcrumb); -} - -- (void)testTracePropagationTargets -{ - SentryOptions *options = - [self getValidOptions:@{ @"tracePropagationTargets" : @[ @"localhost" ] }]; - - XCTAssertEqual(options.tracePropagationTargets.count, 1); - XCTAssertEqual(options.tracePropagationTargets[0], @"localhost"); -} - -- (void)testTracePropagationTargetsInvalidInstanceDoesntCrash -{ - SentryOptions *options = [self getValidOptions:@{ @"tracePropagationTargets" : @[ @YES ] }]; - - XCTAssertEqual(options.tracePropagationTargets.count, 1); - XCTAssertEqual(options.tracePropagationTargets[0], @YES); -} - -- (void)testFailedRequestTargets -{ - SentryOptions *options = - [self getValidOptions:@{ @"failedRequestTargets" : @[ @"localhost" ] }]; - - XCTAssertEqual(options.failedRequestTargets.count, 1); - XCTAssertEqual(options.failedRequestTargets[0], @"localhost"); -} - -- (void)testFailedRequestTargetsInvalidInstanceDoesntCrash -{ - SentryOptions *options = [self getValidOptions:@{ @"failedRequestTargets" : @[ @YES ] }]; - - XCTAssertEqual(options.failedRequestTargets.count, 1); - XCTAssertEqual(options.failedRequestTargets[0], @YES); -} - -- (void)testEnableCaptureFailedRequests -{ - [self testBooleanField:@"enableCaptureFailedRequests" defaultValue:NO]; -} - -- (void)testFailedRequestStatusCodes -{ - SentryHttpStatusCodeRange *httpStatusCodeRange = - [[SentryHttpStatusCodeRange alloc] initWithMin:400 max:599]; - SentryOptions *options = - [self getValidOptions:@{ @"failedRequestStatusCodes" : @[ httpStatusCodeRange ] }]; - - XCTAssertEqual(options.failedRequestStatusCodes.count, 1); - XCTAssertEqual(options.failedRequestStatusCodes[0].min, 400); - XCTAssertEqual(options.failedRequestStatusCodes[0].max, 599); -} - -- (void)testGarbageBeforeBreadcrumb_ReturnsNil -{ - SentryOptions *options = [self getValidOptions:@{ @"beforeBreadcrumb" : @"fault" }]; - - XCTAssertEqual(nil, options.beforeBreadcrumb); -} - -- (void)testOnCrashedLastRun -{ - __block BOOL onCrashedLastRunCalled = NO; - SentryOnCrashedLastRunCallback callback = ^(SentryEvent *event) { - onCrashedLastRunCalled = YES; - XCTAssertNotNil(event); - }; - SentryOptions *options = [self getValidOptions:@{ @"onCrashedLastRun" : callback }]; - - options.onCrashedLastRun([[SentryEvent alloc] init]); - - XCTAssertEqual(callback, options.onCrashedLastRun); - XCTAssertTrue(onCrashedLastRunCalled); -} - -- (void)testDefaultOnCrashedLastRun -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertNil(options.onCrashedLastRun); -} - -- (void)testGarbageOnCrashedLastRun_ReturnsNil -{ - SentryOptions *options = [self getValidOptions:@{ @"onCrashedLastRun" : @"fault" }]; - - XCTAssertNil(options.onCrashedLastRun); -} - -- (void)testIntegrations -{ - NSArray *integrations = @[ @"integration1", @"integration2" ]; - SentryOptions *options = [self getValidOptions:@{ @"integrations" : integrations }]; - - [self assertArrayEquals:integrations actual:options.integrations]; -} - -- (void)testDefaultIntegrations -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertTrue([[SentryOptions defaultIntegrations] isEqualToArray:options.integrations], - @"Default integrations are not set correctly"); -} - -- (void)testSampleRateWithDict -{ - NSNumber *sampleRate = @0.1; - SentryOptions *options = [self getValidOptions:@{ @"sampleRate" : sampleRate }]; - XCTAssertEqual(sampleRate, options.sampleRate); -} - - (void)testSampleRate_SetToNil { SentryOptions *options = [[SentryOptions alloc] init]; @@ -424,54 +68,6 @@ - (void)testSampleRateUpperBound XCTAssertEqual(@1, options.sampleRate); } -- (void)testSampleRateNotSet -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertEqual(@1, options.sampleRate); -} - -- (void)testEnableAutoSessionTracking -{ - [self testBooleanField:@"enableAutoSessionTracking"]; -} - -- (void)testEnableOutOfMemoryTracking -{ - [self testBooleanField:@"enableOutOfMemoryTracking"]; -} - -- (void)testSessionTrackingIntervalMillis -{ - NSNumber *sessionTracking = @2000; - SentryOptions *options = - [self getValidOptions:@{ @"sessionTrackingIntervalMillis" : sessionTracking }]; - - XCTAssertEqual([sessionTracking unsignedIntValue], options.sessionTrackingIntervalMillis); -} - -- (void)testDefaultSessionTrackingIntervalMillis -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertEqual([@30000 unsignedIntValue], options.sessionTrackingIntervalMillis); -} - -- (void)testAttachStackTrace -{ - [self testBooleanField:@"attachStacktrace"]; -} - -- (void)testStitchAsyncCodeDisabledPerDefault -{ - [self testBooleanField:@"stitchAsyncCode" defaultValue:NO]; -} - -- (void)testEnableIOTracking -{ - [self testBooleanField:@"enableFileIOTracking" defaultValue:NO]; -} - - (void)testEmptyConstructorSetsDefaultValues { SentryOptions *options = [[SentryOptions alloc] init]; @@ -479,57 +75,6 @@ - (void)testEmptyConstructorSetsDefaultValues [self assertDefaultValues:options]; } -- (void)testNSNull_SetsDefaultValue -{ - SentryOptions *options = [[SentryOptions alloc] initWithDict:@{ - @"dsn" : [NSNull null], - @"enabled" : [NSNull null], - @"debug" : [NSNull null], - @"diagnosticLevel" : [NSNull null], - @"release" : [NSNull null], - @"environment" : [NSNull null], - @"dist" : [NSNull null], - @"maxBreadcrumbs" : [NSNull null], - @"enableNetworkBreadcrumbs" : [NSNull null], - @"maxCacheItems" : [NSNull null], - @"beforeSend" : [NSNull null], - @"beforeBreadcrumb" : [NSNull null], - @"onCrashedLastRun" : [NSNull null], - @"integrations" : [NSNull null], - @"sampleRate" : [NSNull null], - @"enableAutoSessionTracking" : [NSNull null], - @"enableOutOfMemoryTracking" : [NSNull null], - @"sessionTrackingIntervalMillis" : [NSNull null], - @"attachStacktrace" : [NSNull null], - @"stitchAsyncCode" : [NSNull null], - @"maxAttachmentSize" : [NSNull null], - @"sendDefaultPii" : [NSNull null], - @"enableAutoPerformanceTracking" : [NSNull null], -#if SENTRY_HAS_UIKIT - @"enableUIViewControllerTracking" : [NSNull null], - @"attachScreenshot" : [NSNull null], -#endif - @"enableAppHangTracking" : [NSNull null], - @"appHangTimeoutInterval" : [NSNull null], - @"enableNetworkTracking" : [NSNull null], - @"enableAutoBreadcrumbTracking" : [NSNull null], - @"tracesSampleRate" : [NSNull null], - @"tracesSampler" : [NSNull null], - @"inAppIncludes" : [NSNull null], - @"inAppExcludes" : [NSNull null], - @"urlSessionDelegate" : [NSNull null], - @"enableSwizzling" : [NSNull null], - @"enableIOTracking" : [NSNull null], - @"sdk" : [NSNull null], - @"enableCaptureFailedRequests" : [NSNull null], - @"failedRequestStatusCodes" : [NSNull null], - } - didFailWithError:nil]; - - XCTAssertNotNil(options.parsedDsn); - [self assertDefaultValues:options]; -} - - (void)assertDefaultValues:(SentryOptions *)options { XCTAssertEqual(YES, options.enabled); @@ -591,12 +136,6 @@ - (void)assertDefaultValues:(SentryOptions *)options XCTAssertNil(options.profilesSampleRate); XCTAssertNil(options.profilesSampler); #endif - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - XCTAssertEqual(SentryMeta.sdkName, options.sdkInfo.name); - XCTAssertEqual(SentryMeta.versionString, options.sdkInfo.version); -#pragma clang diagnostic pop } - (void)testSetValidDsn @@ -633,175 +172,6 @@ - (void)testSetInvalidValidDsn XCTAssertEqual(YES, options.enabled); } -- (void)testSdkInfo -{ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - SentryOptions *options = [[SentryOptions alloc] init]; - XCTAssertEqual(SentryMeta.sdkName, options.sdkInfo.name); - XCTAssertEqual(SentryMeta.versionString, options.sdkInfo.version); -#pragma clang diagnostic pop -} - -- (void)testSetCustomSdkInfo -{ - NSDictionary *dict = @{ @"name" : @"custom.sdk", @"version" : @"1.2.3-alpha.0" }; - - NSError *error = nil; - SentryOptions *options = - [[SentryOptions alloc] initWithDict:@{ @"sdk" : dict, @"dsn" : @"https://a:b@c.d/1" } - didFailWithError:&error]; - - XCTAssertNil(error); - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - XCTAssertEqual(dict[@"name"], options.sdkInfo.name); - XCTAssertEqual(dict[@"version"], options.sdkInfo.version); -#pragma clang diagnostic pop - - NSDictionary *info = [[NSBundle bundleForClass:[SentryClient class]] infoDictionary]; - NSString *version = [NSString stringWithFormat:@"%@", info[@"CFBundleShortVersionString"]]; - SentryMeta.versionString = version; -} - -- (void)testSetCustomSdkName -{ - NSDictionary *dict = @{ @"name" : @"custom.sdk" }; - NSString *originalVersion = SentryMeta.versionString; - - NSError *error = nil; - SentryOptions *options = - [[SentryOptions alloc] initWithDict:@{ @"sdk" : dict, @"dsn" : @"https://a:b@c.d/1" } - didFailWithError:&error]; - - XCTAssertNil(error); - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - XCTAssertEqual(dict[@"name"], options.sdkInfo.name); - // version stays unchanged - XCTAssertEqual(SentryMeta.versionString, options.sdkInfo.version); - XCTAssertEqual(SentryMeta.versionString, originalVersion); -#pragma clang diagnostic pop -} - -- (void)testSetCustomSdkVersion -{ - NSDictionary *dict = @{ @"version" : @"1.2.3-alpha.0" }; - NSString *originalName = SentryMeta.sdkName; - - NSError *error = nil; - SentryOptions *options = - [[SentryOptions alloc] initWithDict:@{ @"sdk" : dict, @"dsn" : @"https://a:b@c.d/1" } - didFailWithError:&error]; - - XCTAssertNil(error); - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - XCTAssertEqual(dict[@"version"], options.sdkInfo.version); - // name stays unchanged - XCTAssertEqual(SentryMeta.sdkName, options.sdkInfo.name); - XCTAssertEqual(SentryMeta.sdkName, originalName); -#pragma clang diagnostic pop - - NSDictionary *info = [[NSBundle bundleForClass:[SentryClient class]] infoDictionary]; - NSString *version = [NSString stringWithFormat:@"%@", info[@"CFBundleShortVersionString"]]; - SentryMeta.versionString = version; -} - -- (void)testMaxAttachmentSize -{ - NSNumber *maxAttachmentSize = @21; - SentryOptions *options = [self getValidOptions:@{ @"maxAttachmentSize" : maxAttachmentSize }]; - - XCTAssertEqual([maxAttachmentSize unsignedIntValue], options.maxAttachmentSize); -} - -- (void)testDefaultMaxAttachmentSize -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertEqual(20 * 1024 * 1024, options.maxAttachmentSize); -} - -- (void)testSendDefaultPii -{ - [self testBooleanField:@"sendDefaultPii" defaultValue:NO]; -} - -- (void)testEnableAutoPerformanceTracking -{ - [self testBooleanField:@"enableAutoPerformanceTracking"]; -} - -#if SENTRY_HAS_UIKIT -- (void)testEnableUIViewControllerTracking -{ - [self testBooleanField:@"enableUIViewControllerTracking"]; -} - -- (void)testAttachScreenshot -{ - [self testBooleanField:@"attachScreenshot" defaultValue:NO]; -} - -- (void)testEnableUserInteractionTracking -{ - [self testBooleanField:@"enableUserInteractionTracing" defaultValue:NO]; -} - -- (void)testIdleTimeout -{ - NSNumber *idleTimeout = @2.1; - SentryOptions *options = [self getValidOptions:@{ @"idleTimeout" : idleTimeout }]; - - XCTAssertEqual([idleTimeout doubleValue], options.idleTimeout); -} - -- (void)testEnablePreWarmedAppStartTracking -{ - [self testBooleanField:@"enablePreWarmedAppStartTracking" defaultValue:NO]; -} - -#endif - -- (void)testEnableAppHangTracking -{ - [self testBooleanField:@"enableAppHangTracking" defaultValue:NO]; -} - -- (void)testDefaultAppHangsTimeout -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertEqual(2, options.appHangTimeoutInterval); -} - -- (void)testEnableNetworkTracking -{ - [self testBooleanField:@"enableNetworkTracking"]; -} - -- (void)testEnableSwizzling -{ - [self testBooleanField:@"enableSwizzling"]; -} - -- (void)testTracesSampleRate -{ - SentryOptions *options = [self getValidOptions:@{ @"tracesSampleRate" : @0.1 }]; - - XCTAssertEqual(options.tracesSampleRate.doubleValue, 0.1); -} - -- (void)testDefaultTracesSampleRate -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertNil(options.tracesSampleRate); -} - - (void)testTracesSampleRate_SetToNil { SentryOptions *options = [[SentryOptions alloc] init]; @@ -841,36 +211,6 @@ - (void)testTracesSampleRateUpperBound XCTAssertNil(options.tracesSampleRate); } -- (double)tracesSamplerCallback:(NSDictionary *)context -{ - return 0.1; -} - -- (void)testTracesSampler -{ - SentryTracesSamplerCallback sampler = ^(SentrySamplingContext *context) { - XCTAssertNotNil(context); - return @1.0; - }; - - SentryOptions *options = [self getValidOptions:@{ @"tracesSampler" : sampler }]; - - SentrySamplingContext *context = [[SentrySamplingContext alloc] init]; - XCTAssertEqual(options.tracesSampler(context), @1.0); -} - -- (void)testDefaultTracesSampler -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertNil(options.tracesSampler); -} - -- (void)testGarbageTracesSampler_ReturnsNil -{ - SentryOptions *options = [self getValidOptions:@{ @"tracesSampler" : @"fault" }]; - XCTAssertNil(options.tracesSampler); -} - - (void)testIsTracingEnabled_NothingSet_IsDisabled { SentryOptions *options = [[SentryOptions alloc] init]; @@ -902,25 +242,6 @@ - (void)testIsTracingEnabled_TracesSamplerSet_IsEnabled } #if SENTRY_TARGET_PROFILING_SUPPORTED -- (void)testEnableProfiling -{ - [self testBooleanField:@"enableProfiling" defaultValue:NO]; -} - -- (void)testProfilesSampleRate -{ - SentryOptions *options = [self getValidOptions:@{ @"profilesSampleRate" : @0.1 }]; - - XCTAssertEqual(options.profilesSampleRate.doubleValue, 0.1); -} - -- (void)testDefaultProfilesSampleRate -{ - SentryOptions *options = [self getValidOptions:@{}]; - - XCTAssertNil(options.profilesSampleRate); -} - - (void)testProfilesSampleRate_SetToNil { SentryOptions *options = [[SentryOptions alloc] init]; @@ -999,182 +320,8 @@ - (void)testIsProfilingEnabled_EnableProfilingSet_IsEnabled # pragma clang diagnostic pop XCTAssertTrue(options.isProfilingEnabled); } - -- (double)profilesSamplerCallback:(NSDictionary *)context -{ - return 0.1; -} - -- (void)testProfilesSampler -{ - SentryTracesSamplerCallback sampler = ^(SentrySamplingContext *context) { - XCTAssertNotNil(context); - return @1.0; - }; - - SentryOptions *options = [self getValidOptions:@{ @"profilesSampler" : sampler }]; - - SentrySamplingContext *context = [[SentrySamplingContext alloc] init]; - XCTAssertEqual(options.profilesSampler(context), @1.0); -} - -- (void)testDefaultProfilesSampler -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertNil(options.profilesSampler); -} - -- (void)testGarbageProfilesSampler_ReturnsNil -{ - SentryOptions *options = [self getValidOptions:@{ @"profilesSampler" : @"fault" }]; - XCTAssertNil(options.profilesSampler); -} #endif -- (void)testInAppIncludes -{ - NSArray *expected = @[ @"iOS-Swift", @"BusinessLogic" ]; - NSArray *inAppIncludes = @[ @"iOS-Swift", @"BusinessLogic", @1 ]; - SentryOptions *options = [self getValidOptions:@{ @"inAppIncludes" : inAppIncludes }]; - - NSString *bundleExecutable = [self getBundleExecutable]; - if (nil != bundleExecutable) { - expected = [expected arrayByAddingObject:bundleExecutable]; - } - - [self assertArrayEquals:expected actual:options.inAppIncludes]; -} - -- (void)testAddInAppIncludes -{ - SentryOptions *options = [self getValidOptions:@{}]; - [options addInAppInclude:@"App"]; - - NSArray *expected = @[ @"App" ]; - NSString *bundleExecutable = [self getBundleExecutable]; - if (nil != bundleExecutable) { - expected = [expected arrayByAddingObject:bundleExecutable]; - } - - [self assertArrayEquals:expected actual:options.inAppIncludes]; -} - -- (NSString *)getBundleExecutable -{ - NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; - return infoDict[@"CFBundleExecutable"]; -} - -- (void)testDefaultInAppIncludes -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertEqualObjects([self getDefaultInAppIncludes], options.inAppIncludes); -} - -- (void)testInAppExcludes -{ - NSArray *expected = @[ @"Sentry" ]; - NSArray *inAppExcludes = @[ @"Sentry", @2 ]; - - SentryOptions *options = [self getValidOptions:@{ @"inAppExcludes" : inAppExcludes }]; - - XCTAssertEqualObjects(expected, options.inAppExcludes); -} - -- (void)testAddInAppExcludes -{ - SentryOptions *options = [self getValidOptions:@{}]; - [options addInAppExclude:@"App"]; - XCTAssertEqualObjects(@[ @"App" ], options.inAppExcludes); -} - -- (void)testDefaultInAppExcludes -{ - SentryOptions *options = [self getValidOptions:@{}]; - XCTAssertEqualObjects(@[], options.inAppExcludes); -} - -- (SentryOptions *)getValidOptions:(NSDictionary *)dict -{ - NSError *error = nil; - - NSMutableDictionary *options = [[NSMutableDictionary alloc] init]; - options[@"dsn"] = @"https://username:password@sentry.io/1"; - - [options addEntriesFromDictionary:dict]; - - SentryOptions *sentryOptions = [[SentryOptions alloc] initWithDict:options - didFailWithError:&error]; - XCTAssertNil(error); - return sentryOptions; -} - -- (void)testUrlSessionDelegate -{ - id urlSessionDelegate = [[UrlSessionDelegateSpy alloc] init]; - - SentryOptions *options = [self getValidOptions:@{ @"urlSessionDelegate" : urlSessionDelegate }]; - - XCTAssertNotNil(options.urlSessionDelegate); -} - -- (void)testSdkInfoChanges -{ - SentryOptions *options = [self getValidOptions:@{}]; - SentryMeta.sdkName = @"new name"; - SentryMeta.versionString = @"0.0.6"; - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - XCTAssertEqual(options.sdkInfo.name, SentryMeta.sdkName); - XCTAssertEqual(options.sdkInfo.version, SentryMeta.versionString); -#pragma clang diagnostic pop -} - -- (void)assertArrayEquals:(NSArray *)expected actual:(NSArray *)actual -{ - XCTAssertEqualObjects([expected sortedArrayUsingSelector:@selector(compare:)], - [actual sortedArrayUsingSelector:@selector(compare:)]); -} - -- (void)testBooleanField:(NSString *)property -{ - [self testBooleanField:property defaultValue:YES]; -} - -- (void)testBooleanField:(NSString *)property defaultValue:(BOOL)defaultValue -{ - // Opposite of default - SentryOptions *options = [self getValidOptions:@{ property : @(!defaultValue) }]; - XCTAssertEqual(!defaultValue, [self getProperty:property of:options]); - - // Default - options = [self getValidOptions:@{}]; - XCTAssertEqual(defaultValue, [self getProperty:property of:options]); - - // Garbage - options = [self getValidOptions:@{ property : @"" }]; - XCTAssertEqual(NO, [self getProperty:property of:options]); -} - -- (BOOL)getProperty:(NSString *)property of:(SentryOptions *)options -{ - SEL selector = NSSelectorFromString(property); - NSAssert( - [options respondsToSelector:selector], @"Options doesn't have a property '%@'", property); - - NSInvocation *invocation = [NSInvocation - invocationWithMethodSignature:[[options class] - instanceMethodSignatureForSelector:selector]]; - [invocation setSelector:selector]; - [invocation setTarget:options]; - [invocation invoke]; - BOOL result; - [invocation getReturnValue:&result]; - - return result; -} - - (NSArray *)getDefaultInAppIncludes { NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; diff --git a/Tests/SentryTests/SentryTests.m b/Tests/SentryTests/SentryTests.m index c3116499191..b1907268b03 100644 --- a/Tests/SentryTests/SentryTests.m +++ b/Tests/SentryTests/SentryTests.m @@ -45,9 +45,9 @@ - (void)testVersion - (void)testSharedClient { NSError *error = nil; - SentryOptions *options = [[SentryOptions alloc] - initWithDict:@{ @"dsn" : @"https://username:password@app.getsentry.com/12345" } - didFailWithError:&error]; + SentryOptions *options = + [[SentryOptions alloc] initWithDsn:@"https://username:password@app.getsentry.com/12345" + didFailWithError:&error]; SentryClient *client = [[SentryClient alloc] initWithOptions:options]; XCTAssertNil(error); @@ -59,14 +59,18 @@ - (void)testSharedClient - (void)testSDKDefaultHub { - [SentrySDK startWithOptions:@{ @"dsn" : @"https://username:password@app.getsentry.com/12345" }]; + [SentrySDK startWithConfigureOptions:^(SentryOptions *_Nonnull options) { + options.dsn = @"https://username:password@app.getsentry.com/12345"; + }]; XCTAssertNotNil([SentrySDK.currentHub getClient]); [SentrySDK.currentHub bindClient:nil]; } - (void)testSDKBreadCrumbAdd { - [SentrySDK startWithOptions:@{ @"dsn" : @"https://username:password@app.getsentry.com/12345" }]; + [SentrySDK startWithConfigureOptions:^(SentryOptions *_Nonnull options) { + options.dsn = @"https://username:password@app.getsentry.com/12345"; + }]; SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentryLevelInfo category:@"testCategory"]; @@ -79,7 +83,9 @@ - (void)testSDKBreadCrumbAdd - (void)testSDKCaptureEvent { - [SentrySDK startWithOptions:@{ @"dsn" : @"https://username:password@app.getsentry.com/12345" }]; + [SentrySDK startWithConfigureOptions:^(SentryOptions *_Nonnull options) { + options.dsn = @"https://username:password@app.getsentry.com/12345"; + }]; SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentryLevelFatal]; @@ -91,7 +97,9 @@ - (void)testSDKCaptureEvent - (void)testSDKCaptureError { - [SentrySDK startWithOptions:@{ @"dsn" : @"https://username:password@app.getsentry.com/12345" }]; + [SentrySDK startWithConfigureOptions:^(SentryOptions *_Nonnull options) { + options.dsn = @"https://username:password@app.getsentry.com/12345"; + }]; NSError *error = [NSError errorWithDomain:@"testworld" diff --git a/Tests/SentryTests/TestUtils/SentryTestObserver.m b/Tests/SentryTests/TestUtils/SentryTestObserver.m index c9fd968b437..c34b08da32d 100644 --- a/Tests/SentryTests/TestUtils/SentryTestObserver.m +++ b/Tests/SentryTests/TestUtils/SentryTestObserver.m @@ -48,7 +48,7 @@ - (instancetype)init // The SentryCrashIntegration enriches the scope. We need to install the integration // once to get the scope data. - [SentrySDK startWithOptionsObject:options]; + [SentrySDK startWithOptions:options]; self.scope = [[SentryScope alloc] init]; [SentryCrashIntegration enrichScope:self.scope