Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

test: prevent crashing on nil or index out of bounds #2423

Merged
merged 2 commits into from
Nov 23, 2022

Conversation

armcknight
Copy link
Member

@armcknight armcknight commented Nov 22, 2022

While testing after making changes for #2228, the test suite would crash in various places due to changes in behavior changing e.g. the amount of things in an array. Change such logic to allow graceful degradation into simply failing the test case instead of crashing the test run.

#skip-changelog

@philipphofmann philipphofmann changed the base branch from master to 8.0.0 November 22, 2022 07:09
Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @armcknight for that great refactoring 🙏😀. You could refactor this to use XCTUnwrap. But it's also fine not doing that. I changed the base to 8.0.0 as this is our default branch now until we make 8.0.0 GA to avoid merging back and forth from the master branch.

If you need this for #2228 and you want to do a hotfix for #2228 we could also merge it to both 8.0.0 and master, but I would like to avoid that if possible.

Comment on lines 118 to 121
guard let tracer = transactionSpan as? SentryTracer else {
XCTFail("Expected a tracer")
return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: You could use XCTUnwrap for all of these instead. let tracer = try XCTUnwrap(transactionSpan). For more info see https://www.hackingwithswift.com/example-code/testing/how-to-check-and-unwrap-optionals-in-tests-using-xctunwrap.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I've seen that used in the tests but forgot about it, and haven't used it myself before. I'll clean things up to use it instead!

Copy link
Member Author

@armcknight armcknight Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes things much nicer. The only place I couldn't use them was within the view controller lifecycle callbacks, because they would need to be declared as throws in swift which would require i think an inout NSError in the ObjC interface? I was just wary of changing that SDK code solely for testing purposes.

func testUILifeCycle_ViewDidAppear() {
assertUILifeCycle(finishStatus: SentrySpanStatus.ok) { sut, viewController, tracker, callbackExpectation, transactionSpan in
func testUILifeCycle_ViewDidAppear() throws {
try assertUILifeCycle(finishStatus: SentrySpanStatus.ok) { sut, viewController, tracker, callbackExpectation, transactionSpan in
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertUILifeCycle had some XCTUnwraps in it so had to be marked as throws, which bubbled up to a few other test cases.

@@ -209,63 +203,57 @@ class SentryUIViewControllerPerformanceTrackerTests: XCTestCase {
self.advanceTime(bySeconds: 1)
callbackExpectation.fulfill()
}
assertSpanDuration(span: lastSpan, expectedDuration: 1)
try assertSpanDuration(span: lastSpan, expectedDuration: 1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, there are some XCTUnwraps in assertSpanDuration forcing it to be throws

@armcknight armcknight merged commit bbe81cb into 8.0.0 Nov 23, 2022
@armcknight armcknight deleted the armcknight/test/prevent-crashes branch November 23, 2022 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants