Skip to content

Commit

Permalink
Add Sentry.isEnabled() to common code (#273)
Browse files Browse the repository at this point in the history
* add function

* fix imports

* update changelog

* Format code

* update

* update api

---------

Co-authored-by: Sentry Github Bot <[email protected]>
  • Loading branch information
buenaflor and getsentry-bot authored Oct 7, 2024
1 parent 7715854 commit 27c2e03
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add `Sentry.isEnabled()` API to common code ([#273](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/273))

## 0.9.0

### Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public final class io/sentry/kotlin/multiplatform/Sentry {
public final fun init (Lkotlin/jvm/functions/Function1;)V
public final fun initWithPlatformOptions (Lkotlin/jvm/functions/Function1;)V
public final fun isCrashedLastRun ()Z
public final fun isEnabled ()Z
public final fun setUser (Lio/sentry/kotlin/multiplatform/protocol/User;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public final class io/sentry/kotlin/multiplatform/Sentry {
public final fun init (Lkotlin/jvm/functions/Function1;)V
public final fun initWithPlatformOptions (Lkotlin/jvm/functions/Function1;)V
public final fun isCrashedLastRun ()Z
public final fun isEnabled ()Z
public final fun setUser (Lio/sentry/kotlin/multiplatform/protocol/User;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ internal actual class SentryBridge actual constructor(private val sentryInstance
return SentrySDK.crashedLastRun()
}

actual fun isEnabled(): Boolean {
return SentrySDK.isEnabled()
}

actual fun close() {
SentrySDK.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ internal actual class SentryBridge actual constructor(private val sentryInstance
return Sentry.isCrashedLastRun() ?: false
}

actual fun isEnabled(): Boolean {
return Sentry.isEnabled()
}

actual fun close() {
Sentry.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ internal expect class SentryBridge(sentryInstance: SentryInstance = SentryPlatfo

fun isCrashedLastRun(): Boolean

fun isEnabled(): Boolean

fun close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ public object Sentry {
throw RuntimeException("Uncaught Exception from Kotlin Multiplatform.")
}

/**
* Checks if the SDK is enabled.
*/
public fun isEnabled(): Boolean {
return bridge.isEnabled()
}

/**
* Closes the SDK.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.test.runTest
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -225,6 +226,20 @@ class SentryIntegrationTest : BaseSentryTest() {
assertEquals(expectedUsername, actualUsername)
}

@Test
fun `isEnabled returns true when SDK is enabled`() {
sentryInit {
it.dsn = fakeDsn
}

assertTrue(Sentry.isEnabled())
}

@Test
fun `isEnabled returns false when SDK is disabled`() {
assertFalse(Sentry.isEnabled())
}

@Test
fun `global scope sets context correctly with different data types`() = runTest {
val stringKey = "stringKey"
Expand Down

0 comments on commit 27c2e03

Please sign in to comment.