Skip to content

Commit

Permalink
test: convert remaining unchecked accesses to failable versions (#4132)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Jul 8, 2024
1 parent 2deb275 commit 0f4071f
Show file tree
Hide file tree
Showing 36 changed files with 311 additions and 324 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
selectedDebuggerIdentifier = ""
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down Expand Up @@ -95,7 +95,7 @@
</CommandLineArgument>
<CommandLineArgument
argument = "--io.sentry.enableContinuousProfiling"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--io.sentry.wipe-data"
Expand Down Expand Up @@ -155,7 +155,7 @@
</CommandLineArgument>
<CommandLineArgument
argument = "--disable-ui-tracing"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
<EnvironmentVariables>
Expand Down
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@
84AC61D729F75A98009EEF61 /* SentryDispatchFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = 84AC61D529F75A98009EEF61 /* SentryDispatchFactory.m */; };
84AC61D929F7643B009EEF61 /* TestDispatchFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84AC61D829F7643B009EEF61 /* TestDispatchFactory.swift */; };
84AC61DB29F7654A009EEF61 /* TestDispatchSourceWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84AC61DA29F7654A009EEF61 /* TestDispatchSourceWrapper.swift */; };
84AEB46A2C2F97FC007E46E1 /* ArrayAccesses.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84AEB4682C2F9673007E46E1 /* ArrayAccesses.swift */; };
84AF45A629A7FFA500FBB177 /* SentryProfiledTracerConcurrency.h in Headers */ = {isa = PBXBuildFile; fileRef = 84AF45A429A7FFA500FBB177 /* SentryProfiledTracerConcurrency.h */; };
84AF45A729A7FFA500FBB177 /* SentryProfiledTracerConcurrency.mm in Sources */ = {isa = PBXBuildFile; fileRef = 84AF45A529A7FFA500FBB177 /* SentryProfiledTracerConcurrency.mm */; };
84B7FA3529B285FC00AD93B1 /* Sentry.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63AA759B1EB8AEF500D153DE /* Sentry.framework */; };
Expand Down Expand Up @@ -1740,6 +1741,7 @@
84AC61D529F75A98009EEF61 /* SentryDispatchFactory.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryDispatchFactory.m; sourceTree = "<group>"; };
84AC61D829F7643B009EEF61 /* TestDispatchFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDispatchFactory.swift; sourceTree = "<group>"; };
84AC61DA29F7654A009EEF61 /* TestDispatchSourceWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDispatchSourceWrapper.swift; sourceTree = "<group>"; };
84AEB4682C2F9673007E46E1 /* ArrayAccesses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayAccesses.swift; sourceTree = "<group>"; };
84AF45A429A7FFA500FBB177 /* SentryProfiledTracerConcurrency.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryProfiledTracerConcurrency.h; path = ../include/SentryProfiledTracerConcurrency.h; sourceTree = "<group>"; };
84AF45A529A7FFA500FBB177 /* SentryProfiledTracerConcurrency.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryProfiledTracerConcurrency.mm; sourceTree = "<group>"; };
84B7FA3B29B2866200AD93B1 /* SentryTestUtils-ObjC-BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentryTestUtils-ObjC-BridgingHeader.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3434,6 +3436,7 @@
8431F00B29B284F200D8DC56 /* SentryTestUtils */ = {
isa = PBXGroup;
children = (
84AEB4682C2F9673007E46E1 /* ArrayAccesses.swift */,
841325DE2BFED0510029228F /* TestFramesTracker.swift */,
841325C42BF49EC40029228F /* SentryLaunchProfiling+Tests.h */,
7B4C817124D1BC2B0076ACE4 /* SentryFileManager+Test.h */,
Expand Down Expand Up @@ -5040,6 +5043,7 @@
84B7FA4229B28CDE00AD93B1 /* TestCurrentDateProvider.swift in Sources */,
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */,
62FC69362BEDFF18002D3EF2 /* SentryLogExtensions.swift in Sources */,
84AEB46A2C2F97FC007E46E1 /* ArrayAccesses.swift in Sources */,
84B7FA3F29B28BAD00AD93B1 /* TestTransport.swift in Sources */,
84A5D75B29D5170700388BFA /* TimeInterval+Sentry.swift in Sources */,
62AB8C9E2BF3925700BFC2AC /* WeakReference.swift in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions SentryTestUtils/ArrayAccesses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

public extension Array {
func element(at index: Int) -> Self.Element? {
guard count >= index else {
return nil
}
return self[index]
}
}
10 changes: 5 additions & 5 deletions Tests/SentryProfilerTests/SentryContinuousProfilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private extension SentryContinuousProfilerTests {
let debugMeta = try XCTUnwrap(profile["debug_meta"] as? [String: Any])
let images = try XCTUnwrap(debugMeta["images"] as? [[String: Any]])
XCTAssertFalse(images.isEmpty)
let firstImage = images[0]
let firstImage = try XCTUnwrap(images.first)
XCTAssertFalse(try XCTUnwrap(firstImage["code_file"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(firstImage["debug_id"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(firstImage["image_addr"] as? String).isEmpty)
Expand All @@ -196,8 +196,8 @@ private extension SentryContinuousProfilerTests {

let frames = try XCTUnwrap(sampledProfile["frames"] as? [[String: Any]])
XCTAssertFalse(frames.isEmpty)
XCTAssertFalse(try XCTUnwrap(frames[0]["instruction_addr"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(frames[0]["function"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(frames.first?["instruction_addr"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(frames.first?["function"] as? String).isEmpty)

let stacks = try XCTUnwrap(sampledProfile["stacks"] as? [[Int]])
var foundAtLeastOneNonEmptySample = false
Expand Down Expand Up @@ -285,10 +285,10 @@ private extension SentryContinuousProfilerTests {
XCTAssertEqual(values.count, readingsPerBatch - (expectOneLessEnergyReading ? 1 : 0), "Wrong number of values under \(key); (expectOneLessEnergyReading: \(expectOneLessEnergyReading))")

if let expectedValue = expectedValue {
let actualValue = try XCTUnwrap(values[1]["value"] as? T)
let actualValue = try XCTUnwrap(try XCTUnwrap(values.element(at: 1))["value"] as? T)
XCTAssertEqual(actualValue, expectedValue, "Wrong value for \(key)")

let timestamp = try XCTUnwrap(values[0]["timestamp"] as? TimeInterval)
let timestamp = try XCTUnwrap(values.first?["timestamp"] as? TimeInterval)
try assertTimestampOccursWithinTransaction(timestamp: timestamp, chunkStartTime: chunkStartTime, chunkEndTime: chunkEndTime)

let actualUnits = try XCTUnwrap(metricContainer["unit"] as? String)
Expand Down
10 changes: 5 additions & 5 deletions Tests/SentryProfilerTests/SentryTraceProfilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ private extension SentryTraceProfilerTests {
XCTAssertEqual(values.count, numberOfReadings, "Wrong number of values under \(key)")

if let expectedValue = expectedValue {
let actualValue = try XCTUnwrap(values[1]["value"] as? T)
let actualValue = try XCTUnwrap(values.element(at: 1)?["value"] as? T)
XCTAssertEqual(actualValue, expectedValue, "Wrong value for \(key)")

let timestamp = try XCTUnwrap(values[0]["elapsed_since_start_ns"] as? NSString)
let timestamp = try XCTUnwrap(values.first?["elapsed_since_start_ns"] as? NSString)
try assertTimestampOccursWithinTransaction(timestamp: timestamp, transaction: transaction)

let actualUnits = try XCTUnwrap(metricContainer["unit"] as? String)
Expand Down Expand Up @@ -527,7 +527,7 @@ private extension SentryTraceProfilerTests {
let debugMeta = try XCTUnwrap(profile["debug_meta"] as? [String: Any])
let images = try XCTUnwrap(debugMeta["images"] as? [[String: Any]])
XCTAssertFalse(images.isEmpty)
let firstImage = images[0]
let firstImage = try XCTUnwrap(images.first)
XCTAssertFalse(try XCTUnwrap(firstImage["code_file"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(firstImage["debug_id"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(firstImage["image_addr"] as? String).isEmpty)
Expand Down Expand Up @@ -555,8 +555,8 @@ private extension SentryTraceProfilerTests {

let frames = try XCTUnwrap(sampledProfile["frames"] as? [[String: Any]])
XCTAssertFalse(frames.isEmpty)
XCTAssertFalse(try XCTUnwrap(frames[0]["instruction_addr"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(frames[0]["function"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(frames.first?["instruction_addr"] as? String).isEmpty)
XCTAssertFalse(try XCTUnwrap(frames.first?["function"] as? String).isEmpty)

let stacks = try XCTUnwrap(sampledProfile["stacks"] as? [[Int]])
var foundAtLeastOneNonEmptySample = false
Expand Down
36 changes: 18 additions & 18 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class SentryFileManagerTests: XCTestCase {
let envelopes = sut.getAllEnvelopes()
XCTAssertEqual(1, envelopes.count)

let actualData = envelopes[0].contents
let actualData = try XCTUnwrap(envelopes.first).contents
XCTAssertEqual(expectedData, actualData as Data)
}

Expand Down Expand Up @@ -337,7 +337,7 @@ class SentryFileManagerTests: XCTestCase {
XCTAssertEqual(maxCacheItems, UInt(events.count))
}

func testMigrateSessionInit_SessionUpdateIsLast() {
func testMigrateSessionInit_SessionUpdateIsLast() throws {
sut.store(fixture.sessionEnvelope)
// just some other session
sut.store(SentryEnvelope(session: SentrySession(releaseName: "1.0.0", distinctId: "some-id")))
Expand All @@ -346,22 +346,22 @@ class SentryFileManagerTests: XCTestCase {
}
sut.store(fixture.sessionUpdateEnvelope)

assertSessionInitMoved(sut.getAllEnvelopes().last!)
try assertSessionInitMoved(sut.getAllEnvelopes().last!)
assertSessionEnvelopesStored(count: 2)
}

func testMigrateSessionInit_SessionUpdateIsSecond() {
func testMigrateSessionInit_SessionUpdateIsSecond() throws {
sut.store(fixture.sessionEnvelope)
sut.store(fixture.sessionUpdateEnvelope)
for _ in 0...(fixture.maxCacheItems - 2) {
sut.store(TestConstants.envelope)
}

assertSessionInitMoved(sut.getAllEnvelopes().first!)
try assertSessionInitMoved(sut.getAllEnvelopes().first!)
assertSessionEnvelopesStored(count: 1)
}

func testMigrateSessionInit_IsInMiddle() {
func testMigrateSessionInit_IsInMiddle() throws {
sut.store(fixture.sessionEnvelope)
for _ in 0...10 {
sut.store(TestConstants.envelope)
Expand All @@ -371,11 +371,11 @@ class SentryFileManagerTests: XCTestCase {
sut.store(TestConstants.envelope)
}

assertSessionInitMoved(sut.getAllEnvelopes()[10])
try assertSessionInitMoved(sut.getAllEnvelopes()[10])
assertSessionEnvelopesStored(count: 1)
}

func testMigrateSessionInit_MovesInitFlagOnlyToFirstSessionUpdate() {
func testMigrateSessionInit_MovesInitFlagOnlyToFirstSessionUpdate() throws {
sut.store(fixture.sessionEnvelope)
for _ in 0...10 {
sut.store(TestConstants.envelope)
Expand All @@ -387,9 +387,9 @@ class SentryFileManagerTests: XCTestCase {
sut.store(TestConstants.envelope)
}

assertSessionInitMoved(sut.getAllEnvelopes()[10])
assertSessionInitNotMoved(sut.getAllEnvelopes()[11])
assertSessionInitNotMoved(sut.getAllEnvelopes()[12])
try assertSessionInitMoved(sut.getAllEnvelopes()[10])
try assertSessionInitNotMoved(sut.getAllEnvelopes()[11])
try assertSessionInitNotMoved(sut.getAllEnvelopes()[12])
assertSessionEnvelopesStored(count: 3)
}

Expand All @@ -403,7 +403,7 @@ class SentryFileManagerTests: XCTestCase {
assertSessionEnvelopesStored(count: 0)
}

func testMigrateSessionInit_FailToLoadEnvelope() {
func testMigrateSessionInit_FailToLoadEnvelope() throws {
sut.store(fixture.sessionEnvelope)

for _ in 0...(fixture.maxCacheItems - 3) {
Expand All @@ -418,7 +418,7 @@ class SentryFileManagerTests: XCTestCase {

sut.store(fixture.sessionUpdateEnvelope)

assertSessionInitMoved(sut.getAllEnvelopes().last!)
try assertSessionInitMoved(sut.getAllEnvelopes().last!)
}

func testMigrateSessionInit_DoesNotCallEnvelopeItemDeleted() {
Expand Down Expand Up @@ -452,7 +452,7 @@ class SentryFileManagerTests: XCTestCase {

let actualEnvelope = SentrySerialization.envelope(with: sut.getOldestEnvelope()?.contents ?? Data())

XCTAssertEqual(fixture.eventIds[11], actualEnvelope?.header.eventId)
XCTAssertEqual(try XCTUnwrap(fixture.eventIds.element(at: 11)), actualEnvelope?.header.eventId)
}

func testGetOldestEnvelope_WhenNoEnvelopes() {
Expand Down Expand Up @@ -909,21 +909,21 @@ private extension SentryFileManagerTests {
"Folder for events should be deleted on init: \(sut.eventsPath)")
}

func assertSessionInitMoved(_ actualSessionFileContents: SentryFileContents) {
func assertSessionInitMoved(_ actualSessionFileContents: SentryFileContents) throws {
let actualSessionEnvelope = SentrySerialization.envelope(with: actualSessionFileContents.contents)
XCTAssertEqual(2, actualSessionEnvelope?.items.count)

let actualSession = SentrySerialization.session(with: actualSessionEnvelope?.items[1].data ?? Data())
let actualSession = SentrySerialization.session(with: try XCTUnwrap(actualSessionEnvelope?.items.element(at: 1)).data)
XCTAssertNotNil(actualSession)

XCTAssertEqual(fixture.expectedSessionUpdate, actualSession)
}

func assertSessionInitNotMoved(_ actualSessionFileContents: SentryFileContents) {
func assertSessionInitNotMoved(_ actualSessionFileContents: SentryFileContents) throws {
let actualSessionEnvelope = SentrySerialization.envelope(with: actualSessionFileContents.contents)
XCTAssertEqual(2, actualSessionEnvelope?.items.count)

let actualSession = SentrySerialization.session(with: actualSessionEnvelope?.items[0].data ?? Data())
let actualSession = SentrySerialization.session(with: try XCTUnwrap(actualSessionEnvelope?.items.first).data)
XCTAssertNotNil(actualSession)

XCTAssertEqual(fixture.sessionUpdate, actualSession)
Expand Down
Loading

0 comments on commit 0f4071f

Please sign in to comment.