Skip to content

Commit

Permalink
deprecation workarounds in swift test code
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight committed Feb 20, 2025
1 parent ace8109 commit bd9301e
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 26 deletions.
7 changes: 7 additions & 0 deletions SentryTestUtils/TestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,18 @@ public class TestClient: SentryClient {
saveCrashTransactionInvocations.record((transaction, scope))
}

@available(*, deprecated, message: "-[SentryClient captureUserFeedback:] is deprecated. -[SentryClient captureFeedback:withScope:] is the new way. See captureFeedbackInvocations.")
public var captureUserFeedbackInvocations = Invocations<UserFeedback>()
@available(*, deprecated, message: "-[SentryClient captureUserFeedback:] is deprecated. -[SentryClient captureFeedback:withScope:] is the new way. See capture(feedback:scope:).")
public override func capture(userFeedback: UserFeedback) {
captureUserFeedbackInvocations.record(userFeedback)
}

public var captureFeedbackInvocations = Invocations<(SentryFeedback, Scope)>()
public override func capture(feedback: SentryFeedback, scope: Scope) {
captureFeedbackInvocations.record((feedback, scope))
}

public var captureEnvelopeInvocations = Invocations<SentryEnvelope>()
public override func capture(_ envelope: SentryEnvelope) {
captureEnvelopeInvocations.record(envelope)
Expand Down
4 changes: 3 additions & 1 deletion SentryTestUtils/TestTransportAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public class TestTransportAdapter: SentryTransportAdapter {
public override func store(_ event: Event, traceContext: TraceContext?) {
storeEventInvocations.record((event, traceContext))
}


@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback. There is currently no envelope initializer accepting a SentryFeedback; the envelope is currently built directly in -[SentryClient captureFeedback:withScope:] and sent to -[SentryTransportAdapter sendEvent:traceContext:attachments:additionalEnvelopeItems:]. See TestClient.captureFeedbackInvocations, used in SentrySDKTests.testCaptureFeedback.")
public var userFeedbackInvocations = Invocations<UserFeedback>()
@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback. There is currently no envelope initializer accepting a SentryFeedback; the envelope is currently built directly in -[SentryClient captureFeedback:withScope:] and sent to -[SentryTransportAdapter sendEvent:traceContext:attachments:additionalEnvelopeItems:]. See TestClient.capture(feedback:scope:), used in SentrySDKTests.testCaptureFeedback.")
public override func send(userFeedback: UserFeedback) {
userFeedbackInvocations.record(userFeedback)
}
Expand Down
19 changes: 11 additions & 8 deletions Tests/SentryTests/Networking/SentryHttpTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ class SentryHttpTransportTests: XCTestCase {
#endif // !os(watchOS)

let flushTimeout: TimeInterval = 2.0

let userFeedback: UserFeedback
let userFeedbackRequest: SentryNSURLRequest

@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback.")
let userFeedback: UserFeedback = TestData.userFeedback
let feedback: SentryFeedback = TestData.feedback
@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback. There is currently no envelope initializer accepting a SentryFeedback; the envelope is currently built directly in -[SentryClient captureFeedback:withScope:] and sent to -[SentryTransportAdapter sendEvent:traceContext:attachments:additionalEnvelopeItems:].")
lazy var userFeedbackRequest: SentryNSURLRequest = {
let userFeedbackEnvelope = SentryEnvelope(userFeedback: userFeedback)
userFeedbackEnvelope.header.sentAt = SentryDependencyContainer.sharedInstance().dateProvider.date()
return buildRequest(userFeedbackEnvelope)
}()

let clientReport: SentryClientReport
let clientReportEnvelope: SentryEnvelope
Expand Down Expand Up @@ -78,11 +85,6 @@ class SentryHttpTransportTests: XCTestCase {

let currentDate = TestCurrentDateProvider()
rateLimits = DefaultRateLimits(retryAfterHeaderParser: RetryAfterHeaderParser(httpDateParser: HttpDateParser(), currentDateProvider: currentDate), andRateLimitParser: RateLimitParser(currentDateProvider: currentDate), currentDateProvider: currentDate)

userFeedback = TestData.userFeedback
let userFeedbackEnvelope = SentryEnvelope(userFeedback: userFeedback)
userFeedbackEnvelope.header.sentAt = SentryDependencyContainer.sharedInstance().dateProvider.date()
userFeedbackRequest = buildRequest(userFeedbackEnvelope)

let beforeSendTransaction = SentryDiscardedEvent(reason: .beforeSend, category: .transaction, quantity: 2)
let sampleRateTransaction = SentryDiscardedEvent(reason: .sampleRate, category: .transaction, quantity: 1)
Expand Down Expand Up @@ -223,6 +225,7 @@ class SentryHttpTransportTests: XCTestCase {
assertRequestsSent(requestCount: 1)
}

@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback. There is currently no envelope initializer accepting a SentryFeedback; the envelope is currently built directly in -[SentryClient captureFeedback:withScope:] and sent to -[SentryTransportAdapter sendEvent:traceContext:attachments:additionalEnvelopeItems:]. This test case can be removed in favor of SentryClientTests.testCaptureFeedback")
func testSendUserFeedback() {
let envelope = SentryEnvelope(userFeedback: fixture.userFeedback)
sut.send(envelope: envelope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SentryTransportAdapterTests: XCTestCase {
try assertSentEnvelope(expected: expectedEnvelope)
}

@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback. This test case can be removed when SentryUserFeedback is removed.")
func testSendUserFeedback_SendsUserFeedbackEnvelope() throws {
let userFeedback = TestData.userFeedback
sut.send(userFeedback: userFeedback)
Expand Down
11 changes: 4 additions & 7 deletions Tests/SentryTests/Protocol/SentryEnvelopeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class SentryEnvelopeTests: XCTestCase {

private class Fixture {
let sdkVersion = "sdkVersion"
let userFeedback: UserFeedback
@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback.")
let userFeedback: UserFeedback = TestData.userFeedback
let path = "test.log"
let data = "hello".data(using: .utf8)

Expand All @@ -15,11 +16,6 @@ class SentryEnvelopeTests: XCTestCase {
let dataTooBig: Data

init() {
userFeedback = UserFeedback(eventId: SentryId())
userFeedback.comments = "It doesn't work!"
userFeedback.email = "[email protected]"
userFeedback.name = "John Me"

dataAllowed = Data([UInt8](repeating: 1, count: Int(maxAttachmentSize)))
dataTooBig = Data([UInt8](repeating: 1, count: Int(maxAttachmentSize) + 1))
}
Expand Down Expand Up @@ -49,7 +45,7 @@ class SentryEnvelopeTests: XCTestCase {
return event
}
}

private let fixture = Fixture()

override func setUp() {
Expand Down Expand Up @@ -206,6 +202,7 @@ class SentryEnvelopeTests: XCTestCase {
}
}

@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback. This test case can be removed when SentryUserFeedback is removed.")
func testInitWithUserFeedback() throws {
let userFeedback = fixture.userFeedback

Expand Down
1 change: 1 addition & 0 deletions Tests/SentryTests/Protocol/SentryUserFeedbackTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import XCTest

@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback. This test suite can be removed when SentryUserFeedback is removed.")
class SentryUserFeedbackTests: XCTestCase {

func testPropertiesAreSetToEmptyString() {
Expand Down
3 changes: 3 additions & 0 deletions Tests/SentryTests/Protocol/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ class TestData {
return scope
}

@available(*, deprecated, message: "SentryUserFeedback is deprecated in favor of SentryFeedback.")
static var userFeedback: UserFeedback {
let userFeedback = UserFeedback(eventId: SentryId())
userFeedback.comments = "It doesn't really"
Expand All @@ -310,6 +311,8 @@ class TestData {
return userFeedback
}

static var feedback = SentryFeedback(message: "It doesn't really", name: "John Me", email: "[email protected]", associatedEventId: SentryId())

static func setContext(_ scope: Scope) {
scope.setContext(value: TestData.context["context"]!, key: "context")
}
Expand Down
59 changes: 49 additions & 10 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class SentryClientTest: XCTestCase {
let queue = DispatchQueue(label: "SentryHubTests", qos: .utility, attributes: [.concurrent])
let dispatchQueue = TestSentryDispatchQueueWrapper()

let feedback = SentryFeedback(message: "A test message", name: "Abe Tester", email: "[email protected]", source: .custom, associatedEventId: SentryId())

init() throws {
session = SentrySession(releaseName: "release", distinctId: "some-id")
session.incrementErrors()
Expand Down Expand Up @@ -1166,35 +1168,40 @@ class SentryClientTest: XCTestCase {
let serializedSpans = try XCTUnwrap(serialized["spans"] as? [[String: Any]])
XCTAssertEqual(1, serializedSpans.count)
}


@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testNoDsn_MessageNotSent() {
let sut = fixture.getSutWithNoDsn()
let eventId = sut.capture(message: fixture.messageAsString)
eventId.assertIsEmpty()
assertNothingSent()
}

@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testDisabled_MessageNotSent() {
let sut = fixture.getSutDisabledSdk()
let eventId = sut.capture(message: fixture.messageAsString)
eventId.assertIsEmpty()
assertNothingSent()
}


@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testNoDsn_ExceptionNotSent() {
let sut = fixture.getSutWithNoDsn()
let eventId = sut.capture(exception: exception)
eventId.assertIsEmpty()
assertNothingSent()
}


@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testNoDsn_ErrorNotSent() {
let sut = fixture.getSutWithNoDsn()
let eventId = sut.capture(error: error)
eventId.assertIsEmpty()
assertNothingSent()
}


@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testNoDsn_SessionsNotSent() {
_ = SentryEnvelope(event: Event())
fixture.getSut(configureOptions: { options in
Expand All @@ -1203,7 +1210,8 @@ class SentryClientTest: XCTestCase {

assertNothingSent()
}


@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testNoDsn_EventWithSessionsNotSent() {
_ = SentryEnvelope(event: Event())
let eventId = fixture.getSut(configureOptions: { options in
Expand All @@ -1213,7 +1221,8 @@ class SentryClientTest: XCTestCase {
eventId.assertIsEmpty()
assertNothingSent()
}


@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testNoDsn_ExceptionWithSessionsNotSent() {
_ = SentryEnvelope(event: Event())
let eventId = fixture.getSut(configureOptions: { options in
Expand All @@ -1225,7 +1234,8 @@ class SentryClientTest: XCTestCase {
eventId.assertIsEmpty()
assertNothingSent()
}


@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testNoDsn_ErrorWithSessionsNotSent() {
_ = SentryEnvelope(event: Event())
let eventId = fixture.getSut(configureOptions: { options in
Expand All @@ -1238,18 +1248,22 @@ class SentryClientTest: XCTestCase {
assertNothingSent()
}

@available(*, deprecated, message: "This is only marked as deprecated because assertSampleRate is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testSampleRateNil_EventNotSampled() throws {
try assertSampleRate(sampleRate: nil, randomValue: 0, isSampled: false)
}

@available(*, deprecated, message: "This is only marked as deprecated because assertSampleRate is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testSampleRateBiggerRandom_EventNotSampled() throws {
try assertSampleRate(sampleRate: 0.5, randomValue: 0.49, isSampled: false)
}

@available(*, deprecated, message: "This is only marked as deprecated because assertSampleRate is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testSampleRateEqualsRandom_EventNotSampled() throws {
try assertSampleRate(sampleRate: 0.5, randomValue: 0.5, isSampled: false)
}

@available(*, deprecated, message: "This is only marked as deprecated because assertSampleRate is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, these deprecation annotations can go away.")
func testSampleRateSmallerRandom_EventSampled() throws {
try assertSampleRate(sampleRate: 0.50, randomValue: 0.51, isSampled: true)
}
Expand Down Expand Up @@ -1464,22 +1478,45 @@ class SentryClientTest: XCTestCase {
XCTAssertEqual(fixture.transport.recordLostEventsWithCount.get(2)?.reason, SentryDiscardReason.beforeSend)
XCTAssertEqual(fixture.transport.recordLostEventsWithCount.get(2)?.quantity, 1)
}

@available(*, deprecated, message: "-[SentryClient captureUserFeedback:] is deprecated. -[SentryClient captureFeedback:withScope:] is the new way. This test case can be removed in favor of testNoDsn_FeedbackNotSent when -[SentryClient captureUserFeedback:] is removed.")
func testNoDsn_UserFeedbackNotSent() {
let sut = fixture.getSutWithNoDsn()
sut.capture(userFeedback: UserFeedback(eventId: SentryId()))
assertNothingSent()
}

@available(*, deprecated, message: "-[SentryClient captureUserFeedback:] is deprecated. -[SentryClient captureFeedback:withScope:] is the new way. This test case can be removed in favor of testDisabled_FeedbackNotSent when -[SentryClient captureUserFeedback:] is removed.")
func testDisabled_UserFeedbackNotSent() {
let sut = fixture.getSutDisabledSdk()
sut.capture(userFeedback: UserFeedback(eventId: SentryId()))
assertNothingSent()
}

@available(*, deprecated, message: "-[SentryClient captureUserFeedback:] is deprecated. -[SentryClient captureFeedback:withScope:] is the new way. This test case can be removed in favor of testCaptureFeedback_WithEmptyEventId when -[SentryClient captureUserFeedback:] is removed.")
func testCaptureUserFeedback_WithEmptyEventId() {
let sut = fixture.getSut()
sut.capture(userFeedback: UserFeedback(eventId: SentryId.empty))
sut.capture(userFeedback: UserFeedback(eventId: SentryId()))
assertNothingSent()
}

@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, this deprecation annotation can go away.")
func testNoDsn_FeedbackNotSent() {
let sut = fixture.getSutWithNoDsn()
sut.capture(feedback: fixture.feedback, scope: fixture.scope)
assertNothingSent()
}

@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, this deprecation annotation can go away.")
func testDisabled_FeedbackNotSent() {
let sut = fixture.getSutDisabledSdk()
sut.capture(feedback: fixture.feedback, scope: fixture.scope)
assertNothingSent()
}

@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, this deprecation annotation can go away.")
func testCaptureFeedback_WithEmptyEventId() {
let sut = fixture.getSut()
sut.capture(feedback: fixture.feedback, scope: fixture.scope)
assertNothingSent()
}

Expand Down Expand Up @@ -2094,7 +2131,8 @@ private extension SentryClientTest {
private func shortenIntegrations(_ integrations: [String]?) -> [String]? {
return integrations?.map { $0.replacingOccurrences(of: "Sentry", with: "").replacingOccurrences(of: "Integration", with: "") }
}


@available(*, deprecated, message: "Remove check on transportAdapter.userFeedbackInvocations when SentryUserFeedback is removed in favor of SentryFeedback. Then this deprecation annotation can be removed.")
private func assertNothingSent() {
XCTAssertTrue(fixture.transport.sentEnvelopes.isEmpty)
XCTAssertEqual(0, fixture.transportAdapter.sentEventsWithSessionTraceState.count)
Expand Down Expand Up @@ -2154,6 +2192,7 @@ private extension SentryClientTest {
}
#endif

@available(*, deprecated, message: "This is only marked as deprecated because assertNothingSent is marked as deprecated, due to it using a deprecated property inside it. When that property usage is removed, this deprecation annotations can be removed.")
func assertSampleRate( sampleRate: NSNumber?, randomValue: Double, isSampled: Bool) throws {
fixture.random.value = randomValue

Expand Down
Loading

0 comments on commit bd9301e

Please sign in to comment.