diff --git a/Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj b/Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj index 1e717cffe92..57bdcd2b2d7 100644 --- a/Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj +++ b/Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ 7BFC8B0626D4D24B000D3504 /* LoremIpsum.txt in Resources */ = {isa = PBXBuildFile; fileRef = 7BFC8B0526D4D24B000D3504 /* LoremIpsum.txt */; }; 844DA821282584C300E6B62E /* CoreDataViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F57BC427BBD787000D09D4 /* CoreDataViewController.swift */; }; 844DA822282584F700E6B62E /* SentryData.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = D845F35927BAD4CC00A4D7A2 /* SentryData.xcdatamodeld */; }; + 84732B28292F0BE600790372 /* SentryBaseUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84732B27292F0BE600790372 /* SentryBaseUnitTest.swift */; }; 848A256B286E3351008A8858 /* Sentry.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 630853322440C44F00DDE4CE /* Sentry.framework */; }; 848A256D286E3351008A8858 /* fatal-error-binary-images-message2.json in Resources */ = {isa = PBXBuildFile; fileRef = D83A30DF279F1F5C00372D0A /* fatal-error-binary-images-message2.json */; }; 848A256F286E3351008A8858 /* Sentry.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 630853322440C44F00DDE4CE /* Sentry.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; @@ -250,6 +251,7 @@ 7B64386C26A6C544000D0F65 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 7B79000329028C7300A7F467 /* MetricKitManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricKitManager.swift; sourceTree = ""; }; 7BFC8B0526D4D24B000D3504 /* LoremIpsum.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = LoremIpsum.txt; sourceTree = ""; }; + 84732B27292F0BE600790372 /* SentryBaseUnitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBaseUnitTest.swift; sourceTree = ""; }; 848A2573286E3351008A8858 /* PerformanceBenchmarks.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PerformanceBenchmarks.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 848A2578286E3490008A8858 /* PerformanceBenchmarks-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "PerformanceBenchmarks-Info.plist"; sourceTree = ""; }; 84B527B728DD24BA00475E8D /* SentryDeviceTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = SentryDeviceTests.mm; path = ../../../Tests/SentryTests/Helper/SentryDeviceTests.mm; sourceTree = ""; }; @@ -426,6 +428,7 @@ D83A30DF279F1F5C00372D0A /* fatal-error-binary-images-message2.json */, 7B64386A26A6C544000D0F65 /* LaunchUITests.swift */, D83A30E5279FE21F00372D0A /* SentryFileIOTrackingIntegrationTests.swift */, + 84732B27292F0BE600790372 /* SentryBaseUnitTest.swift */, 84B527B728DD24BA00475E8D /* SentryDeviceTests.mm */, 84B527BB28DD25E400475E8D /* SentryDevice.h */, 84B527BC28DD25E400475E8D /* SentryDevice.mm */, @@ -834,6 +837,7 @@ 84B527B928DD24BA00475E8D /* SentryDeviceTests.mm in Sources */, D83A30E6279FE21F00372D0A /* SentryFileIOTrackingIntegrationTests.swift in Sources */, 7B64386B26A6C544000D0F65 /* LaunchUITests.swift in Sources */, + 84732B28292F0BE600790372 /* SentryBaseUnitTest.swift in Sources */, 84B527BD28DD25E400475E8D /* SentryDevice.mm in Sources */, D83A30C8279EFD6E00372D0A /* ClearTestState.swift in Sources */, ); diff --git a/Samples/iOS-Swift/iOS-SwiftUITests/SentryBaseUnitTest.swift b/Samples/iOS-Swift/iOS-SwiftUITests/SentryBaseUnitTest.swift new file mode 100644 index 00000000000..070e3cdd39d --- /dev/null +++ b/Samples/iOS-Swift/iOS-SwiftUITests/SentryBaseUnitTest.swift @@ -0,0 +1,8 @@ +import XCTest + +class SentryBaseUnitTest: XCTestCase { + override func tearDown() { + clearTestState() + super.tearDown() + } +} diff --git a/Sentry.xcodeproj/project.pbxproj b/Sentry.xcodeproj/project.pbxproj index 89528c08b44..471339ead1c 100644 --- a/Sentry.xcodeproj/project.pbxproj +++ b/Sentry.xcodeproj/project.pbxproj @@ -513,7 +513,6 @@ 7BCFBD6D2681D0A900BC27D8 /* SentryCrashScopeObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BCFBD6C2681D0A900BC27D8 /* SentryCrashScopeObserver.h */; }; 7BCFBD6F2681D0EE00BC27D8 /* SentryCrashScopeObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BCFBD6E2681D0EE00BC27D8 /* SentryCrashScopeObserver.m */; }; 7BD337E424A356180050DB6E /* SentryCrashIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD337E324A356180050DB6E /* SentryCrashIntegrationTests.swift */; }; - 7BD47B4E268F0B470076A663 /* ClearTestState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD47B4C268F0B080076A663 /* ClearTestState.swift */; }; 7BD4BD4127EB0F0D0071F4FF /* SentryDiscardReason.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BD4BD4027EB0F0C0071F4FF /* SentryDiscardReason.h */; }; 7BD4BD4327EB29BA0071F4FF /* SentryClientReport.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BD4BD4227EB29BA0071F4FF /* SentryClientReport.h */; }; 7BD4BD4527EB29F50071F4FF /* SentryClientReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BD4BD4427EB29F50071F4FF /* SentryClientReport.m */; }; @@ -602,6 +601,7 @@ 8419C0C428C1889D001C8259 /* SentryProfilerSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8419C0C328C1889D001C8259 /* SentryProfilerSwiftTests.swift */; }; 8453421228BE855D00C22EEC /* SentrySampleDecision.m in Sources */ = {isa = PBXBuildFile; fileRef = 8453421128BE855D00C22EEC /* SentrySampleDecision.m */; }; 8453421628BE8A9500C22EEC /* SentrySpanStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 8453421528BE8A9500C22EEC /* SentrySpanStatus.m */; }; + 84732A71292C3CF800790372 /* SentryBaseUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84732A70292C3CF800790372 /* SentryBaseUnitTest.swift */; }; 84A888FD28D9B11700C51DFD /* SentryProfiler+Test.h in Headers */ = {isa = PBXBuildFile; fileRef = 84A888FC28D9B11700C51DFD /* SentryProfiler+Test.h */; }; 84A8891C28DBD28900C51DFD /* SentryDevice.h in Headers */ = {isa = PBXBuildFile; fileRef = 84A8891A28DBD28900C51DFD /* SentryDevice.h */; }; 84A8891D28DBD28900C51DFD /* SentryDevice.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84A8891B28DBD28900C51DFD /* SentryDevice.mm */; }; @@ -1290,7 +1290,6 @@ 7BCFBD6C2681D0A900BC27D8 /* SentryCrashScopeObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryCrashScopeObserver.h; path = include/SentryCrashScopeObserver.h; sourceTree = ""; }; 7BCFBD6E2681D0EE00BC27D8 /* SentryCrashScopeObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryCrashScopeObserver.m; sourceTree = ""; }; 7BD337E324A356180050DB6E /* SentryCrashIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashIntegrationTests.swift; sourceTree = ""; }; - 7BD47B4C268F0B080076A663 /* ClearTestState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClearTestState.swift; sourceTree = ""; }; 7BD4BD4027EB0F0C0071F4FF /* SentryDiscardReason.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryDiscardReason.h; path = include/SentryDiscardReason.h; sourceTree = ""; }; 7BD4BD4227EB29BA0071F4FF /* SentryClientReport.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryClientReport.h; path = include/SentryClientReport.h; sourceTree = ""; }; 7BD4BD4427EB29F50071F4FF /* SentryClientReport.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryClientReport.m; sourceTree = ""; }; @@ -1413,6 +1412,7 @@ 844DA81F28246DE300E6B62E /* scripts */ = {isa = PBXFileReference; lastKnownFileType = folder; path = scripts; sourceTree = ""; }; 8453421128BE855D00C22EEC /* SentrySampleDecision.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentrySampleDecision.m; sourceTree = ""; }; 8453421528BE8A9500C22EEC /* SentrySpanStatus.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentrySpanStatus.m; sourceTree = ""; }; + 84732A70292C3CF800790372 /* SentryBaseUnitTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SentryBaseUnitTest.swift; sourceTree = ""; }; 84A888FC28D9B11700C51DFD /* SentryProfiler+Test.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "SentryProfiler+Test.h"; path = "Sources/Sentry/include/SentryProfiler+Test.h"; sourceTree = SOURCE_ROOT; }; 84A8891A28DBD28900C51DFD /* SentryDevice.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryDevice.h; path = include/SentryDevice.h; sourceTree = ""; }; 84A8891B28DBD28900C51DFD /* SentryDevice.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryDevice.mm; sourceTree = ""; }; @@ -1962,7 +1962,6 @@ 639889D21EDF06C100EA7442 /* SentryTests-Bridging-Header.h */, 63AA75951EB8AEDB00D153DE /* SentryTests.m */, 7B9421C4260CA393001F9349 /* SentrySDK+Tests.h */, - 7BD47B4C268F0B080076A663 /* ClearTestState.swift */, 7BA8409F24A1EC6E00B718AA /* SentrySDKTests.swift */, 630436151EC0AD3100C4D3FA /* SentryNSDataCompressionTests.m */, 630C01931EC3402C00C52CEF /* SentryKSCrashReportConverterTests.m */, @@ -1981,6 +1980,7 @@ 7B0002312477F0520035FEF1 /* SentrySessionTests.m */, 7B0002332477F52D0035FEF1 /* SentrySessionTests.swift */, 7B944FAF2469B46000A10721 /* TestClient.swift */, + 84732A70292C3CF800790372 /* SentryBaseUnitTest.swift */, 7B0A54552523178700A71716 /* SentryScopeSwiftTests.swift */, 8E70B10025CB8695002B3155 /* SentrySpanIdTests.swift */, 7B4260332630315C00B36EDD /* SampleError.swift */, @@ -3657,6 +3657,7 @@ 63FE722220DA66EC00CDBAE8 /* SentryCrashJSONCodec_Tests.m in Sources */, 7B0A5452252311CE00A71716 /* SentryBreadcrumbTests.swift in Sources */, 7BA0C04A280563AA003E0326 /* TestTransportAdapter.swift in Sources */, + 84732A71292C3CF800790372 /* SentryBaseUnitTest.swift in Sources */, 7BE3C7752445C82300A38442 /* SentryCurrentDateTests.swift in Sources */, 7B3398672459C4AE00BD9C96 /* SentryEnvelopeRateLimitTests.swift in Sources */, 8EA9AF492665AC48002771B4 /* SentryPerformanceTrackerTests.swift in Sources */, @@ -3756,7 +3757,6 @@ 7BC6EC18255C44540059822A /* SentryDebugMetaTests.swift in Sources */, A811D867248E2770008A41EA /* SentrySystemEventBreadcrumbsTest.swift in Sources */, 7B82D54924E2A2D400EE670F /* SentryIdTests.swift in Sources */, - 7BD47B4E268F0B470076A663 /* ClearTestState.swift in Sources */, 7B6D98ED24C703F8005502FA /* Async.swift in Sources */, 7BA0C04C28056556003E0326 /* SentryTransportAdapterTests.swift in Sources */, 7BE0DC29272A9E1C004FA8B7 /* SentryBreadcrumbTrackerTests.swift in Sources */, diff --git a/Sources/Sentry/SentryTracer.m b/Sources/Sentry/SentryTracer.m index cea5432cf56..09da39445c3 100644 --- a/Sources/Sentry/SentryTracer.m +++ b/Sources/Sentry/SentryTracer.m @@ -3,7 +3,6 @@ #import "PrivateSentrySDKOnly.h" #import "SentryAppStartMeasurement.h" #import "SentryClient.h" -#import "SentryCurrentDate.h" #import "SentryFramesTracker.h" #import "SentryHub+Private.h" #import "SentryLog.h" diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c b/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c index 500b7300d29..c70e6edd9a7 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c @@ -394,6 +394,10 @@ sentrycrashfu_removeFile(const char *path, bool mustExist) bool sentrycrashfu_deleteContentsOfPath(const char *path) { + if (path == NULL) { + return false; + } + if (!canDeletePath(path)) { return false; } diff --git a/Tests/SentryTests/ClearTestState.swift b/Tests/SentryTests/ClearTestState.swift deleted file mode 100644 index ac2f1958b51..00000000000 --- a/Tests/SentryTests/ClearTestState.swift +++ /dev/null @@ -1,30 +0,0 @@ -import Foundation -import Sentry - -func clearTestState() { - SentrySDK.close() - SentrySDK.setCurrentHub(nil) - SentrySDK.crashedLastRunCalled = false - SentrySDK.startInvocations = 0 - - PrivateSentrySDKOnly.onAppStartMeasurementAvailable = nil - PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = false - SentrySDK.setAppStartMeasurement(nil) - CurrentDate.setCurrentDateProvider(nil) - SentryNetworkTracker.sharedInstance.disable() - - #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) - let framesTracker = SentryFramesTracker.sharedInstance() - framesTracker.stop() - framesTracker.resetFrames() - - setenv("ActivePrewarm", "0", 1) - SentryAppStartTracker.load() - #endif - - SentryDependencyContainer.reset() - Dynamic(SentryGlobalEventProcessor.shared()).removeAllProcessors() - SentrySwizzleWrapper.sharedInstance.removeAllCallbacks() - - SentryNSDataTracker.sharedInstance.disable() -} diff --git a/Tests/SentryTests/Helper/SentrySwizzleWrapperTests.swift b/Tests/SentryTests/Helper/SentrySwizzleWrapperTests.swift index b6f946987d2..2a0524a78ea 100644 --- a/Tests/SentryTests/Helper/SentrySwizzleWrapperTests.swift +++ b/Tests/SentryTests/Helper/SentrySwizzleWrapperTests.swift @@ -12,7 +12,7 @@ extension SentrySwizzleWrapper { } -class SentrySwizzleWrapperTests: XCTestCase { +class SentrySwizzleWrapperTests: SentryBaseUnitTest { #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) @@ -31,16 +31,10 @@ class SentrySwizzleWrapperTests: XCTestCase { override func setUp() { super.setUp() - fixture = Fixture() sut = SentrySwizzleWrapper.sharedInstance } - - override func tearDown() { - super.tearDown() - clearTestState() - } - + func testSendAction_RegisterCallbacks_CallbacksCalled() { let firstExpectation = expectation(description: "first") sut.swizzleSendAction({ actualAction, _, _, actualEvent in diff --git a/Tests/SentryTests/Integrations/ANR/SentryANRTrackingIntegrationTests.swift b/Tests/SentryTests/Integrations/ANR/SentryANRTrackingIntegrationTests.swift index 15d81d40ba5..4b0c46e8de4 100644 --- a/Tests/SentryTests/Integrations/ANR/SentryANRTrackingIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/ANR/SentryANRTrackingIntegrationTests.swift @@ -28,7 +28,6 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase { override func tearDown() { sut.uninstall() - clearTestState() super.tearDown() } diff --git a/Tests/SentryTests/Integrations/Breadcrumbs/SentryAutoBreadcrumbTrackingIntegrationTests.swift b/Tests/SentryTests/Integrations/Breadcrumbs/SentryAutoBreadcrumbTrackingIntegrationTests.swift index 764569b60b2..ecc043f7c62 100644 --- a/Tests/SentryTests/Integrations/Breadcrumbs/SentryAutoBreadcrumbTrackingIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/Breadcrumbs/SentryAutoBreadcrumbTrackingIntegrationTests.swift @@ -1,7 +1,7 @@ import Sentry import XCTest -class SentryAutoBreadcrumbTrackingIntegrationTests: XCTestCase { +class SentryAutoBreadcrumbTrackingIntegrationTests: SentryBaseUnitTest { private class Fixture { let tracker = SentryTestBreadcrumbTracker(swizzleWrapper: SentrySwizzleWrapper.sharedInstance) @@ -18,11 +18,6 @@ class SentryAutoBreadcrumbTrackingIntegrationTests: XCTestCase { fixture = Fixture() } - override func tearDown() { - super.tearDown() - clearTestState() - } - func testInstallWithSwizzleEnabled_StartSwizzleCalled() { let sut = fixture.sut diff --git a/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift b/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift index e1d05d3cb07..fc80eb77f05 100644 --- a/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Breadcrumbs/SentryBreadcrumbTrackerTests.swift @@ -1,6 +1,6 @@ import XCTest -class SentryBreadcrumbTrackerTests: XCTestCase { +class SentryBreadcrumbTrackerTests: SentryBaseUnitTest { private var scope: Scope! @@ -12,12 +12,7 @@ class SentryBreadcrumbTrackerTests: XCTestCase { let hub = TestHub(client: client, andScope: scope) SentrySDK.setCurrentHub(hub) } - - override func tearDown() { - super.tearDown() - clearTestState() - } - + #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) func testStopRemovesSwizzleSendAction() { diff --git a/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift b/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift index 553c6bd5beb..58f3f05a7bd 100644 --- a/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift +++ b/Tests/SentryTests/Integrations/Breadcrumbs/SentrySystemEventBreadcrumbsTest.swift @@ -1,7 +1,7 @@ @testable import Sentry import XCTest -class SentrySystemEventBreadcrumbsTest: XCTestCase { +class SentrySystemEventBreadcrumbsTest: SentryBaseUnitTest { // This feature only works on iOS #if os(iOS) @@ -59,8 +59,6 @@ class SentrySystemEventBreadcrumbsTest: XCTestCase { override func tearDown() { super.tearDown() - clearTestState() - fixture.fileManager.deleteTimezoneOffset() sut.stop() } diff --git a/Tests/SentryTests/Integrations/NotificationCenterTestCase.swift b/Tests/SentryTests/Integrations/NotificationCenterTestCase.swift index 0c23d29d0ff..4189f0f9264 100644 --- a/Tests/SentryTests/Integrations/NotificationCenterTestCase.swift +++ b/Tests/SentryTests/Integrations/NotificationCenterTestCase.swift @@ -5,7 +5,7 @@ import XCTest import UIKit #endif -class NotificationCenterTestCase: XCTestCase { +class NotificationCenterTestCase: SentryBaseUnitTest { #if os(tvOS) || os(iOS) let willEnterForegroundNotification = UIApplication.willEnterForegroundNotification diff --git a/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryIntegrationTests.swift b/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryIntegrationTests.swift index f994edf9d87..69dd24c736b 100644 --- a/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryIntegrationTests.swift @@ -1,6 +1,6 @@ import XCTest -class SentryOutOfMemoryIntegrationTests: XCTestCase { +class SentryOutOfMemoryIntegrationTests: SentryBaseUnitTest { private class Fixture { let options: Options @@ -37,8 +37,6 @@ class SentryOutOfMemoryIntegrationTests: XCTestCase { override func tearDown() { sut?.uninstall() - fixture.fileManager.deleteAllFolders() - clearTestState() super.tearDown() } diff --git a/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryTrackerTests.swift b/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryTrackerTests.swift index 05ea929fdf3..a487609711b 100644 --- a/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryTrackerTests.swift +++ b/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryTrackerTests.swift @@ -56,9 +56,6 @@ class SentryOutOfMemoryTrackerTests: NotificationCenterTestCase { override func tearDown() { super.tearDown() sut.stop() - fixture.client.fileManager.deleteAllFolders() - - clearTestState() } func testStart_StoresAppState() { diff --git a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift index 8953c454fd9..67422e29471 100644 --- a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift @@ -57,7 +57,6 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { super.tearDown() sut.stop() fixture.fileManager.deleteAllFolders() - clearTestState() } func testFirstStart_IsColdStart() { diff --git a/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift b/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift index 6687543876b..cbfc50d90b4 100644 --- a/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift +++ b/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift @@ -1,7 +1,7 @@ import CoreData import XCTest -class SentryCoreDataTrackerTests: XCTestCase { +class SentryCoreDataTrackerTests: SentryBaseUnitTest { private class Fixture { let context = TestNSManagedObjectContext() @@ -30,11 +30,6 @@ class SentryCoreDataTrackerTests: XCTestCase { fixture = Fixture() } - override func tearDown() { - super.tearDown() - clearTestState() - } - func testConstants() { //Test constants to make sure we don't accidentally change it XCTAssertEqual(SENTRY_COREDATA_FETCH_OPERATION, "db.sql.query") diff --git a/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackingIntegrationTest.swift b/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackingIntegrationTest.swift index 4d09db79657..f1400b6aeb6 100644 --- a/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackingIntegrationTest.swift +++ b/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackingIntegrationTest.swift @@ -1,7 +1,7 @@ import CoreData import XCTest -class SentryCoreDataTrackingIntegrationTests: XCTestCase { +class SentryCoreDataTrackingIntegrationTests: SentryBaseUnitTest { private class Fixture { let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) @@ -29,7 +29,6 @@ class SentryCoreDataTrackingIntegrationTests: XCTestCase { override func tearDown() { super.tearDown() fixture.coreDataStack.reset() - clearTestState() } func test_InstallAndUninstall() { diff --git a/Tests/SentryTests/Integrations/Performance/IO/SentryFileIOTrackingIntegrationTests.swift b/Tests/SentryTests/Integrations/Performance/IO/SentryFileIOTrackingIntegrationTests.swift index f19ca2e3a7f..6ea1c3901e3 100644 --- a/Tests/SentryTests/Integrations/Performance/IO/SentryFileIOTrackingIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/Performance/IO/SentryFileIOTrackingIntegrationTests.swift @@ -6,7 +6,7 @@ import XCTest // GitHub Actions doesn't have simulators for iOS 11 and 10. // That's why we need to keep it generic, without access // to any private part of the SDK. -class SentryFileIOTrackingIntegrationTests: XCTestCase { +class SentryFileIOTrackingIntegrationTests: SentryBaseUnitTest { private class Fixture { let data = "SOME DATA".data(using: .utf8) ?? Data() @@ -52,7 +52,6 @@ class SentryFileIOTrackingIntegrationTests: XCTestCase { if deleteFileDirectory { try? FileManager.default.removeItem(at: fixture.fileDirectory) } - clearTestState() } func test_WritingTrackingDisabled_forIOOption() { diff --git a/Tests/SentryTests/Integrations/Performance/IO/SentryNSDataTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/IO/SentryNSDataTrackerTests.swift index 50dbe225bdc..93df257d3d1 100644 --- a/Tests/SentryTests/Integrations/Performance/IO/SentryNSDataTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/IO/SentryNSDataTrackerTests.swift @@ -1,6 +1,6 @@ import XCTest -class SentryNSDataTrackerTests: XCTestCase { +class SentryNSDataTrackerTests: SentryBaseUnitTest { private class Fixture { @@ -26,11 +26,6 @@ class SentryNSDataTrackerTests: XCTestCase { SentrySDK.start { $0.enableFileIOTracking = true } } - override func tearDown() { - super.tearDown() - clearTestState() - } - func testConstants() { //A test to ensure this constants don't accidentally change XCTAssertEqual("file.read", SENTRY_FILE_READ_OPERATION) diff --git a/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerIntegrationTests.swift b/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerIntegrationTests.swift index 0bf60cf34a4..fbb99d81bbc 100644 --- a/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerIntegrationTests.swift @@ -2,7 +2,7 @@ import Sentry import SwiftUI import XCTest -class SentryNetworkTrackerIntegrationTests: XCTestCase { +class SentryNetworkTrackerIntegrationTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentryNetworkTrackerIntegrationTests") private static let testBaggageURL = URL(string: "http://localhost:8080/echo-baggage-header")! @@ -12,7 +12,7 @@ class SentryNetworkTrackerIntegrationTests: XCTestCase { private static let transactionOperation = "Test" private class Fixture { - let dateProvider = TestCurrentDateProvider() + let dateProvider = CurrentDate.getProvider()! as! TestCurrentDateProvider let options: Options init() { @@ -28,11 +28,6 @@ class SentryNetworkTrackerIntegrationTests: XCTestCase { super.setUp() fixture = Fixture() } - - override func tearDown() { - super.tearDown() - clearTestState() - } func testNSURLSessionConfiguration_NoActiveSpan_NoHeadersAdded() { startSDK() diff --git a/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift index 6f05da0988c..f56cb0d8186 100644 --- a/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/Network/SentryNetworkTrackerTests.swift @@ -1,7 +1,7 @@ import ObjectiveC import XCTest -class SentryNetworkTrackerTests: XCTestCase { +class SentryNetworkTrackerTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentrySessionTrackerTests") private static let testURL = URL(string: "https://www.domain.com/api")! @@ -11,7 +11,7 @@ class SentryNetworkTrackerTests: XCTestCase { private class Fixture { static let url = "" let sentryTask: URLSessionDataTaskMock - let dateProvider = TestCurrentDateProvider() + let dateProvider = CurrentDate.getProvider()! as! TestCurrentDateProvider let options: Options let scope: Scope let nsUrlRequest = NSURLRequest(url: SentryNetworkTrackerTests.testURL) @@ -41,14 +41,7 @@ class SentryNetworkTrackerTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - SentrySDK.setCurrentHub(fixture.hub) - CurrentDate.setCurrentDateProvider(fixture.dateProvider) - } - - override func tearDown() { - super.tearDown() - clearTestState() } func testCaptureCompletion() { diff --git a/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift index 4463ee52e12..73b03bf977e 100644 --- a/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift @@ -1,6 +1,6 @@ import XCTest -class SentryPerformanceTrackerTests: XCTestCase { +class SentryPerformanceTrackerTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentryPerformanceTrackerTests") @@ -31,12 +31,7 @@ class SentryPerformanceTrackerTests: XCTestCase { fixture = Fixture() SentrySDK.setCurrentHub(fixture.hub) } - - override func tearDown() { - super.tearDown() - clearTestState() - } - + func testSingleton() { XCTAssertEqual(SentryPerformanceTracker.shared(), SentryPerformanceTracker.shared()) } diff --git a/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerPerformanceTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerPerformanceTrackerTests.swift index ada26ba8723..6a2d53cb6ae 100644 --- a/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerPerformanceTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerPerformanceTrackerTests.swift @@ -6,7 +6,7 @@ import XCTest class TestViewController: UIViewController { } -class SentryUIViewControllerPerformanceTrackerTests: XCTestCase { +class SentryUIViewControllerPerformanceTrackerTests: SentryBaseUnitTest { let loadView = "loadView" let viewWillLoad = "viewWillLoad" @@ -58,11 +58,6 @@ class SentryUIViewControllerPerformanceTrackerTests: XCTestCase { fixture = Fixture() SentrySDK.start(options: fixture.options) } - - override func tearDown() { - super.tearDown() - clearTestState() - } func testUILifeCycle_ViewDidAppear() throws { try assertUILifeCycle(finishStatus: SentrySpanStatus.ok) { sut, viewController, tracker, callbackExpectation, transactionSpan in diff --git a/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerSwizzlingTests.swift b/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerSwizzlingTests.swift index 5557569a1eb..dfcb3b80f9d 100644 --- a/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerSwizzlingTests.swift +++ b/Tests/SentryTests/Integrations/Performance/UIViewController/SentryUIViewControllerSwizzlingTests.swift @@ -2,7 +2,7 @@ import Sentry import XCTest #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) -class SentryUIViewControllerSwizzlingTests: XCTestCase { +class SentryUIViewControllerSwizzlingTests: SentryBaseUnitTest { private class Fixture { let dispatchQueue = TestSentryDispatchQueueWrapper() @@ -48,11 +48,6 @@ class SentryUIViewControllerSwizzlingTests: XCTestCase { fixture = Fixture() SentrySDK.start(options: fixture.options) } - - override func tearDown() { - super.tearDown() - clearTestState() - } func testShouldSwizzle_TestViewController() { let result = fixture.sut.shouldSwizzleViewController(TestViewController.self) diff --git a/Tests/SentryTests/Integrations/Screenshot/SentryScreenshotIntegrationTests.swift b/Tests/SentryTests/Integrations/Screenshot/SentryScreenshotIntegrationTests.swift index 508c31ae7a7..55faf180bac 100644 --- a/Tests/SentryTests/Integrations/Screenshot/SentryScreenshotIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/Screenshot/SentryScreenshotIntegrationTests.swift @@ -2,7 +2,7 @@ import Sentry import XCTest #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) -class SentryScreenshotIntegrationTests: XCTestCase { +class SentryScreenshotIntegrationTests: SentryBaseUnitTest { private class Fixture { let screenshot: TestSentryScreenshot @@ -24,14 +24,8 @@ class SentryScreenshotIntegrationTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - SentryDependencyContainer.sharedInstance().screenshot = fixture.screenshot } - - override func tearDown() { - super.tearDown() - clearTestState() - } func test_attachScreenshot_disabled() { SentrySDK.start { $0.attachScreenshot = false } diff --git a/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift b/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift index dbb5bd2c850..1ed030a475b 100644 --- a/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift @@ -49,24 +49,6 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase { private lazy var fixture = Fixture() - override func setUp() { - super.setUp() - CurrentDate.setCurrentDateProvider(TestCurrentDateProvider()) - - fixture.client.fileManager.deleteCurrentSession() - fixture.client.fileManager.deleteCrashedSession() - fixture.client.fileManager.deleteAppState() - } - - override func tearDown() { - super.tearDown() - fixture.client.fileManager.deleteCurrentSession() - fixture.client.fileManager.deleteCrashedSession() - fixture.client.fileManager.deleteAppState() - - clearTestState() - } - // Test for GH-581 func testReleaseNamePassedToSentryCrash() { let releaseName = "1.0.0" diff --git a/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift b/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift index 7bd5d9fe1cd..aace644476a 100644 --- a/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift +++ b/Tests/SentryTests/Integrations/Session/SentrySessionGeneratorTests.swift @@ -36,22 +36,10 @@ class SentrySessionGeneratorTests: NotificationCenterTestCase { options.integrations = Options.defaultIntegrations().filter { (name) -> Bool in return name != "SentryAutoSessionTrackingIntegration" } - - fileManager = try! SentryFileManager(options: options, andCurrentDateProvider: DefaultCurrentDateProvider.sharedInstance()) - - fileManager.deleteCurrentSession() - fileManager.deleteCrashedSession() - fileManager.deleteTimestampLastInForeground() - fileManager.deleteAppState() } override func tearDown() { super.tearDown() - - fileManager.deleteCurrentSession() - fileManager.deleteCrashedSession() - fileManager.deleteTimestampLastInForeground() - fileManager.deleteAppState() autoSessionTrackingIntegration.stop() } diff --git a/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift b/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift index ad4060b447a..5dcfd3999d4 100644 --- a/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift @@ -1,7 +1,7 @@ @testable import Sentry import XCTest -class SentrySessionTrackerTests: XCTestCase { +class SentrySessionTrackerTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentrySessionTrackerTests") private static let dsn = TestConstants.dsn(username: "SentrySessionTrackerTests") @@ -9,8 +9,8 @@ class SentrySessionTrackerTests: XCTestCase { private class Fixture { let options: Options - let currentDateProvider = TestCurrentDateProvider() - let client: TestClient! + let currentDateProvider = CurrentDate.getProvider()! as! TestCurrentDateProvider + lazy var client = TestClient(options: options, fileManager: fileManager)! let sentryCrash: TestSentryCrashWrapper let notificationCenter = TestNSNotificationCenterWrapper() @@ -24,8 +24,6 @@ class SentrySessionTrackerTests: XCTestCase { options.sessionTrackingIntervalMillis = 10_000 options.environment = "debug" - client = TestClient(options: options) - sentryCrash = TestSentryCrashWrapper.sharedInstance() } @@ -44,26 +42,13 @@ class SentrySessionTrackerTests: XCTestCase { override func setUp() { super.setUp() - - clearTestState() - fixture = Fixture() - - CurrentDate.setCurrentDateProvider(fixture.currentDateProvider) - - fixture.fileManager.deleteCurrentSession() - fixture.fileManager.deleteCrashedSession() - fixture.fileManager.deleteTimestampLastInForeground() - fixture.setNewHubToSDK() - sut = fixture.getSut() } override func tearDown() { sut.stop() - clearTestState() - super.tearDown() } diff --git a/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackerTests.swift b/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackerTests.swift index f1a6be0f691..c8f5c3711ce 100644 --- a/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackerTests.swift +++ b/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackerTests.swift @@ -2,7 +2,7 @@ import Sentry import XCTest #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) -class SentryUIEventTrackerTests: XCTestCase { +class SentryUIEventTrackerTests: SentryBaseUnitTest { private class Fixture { let swizzleWrapper = TestSentrySwizzleWrapper() @@ -41,7 +41,6 @@ class SentryUIEventTrackerTests: XCTestCase { override func tearDown() { super.tearDown() fixture.swizzleWrapper.removeAllCallbacks() - clearTestState() } func test_NSSender_NoTransaction() { diff --git a/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackingIntegrationTests.swift b/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackingIntegrationTests.swift index 65966f78a4f..f5a547e0882 100644 --- a/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackingIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/UIEvents/SentryUIEventTrackingIntegrationTests.swift @@ -2,7 +2,7 @@ import Sentry import XCTest #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) -class SentryUIEventTrackerIntegrationTests: XCTestCase { +class SentryUIEventTrackerIntegrationTests: SentryBaseUnitTest { private class Fixture { @@ -27,11 +27,6 @@ class SentryUIEventTrackerIntegrationTests: XCTestCase { fixture = Fixture() } - override func tearDown() { - super.tearDown() - clearTestState() - } - func test_noInstallation_SwizzlingDisabled() { let sut = fixture.getSut() sut.install(with: fixture.optionForUIEventTracking(enableSwizzling: false)) diff --git a/Tests/SentryTests/Integrations/ViewHierarchy/SentryViewHierarchyIntegrationTests.swift b/Tests/SentryTests/Integrations/ViewHierarchy/SentryViewHierarchyIntegrationTests.swift index fb8c18e65e1..0fa22c39b48 100644 --- a/Tests/SentryTests/Integrations/ViewHierarchy/SentryViewHierarchyIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/ViewHierarchy/SentryViewHierarchyIntegrationTests.swift @@ -2,7 +2,7 @@ import Sentry import XCTest #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) -class SentryViewHierarchyIntegrationTests: XCTestCase { +class SentryViewHierarchyIntegrationTests: SentryBaseUnitTest { private class Fixture { let viewHierarchy: TestSentryViewHierarchy @@ -28,11 +28,6 @@ class SentryViewHierarchyIntegrationTests: XCTestCase { SentryDependencyContainer.sharedInstance().viewHierarchy = fixture.viewHierarchy } - override func tearDown() { - super.tearDown() - clearTestState() - } - func test_attachViewHierarchy_disabled() { SentrySDK.start { $0.attachViewHierarchy = false } XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 0) diff --git a/Tests/SentryTests/Networking/SentryHttpTransportTests.swift b/Tests/SentryTests/Networking/SentryHttpTransportTests.swift index 40a8a6090eb..d449f89e693 100644 --- a/Tests/SentryTests/Networking/SentryHttpTransportTests.swift +++ b/Tests/SentryTests/Networking/SentryHttpTransportTests.swift @@ -1,7 +1,7 @@ import Sentry import XCTest -class SentryHttpTransportTests: XCTestCase { +class SentryHttpTransportTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentryHttpTransportTests") private static let dsn = TestConstants.dsn(username: "SentryHttpTransportTests") @@ -118,15 +118,12 @@ class SentryHttpTransportTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - fixture.fileManager.deleteAllEnvelopes() fixture.requestManager.returnResponse(response: HTTPURLResponse()) - sut = fixture.sut } override func tearDown() { super.tearDown() - fixture.fileManager.deleteAllEnvelopes() fixture.requestManager.waitForAllRequests() } diff --git a/Tests/SentryTests/Performance/SentryTracerTests.swift b/Tests/SentryTests/Performance/SentryTracerTests.swift index 8ae73c4683c..89e7ec80add 100644 --- a/Tests/SentryTests/Performance/SentryTracerTests.swift +++ b/Tests/SentryTests/Performance/SentryTracerTests.swift @@ -1,6 +1,6 @@ import XCTest -class SentryTracerTests: XCTestCase { +class SentryTracerTests: SentryBaseUnitTest { private class TracerDelegate: SentryTracerDelegate { @@ -90,17 +90,6 @@ class SentryTracerTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - SentryTracer.resetAppStartMeasurementRead() - } - - override func tearDown() { - super.tearDown() - clearTestState() - SentryTracer.resetAppStartMeasurementRead() -#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) - SentryFramesTracker.sharedInstance().resetFrames() - SentryFramesTracker.sharedInstance().stop() -#endif } func testFinish_WithChildren_WaitsForAllChildren() { diff --git a/Tests/SentryTests/PrivateSentrySDKOnlyTests.swift b/Tests/SentryTests/PrivateSentrySDKOnlyTests.swift index e4e72e7be7a..7bfcb59ccae 100644 --- a/Tests/SentryTests/PrivateSentrySDKOnlyTests.swift +++ b/Tests/SentryTests/PrivateSentrySDKOnlyTests.swift @@ -1,12 +1,6 @@ import XCTest -class PrivateSentrySDKOnlyTests: XCTestCase { - - override func tearDown() { - super.tearDown() - clearTestState() - } - +class PrivateSentrySDKOnlyTests: SentryBaseUnitTest { func testStoreEnvelope() { let client = TestClient(options: Options()) SentrySDK.setCurrentHub(TestHub(client: client, andScope: nil)) diff --git a/Tests/SentryTests/Profiling/SentryProfilerSwiftTests.swift b/Tests/SentryTests/Profiling/SentryProfilerSwiftTests.swift index 98fcb74c3d2..07e334a69c4 100644 --- a/Tests/SentryTests/Profiling/SentryProfilerSwiftTests.swift +++ b/Tests/SentryTests/Profiling/SentryProfilerSwiftTests.swift @@ -2,7 +2,7 @@ import Sentry import XCTest #if os(iOS) || os(macOS) || targetEnvironment(macCatalyst) -class SentryProfilerSwiftTests: XCTestCase { +class SentryProfilerSwiftTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentryProfilerSwiftTests") private class Fixture { @@ -30,18 +30,9 @@ class SentryProfilerSwiftTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - SentryTracer.resetAppStartMeasurementRead() + SentrySDK.start(options: self.fixture.options) SentryLog.configure(true, diagnosticLevel: .debug) - } - - override func tearDown() { - super.tearDown() - clearTestState() - SentryTracer.resetAppStartMeasurementRead() -#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) - SentryFramesTracker.sharedInstance().resetFrames() - SentryFramesTracker.sharedInstance().stop() -#endif + CurrentDate.setCurrentDateProvider(nil) } func testConcurrentProfilingTransactions() { diff --git a/Tests/SentryTests/SentryBaseUnitTest.swift b/Tests/SentryTests/SentryBaseUnitTest.swift new file mode 100644 index 00000000000..2e6ca2583a9 --- /dev/null +++ b/Tests/SentryTests/SentryBaseUnitTest.swift @@ -0,0 +1,67 @@ +import Foundation +import Sentry +import XCTest + +class SentryBaseUnitTest: XCTestCase { + override func setUp() { + super.setUp() + do { + try clearTestState() + } catch { + XCTFail("Failed to clear app state at setup.") + } + } + + override func tearDown() { + do { + try clearTestState() + } catch { + XCTFail("Failed to clear app state at teardown.") + } + super.tearDown() + } +} + +private extension SentryBaseUnitTest { + func clearTestState() throws { + SentrySDK.close() + SentrySDK.setCurrentHub(nil) + SentrySDK.crashedLastRunCalled = false + SentrySDK.startInvocations = 0 + + SentryLog.configure(true, diagnosticLevel: .debug) + CurrentDate.setCurrentDateProvider(TestCurrentDateProvider()) + + SentryTracer.resetAppStartMeasurementRead() + SentrySDK.setAppStartMeasurement(nil) + PrivateSentrySDKOnly.onAppStartMeasurementAvailable = nil + PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = false + + SentryNetworkTracker.sharedInstance.disable() + + #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) + let framesTracker = SentryFramesTracker.sharedInstance() + framesTracker.stop() + framesTracker.resetFrames() + + setenv("ActivePrewarm", "0", 1) + SentryAppStartTracker.load() + #endif + + SentryDependencyContainer.reset() + Dynamic(SentryGlobalEventProcessor.shared()).removeAllProcessors() + SentrySwizzleWrapper.sharedInstance.removeAllCallbacks() + + sentrycrash_deleteAllReports() + + let dqw = TestSentryDispatchQueueWrapper() + let fileManager = try! SentryFileManager(options: Options(), andCurrentDateProvider: TestCurrentDateProvider(), dispatchQueueWrapper: dqw) + fileManager.deleteCurrentSession() + fileManager.deleteCrashedSession() + fileManager.deleteTimestampLastInForeground() + fileManager.deleteAppState() + fileManager.deleteAllEnvelopes() + fileManager.deleteTimezoneOffset() + fileManager.deleteAllFolders() + } +} diff --git a/Tests/SentryTests/SentryClientTests.swift b/Tests/SentryTests/SentryClientTests.swift index ea4641930d3..471443f6dec 100644 --- a/Tests/SentryTests/SentryClientTests.swift +++ b/Tests/SentryTests/SentryClientTests.swift @@ -4,7 +4,7 @@ import XCTest // swiftlint:disable file_length // We are aware that the client has a lot of logic and we should maybe // move some of it to other classes. -class SentryClientTest: XCTestCase { +class SentryClientTest: SentryBaseUnitTest { private static let dsn = TestConstants.dsnAsString(username: "SentryClientTest") @@ -50,7 +50,7 @@ class SentryClientTest: XCTestCase { let options = Options() options.dsn = SentryClientTest.dsn - fileManager = try! SentryFileManager(options: options, andCurrentDateProvider: TestCurrentDateProvider()) + fileManager = try! TestFileManager(options: options) transaction = Transaction(trace: trace, children: []) @@ -126,12 +126,6 @@ class SentryClientTest: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - fixture.fileManager.deleteAllEnvelopes() - } - - override func tearDown() { - super.tearDown() - clearTestState() } func testCaptureMessage() { diff --git a/Tests/SentryTests/SentryCrash/SentryCrashInstallationReporterTests.swift b/Tests/SentryTests/SentryCrash/SentryCrashInstallationReporterTests.swift index 8f970c16b1b..a9b40776e1f 100644 --- a/Tests/SentryTests/SentryCrash/SentryCrashInstallationReporterTests.swift +++ b/Tests/SentryTests/SentryCrash/SentryCrashInstallationReporterTests.swift @@ -2,7 +2,7 @@ import XCTest @available(OSX 10.10, *) -class SentryCrashInstallationReporterTests: XCTestCase { +class SentryCrashInstallationReporterTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentryCrashInstallationReporterTests") @@ -13,14 +13,6 @@ class SentryCrashInstallationReporterTests: XCTestCase { super.setUp() sut = SentryCrashInstallationReporter(inAppLogic: SentryInAppLogic(inAppIncludes: [], inAppExcludes: []), crashWrapper: TestSentryCrashWrapper.sharedInstance(), dispatchQueue: TestSentryDispatchQueueWrapper()) sut.install() - // Works only if SentryCrash is installed - sentrycrash_deleteAllReports() - } - - override func tearDown() { - super.tearDown() - sentrycrash_deleteAllReports() - clearTestState() } func testFaultyReportIsNotSentAndDeleted() throws { diff --git a/Tests/SentryTests/SentryCrash/SentryStacktraceBuilderTests.swift b/Tests/SentryTests/SentryCrash/SentryStacktraceBuilderTests.swift index c65beeb9c79..d734396eefb 100644 --- a/Tests/SentryTests/SentryCrash/SentryStacktraceBuilderTests.swift +++ b/Tests/SentryTests/SentryCrash/SentryStacktraceBuilderTests.swift @@ -1,7 +1,7 @@ @testable import Sentry import XCTest -class SentryStacktraceBuilderTests: XCTestCase { +class SentryStacktraceBuilderTests: SentryBaseUnitTest { private class Fixture { let queue = DispatchQueue(label: "SentryStacktraceBuilderTests") @@ -16,12 +16,6 @@ class SentryStacktraceBuilderTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - clearTestState() - } - - override func tearDown() { - super.tearDown() - clearTestState() } func testEnoughFrames() { diff --git a/Tests/SentryTests/SentryHubTests.swift b/Tests/SentryTests/SentryHubTests.swift index 497f88befb0..c67d8c4178f 100644 --- a/Tests/SentryTests/SentryHubTests.swift +++ b/Tests/SentryTests/SentryHubTests.swift @@ -1,7 +1,7 @@ import Sentry import XCTest -class SentryHubTests: XCTestCase { +class SentryHubTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentryHubTests") private static let dsn = TestConstants.dsn(username: "SentryHubTests") @@ -60,23 +60,8 @@ class SentryHubTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() - fixture.fileManager.deleteCurrentSession() - fixture.fileManager.deleteCrashedSession() - fixture.fileManager.deleteAppState() - fixture.fileManager.deleteTimestampLastInForeground() - fixture.fileManager.deleteAllEnvelopes() - sut = fixture.getSut() } - - override func tearDown() { - super.tearDown() - fixture.fileManager.deleteCurrentSession() - fixture.fileManager.deleteCrashedSession() - fixture.fileManager.deleteAppState() - fixture.fileManager.deleteTimestampLastInForeground() - fixture.fileManager.deleteAllEnvelopes() - } func testBeforeBreadcrumbWithoutCallbackStoresBreadcrumb() { let hub = fixture.getSut() diff --git a/Tests/SentryTests/SentrySDKIntegrationTestsBase.swift b/Tests/SentryTests/SentrySDKIntegrationTestsBase.swift index 2ed92c53618..e4c9a001440 100644 --- a/Tests/SentryTests/SentrySDKIntegrationTestsBase.swift +++ b/Tests/SentryTests/SentrySDKIntegrationTestsBase.swift @@ -1,7 +1,7 @@ import Foundation import XCTest -class SentrySDKIntegrationTestsBase: XCTestCase { +class SentrySDKIntegrationTestsBase: SentryBaseUnitTest { var currentDate = TestCurrentDateProvider() var crashWrapper: TestSentryCrashWrapper! @@ -17,11 +17,6 @@ class SentrySDKIntegrationTestsBase: XCTestCase { currentDate = TestCurrentDateProvider() } - override func tearDown() { - super.tearDown() - clearTestState() - } - func givenSdkWithHub(_ options: Options? = nil) { let client = TestClient(options: options ?? self.options) let hub = SentryHub(client: client, andScope: Scope(), andCrashWrapper: TestSentryCrashWrapper.sharedInstance(), andCurrentDateProvider: currentDate) diff --git a/Tests/SentryTests/SentrySDKTests.swift b/Tests/SentryTests/SentrySDKTests.swift index 20126473576..2666bf2a7e0 100644 --- a/Tests/SentryTests/SentrySDKTests.swift +++ b/Tests/SentryTests/SentrySDKTests.swift @@ -1,13 +1,13 @@ @testable import Sentry import XCTest -class SentrySDKTests: XCTestCase { +class SentrySDKTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentrySDKTests") private static let dsn = TestConstants.dsn(username: "SentrySDKTests") private class Fixture { - + let options: Options let event: Event let scope: Scope @@ -59,20 +59,17 @@ class SentrySDKTests: XCTestCase { override func setUp() { super.setUp() fixture = Fixture() + SentrySDK.setCurrentHub(fixture.hub) } override func tearDown() { super.tearDown() - givenSdkWithHubButNoClient() - if let autoSessionTracking = SentrySDK.currentHub().installedIntegrations.first(where: { it in it is SentryAutoSessionTrackingIntegration }) as? SentryAutoSessionTrackingIntegration { autoSessionTracking.stop() } - - clearTestState() } // Repro for: https://github.com/getsentry/sentry-cocoa/issues/1325 @@ -154,7 +151,7 @@ class SentrySDKTests: XCTestCase { } func testCrashedLastRun() { - XCTAssertEqual(SentryCrash.sharedInstance().crashedLastLaunch, SentrySDK.crashedLastRun) + XCTAssertEqual(SentryCrash.sharedInstance().crashedLastLaunch, SentrySDK.crashedLastRun) } func testCaptureCrashEvent() { @@ -163,56 +160,44 @@ class SentrySDKTests: XCTestCase { let event = fixture.event SentrySDK.captureCrash(event) - + XCTAssertEqual(1, hub.sentCrashEvents.count) XCTAssertEqual(event.message, hub.sentCrashEvents.first?.message) XCTAssertEqual(event.eventId, hub.sentCrashEvents.first?.eventId) } func testCaptureEvent() { - givenSdkWithHub() - SentrySDK.capture(event: fixture.event) assertEventCaptured(expectedScope: fixture.scope) } func testCaptureEventWithScope() { - givenSdkWithHub() - let scope = Scope() SentrySDK.capture(event: fixture.event, scope: scope) - + assertEventCaptured(expectedScope: scope) } - + func testCaptureEventWithScopeBlock_ScopePassedToHub() { - givenSdkWithHub() - SentrySDK.capture(event: fixture.event, block: fixture.scopeBlock) - + assertEventCaptured(expectedScope: fixture.scopeWithBlockApplied) } func testCaptureEventWithScopeBlock_CreatesNewScope() { - givenSdkWithHub() - SentrySDK.capture(event: fixture.event, block: fixture.scopeBlock) - + assertHubScopeNotChanged() } func testCaptureError() { - givenSdkWithHub() - SentrySDK.capture(error: fixture.error) assertErrorCaptured(expectedScope: fixture.scope) } func testCaptureErrorWithScope() { - givenSdkWithHub() - let scope = Scope() SentrySDK.capture(error: fixture.error, scope: scope) @@ -220,32 +205,24 @@ class SentrySDKTests: XCTestCase { } func testCaptureErrorWithScopeBlock_ScopePassedToHub() { - givenSdkWithHub() - SentrySDK.capture(error: fixture.error, block: fixture.scopeBlock) assertErrorCaptured(expectedScope: fixture.scopeWithBlockApplied) } func testCaptureErrorWithScopeBlock_CreatesNewScope() { - givenSdkWithHub() - SentrySDK.capture(error: fixture.error, block: fixture.scopeBlock) assertHubScopeNotChanged() } func testCaptureException() { - givenSdkWithHub() - SentrySDK.capture(exception: fixture.exception) assertExceptionCaptured(expectedScope: fixture.scope) } func testCaptureExceptionWithScope() { - givenSdkWithHub() - let scope = Scope() SentrySDK.capture(exception: fixture.exception, scope: scope) @@ -253,40 +230,30 @@ class SentrySDKTests: XCTestCase { } func testCaptureExceptionWithScopeBlock_ScopePassedToHub() { - givenSdkWithHub() - SentrySDK.capture(exception: fixture.exception, block: fixture.scopeBlock) assertExceptionCaptured(expectedScope: fixture.scopeWithBlockApplied) } func testCaptureExceptionWithScopeBlock_CreatesNewScope() { - givenSdkWithHub() - SentrySDK.capture(exception: fixture.exception, block: fixture.scopeBlock) assertHubScopeNotChanged() } func testCaptureMessageWithScopeBlock_ScopePassedToHub() { - givenSdkWithHub() - SentrySDK.capture(message: fixture.message, block: fixture.scopeBlock) assertMessageCaptured(expectedScope: fixture.scopeWithBlockApplied) } func testCaptureMessageWithScopeBlock_CreatesNewScope() { - givenSdkWithHub() - SentrySDK.capture(message: fixture.message, block: fixture.scopeBlock) assertHubScopeNotChanged() } func testCaptureEnvelope() { - givenSdkWithHub() - let envelope = SentryEnvelope(event: TestData.event) SentrySDK.capture(envelope) @@ -295,8 +262,6 @@ class SentrySDKTests: XCTestCase { } func testStoreEnvelope() { - givenSdkWithHub() - let envelope = SentryEnvelope(event: TestData.event) SentrySDK.store(envelope) @@ -305,14 +270,13 @@ class SentrySDKTests: XCTestCase { } func testStoreEnvelope_WhenNoClient_NoCrash() { + SentrySDK.setCurrentHub(SentryHub(client: nil, andScope: nil)) SentrySDK.store(SentryEnvelope(event: TestData.event)) XCTAssertEqual(0, fixture.client.storedEnvelopeInvocations.count) } func testCaptureUserFeedback() { - givenSdkWithHub() - SentrySDK.capture(userFeedback: fixture.userFeedback) let client = fixture.client XCTAssertEqual(1, client.captureUserFeedbackInvocations.count) @@ -326,8 +290,6 @@ class SentrySDKTests: XCTestCase { } func testSetUser_SetsUserToScopeOfHub() { - givenSdkWithHub() - let user = TestData.user SentrySDK.setUser(user) @@ -337,8 +299,6 @@ class SentrySDKTests: XCTestCase { } func testStartTransaction() { - givenSdkWithHub() - let span = SentrySDK.startTransaction(name: "Some Transaction", operation: "Operations", bindToScope: true) let newSpan = SentrySDK.span @@ -370,8 +330,6 @@ class SentrySDKTests: XCTestCase { } func testStartSession() { - givenSdkWithHub() - SentrySDK.startSession() XCTAssertEqual(1, fixture.client.captureSessionInvocations.count) @@ -390,8 +348,6 @@ class SentrySDKTests: XCTestCase { } func testEndSession() throws { - givenSdkWithHub() - SentrySDK.startSession() advanceTime(bySeconds: 1) SentrySDK.endSession() @@ -416,15 +372,18 @@ class SentrySDKTests: XCTestCase { func testSetAppStartMeasurement_CallsPrivateSDKCallback() { let appStartMeasurement = TestData.getAppStartMeasurement(type: .warm) - - var callbackCalled = false + + let exp = expectation(description: "Callback invoked with app start measurement") PrivateSentrySDKOnly.onAppStartMeasurementAvailable = { measurement in + guard measurement != nil else { + return + } XCTAssertEqual(appStartMeasurement, measurement) - callbackCalled = true + exp.fulfill() } SentrySDK.setAppStartMeasurement(appStartMeasurement) - XCTAssertTrue(callbackCalled) + waitForExpectations(timeout: 1) } func testSetAppStartMeasurement_NoCallback_CallbackNotCalled() { @@ -446,6 +405,8 @@ class SentrySDKTests: XCTestCase { } func testIsEnabled() { + SentrySDK.setCurrentHub(SentryHub(client: nil, andScope: nil)) + XCTAssertFalse(SentrySDK.isEnabled) SentrySDK.capture(message: "message") @@ -560,16 +521,10 @@ class SentrySDKTests: XCTestCase { let result = fileManager.readPreviousBreadcrumbs() XCTAssertEqual(result.count, 3) } - - private func givenSdkWithHub() { - SentrySDK.setCurrentHub(fixture.hub) - } - - private func givenSdkWithHubButNoClient() { - SentrySDK.setCurrentHub(SentryHub(client: nil, andScope: nil)) - } - - private func assertIntegrationsInstalled(integrations: [String]) { +} + +private extension SentrySDKTests { + func assertIntegrationsInstalled(integrations: [String]) { XCTAssertEqual(integrations.count, SentrySDK.currentHub().installedIntegrations.count) integrations.forEach { integration in if let integrationClass = NSClassFromString(integration) { @@ -580,40 +535,40 @@ class SentrySDKTests: XCTestCase { } } - private func assertEventCaptured(expectedScope: Scope) { + func assertEventCaptured(expectedScope: Scope) { let client = fixture.client XCTAssertEqual(1, client.captureEventWithScopeInvocations.count) XCTAssertEqual(fixture.event, client.captureEventWithScopeInvocations.first?.event) XCTAssertEqual(expectedScope, client.captureEventWithScopeInvocations.first?.scope) } - private func assertErrorCaptured(expectedScope: Scope) { + func assertErrorCaptured(expectedScope: Scope) { let client = fixture.client XCTAssertEqual(1, client.captureErrorWithScopeInvocations.count) XCTAssertEqual(fixture.error.localizedDescription, client.captureErrorWithScopeInvocations.first?.error.localizedDescription) XCTAssertEqual(expectedScope, client.captureErrorWithScopeInvocations.first?.scope) } - private func assertExceptionCaptured(expectedScope: Scope) { + func assertExceptionCaptured(expectedScope: Scope) { let client = fixture.client XCTAssertEqual(1, client.captureExceptionWithScopeInvocations.count) XCTAssertEqual(fixture.exception, client.captureExceptionWithScopeInvocations.first?.exception) XCTAssertEqual(expectedScope, client.captureExceptionWithScopeInvocations.first?.scope) } - private func assertMessageCaptured(expectedScope: Scope) { + func assertMessageCaptured(expectedScope: Scope) { let client = fixture.client XCTAssertEqual(1, client.captureMessageWithScopeInvocations.count) XCTAssertEqual(fixture.message, client.captureMessageWithScopeInvocations.first?.message) XCTAssertEqual(expectedScope, client.captureMessageWithScopeInvocations.first?.scope) } - private func assertHubScopeNotChanged() { + func assertHubScopeNotChanged() { let hubScope = SentrySDK.currentHub().scope XCTAssertEqual(fixture.scope, hubScope) } - private func advanceTime(bySeconds: TimeInterval) { + func advanceTime(bySeconds: TimeInterval) { fixture.currentDate.setDate(date: fixture.currentDate.date().addingTimeInterval(bySeconds)) } } diff --git a/Tests/SentryTests/SentryScreenShotTests.swift b/Tests/SentryTests/SentryScreenShotTests.swift index e8dc6725486..a750f6f39d1 100644 --- a/Tests/SentryTests/SentryScreenShotTests.swift +++ b/Tests/SentryTests/SentryScreenShotTests.swift @@ -1,7 +1,7 @@ import XCTest #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) -class SentryScreenShotTests: XCTestCase { +class SentryScreenShotTests: SentryBaseUnitTest { private class Fixture { let uiApplication = TestSentryUIApplication() @@ -19,11 +19,6 @@ class SentryScreenShotTests: XCTestCase { SentryDependencyContainer.sharedInstance().application = fixture.uiApplication } - override func tearDown() { - super.tearDown() - clearTestState() - } - func test_IsMainThread() { let testWindow = TestWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10)) var isMainThread = false diff --git a/Tests/SentryTests/SentryViewHierarchyTests.swift b/Tests/SentryTests/SentryViewHierarchyTests.swift index ae5d1ab21ad..27b3a985dee 100644 --- a/Tests/SentryTests/SentryViewHierarchyTests.swift +++ b/Tests/SentryTests/SentryViewHierarchyTests.swift @@ -1,7 +1,7 @@ import XCTest #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) -class SentryViewHierarchyTests: XCTestCase { +class SentryViewHierarchyTests: SentryBaseUnitTest { private class Fixture { let uiApplication = TestSentryUIApplication() @@ -19,11 +19,6 @@ class SentryViewHierarchyTests: XCTestCase { SentryDependencyContainer.sharedInstance().application = fixture.uiApplication } - override func tearDown() { - super.tearDown() - clearTestState() - } - func test_Draw_Each_Window() { let firstWindow = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10)) let secondWindow = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10)) diff --git a/Tests/SentryTests/Transaction/SentryTraceStateTests.swift b/Tests/SentryTests/Transaction/SentryTraceStateTests.swift index bec8212919c..63e0ffb6ad7 100644 --- a/Tests/SentryTests/Transaction/SentryTraceStateTests.swift +++ b/Tests/SentryTests/Transaction/SentryTraceStateTests.swift @@ -1,6 +1,6 @@ import XCTest -class SentryTraceContextTests: XCTestCase { +class SentryTraceContextTests: SentryBaseUnitTest { private static let dsnAsString = TestConstants.dsnAsString(username: "SentrySessionTrackerTests") @@ -43,11 +43,6 @@ class SentryTraceContextTests: XCTestCase { fixture = Fixture() } - override func tearDown() { - super.tearDown() - clearTestState() - } - func testInit() { let traceContext = SentryTraceContext( trace: fixture.traceId,