Browse Source

Add `isInDebug` variable to simulate debug/release configs for tests (#2566)

* `isInDebug` as `ThreadLocal<Boolean>`

* Use a simple var for `isInDebug` and add a helper method to test release mode, when running the debug test.

* Add some more docs

---------

Co-authored-by: Benoit Marty <benoit@matrix.org>
feature/bma/fixNotificationContent
Jorge Martin Espinosa 6 months ago committed by GitHub
parent
commit
5cc5a0b699
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      libraries/androidutils/build.gradle.kts
  2. 37
      libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/metadata/IsInDebug.kt
  3. 1
      libraries/matrix/api/build.gradle.kts
  4. 4
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/EventId.kt
  5. 4
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/MatrixPatterns.kt
  6. 4
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/RoomId.kt
  7. 4
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/SpaceId.kt
  8. 4
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/ThreadId.kt
  9. 4
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/UserId.kt
  10. 6
      libraries/matrix/api/src/test/kotlin/io/element/android/libraries/matrix/api/permalink/PermalinkBuilderTest.kt
  11. 1
      tests/testutils/build.gradle.kts
  12. 3
      tests/testutils/src/main/kotlin/io/element/android/tests/testutils/AssertThrowInDebug.kt
  13. 22
      tests/testutils/src/main/kotlin/io/element/android/tests/testutils/IsInDebug.kt

4
libraries/androidutils/build.gradle.kts

@ -20,6 +20,10 @@ plugins { @@ -20,6 +20,10 @@ plugins {
android {
namespace = "io.element.android.libraries.androidutils"
buildFeatures {
buildConfig = true
}
}
anvil {

37
libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/metadata/IsInDebug.kt

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.androidutils.metadata
import io.element.android.libraries.androidutils.BuildConfig
/**
* true if the app is built in debug mode.
* For testing purpose, this can be changed with [withReleaseBehavior].
*/
var isInDebug: Boolean = BuildConfig.DEBUG
private set
/**
* Run the lambda simulating the app is in release mode.
*
* **IMPORTANT**: this should **ONLY** be used for testing purposes.
*/
fun withReleaseBehavior(lambda: () -> Unit) {
isInDebug = false
lambda()
isInDebug = BuildConfig.DEBUG
}

1
libraries/matrix/api/build.gradle.kts

@ -37,6 +37,7 @@ dependencies { @@ -37,6 +37,7 @@ dependencies {
implementation(projects.appconfig)
implementation(projects.libraries.di)
implementation(libs.dagger)
implementation(projects.libraries.androidutils)
implementation(projects.libraries.core)
implementation(libs.serialization.json)
api(projects.libraries.sessionStorage.api)

4
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/EventId.kt

@ -16,13 +16,13 @@ @@ -16,13 +16,13 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import io.element.android.libraries.androidutils.metadata.isInDebug
import java.io.Serializable
@JvmInline
value class EventId(val value: String) : Serializable {
init {
if (BuildConfig.DEBUG && !MatrixPatterns.isEventId(value)) {
if (isInDebug && !MatrixPatterns.isEventId(value)) {
error("`$value` is not a valid event id.\nExample event id: `\$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg`.")
}
}

4
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/MatrixPatterns.kt

@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import io.element.android.libraries.androidutils.metadata.isInDebug
import timber.log.Timber
/**
@ -217,7 +217,7 @@ object MatrixPatterns { @@ -217,7 +217,7 @@ object MatrixPatterns {
* - "@bob:domain.org:3455".getDomain() will return "domain.org:3455"
*/
fun String.getServerName(): String {
if (BuildConfig.DEBUG && !isUserId(this)) {
if (isInDebug && !isUserId(this)) {
// They are some invalid userId localpart in the wild, but the domain part should be there anyway
Timber.w("Not a valid user ID: $this")
}

4
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/RoomId.kt

@ -16,13 +16,13 @@ @@ -16,13 +16,13 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import io.element.android.libraries.androidutils.metadata.isInDebug
import java.io.Serializable
@JvmInline
value class RoomId(val value: String) : Serializable {
init {
if (BuildConfig.DEBUG && !MatrixPatterns.isRoomId(value)) {
if (isInDebug && !MatrixPatterns.isRoomId(value)) {
error("`$value` is not a valid room id.\n Example room id: `!room_id:domain`.")
}
}

4
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/SpaceId.kt

@ -16,13 +16,13 @@ @@ -16,13 +16,13 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import io.element.android.libraries.androidutils.metadata.isInDebug
import java.io.Serializable
@JvmInline
value class SpaceId(val value: String) : Serializable {
init {
if (BuildConfig.DEBUG && !MatrixPatterns.isSpaceId(value)) {
if (isInDebug && !MatrixPatterns.isSpaceId(value)) {
error(
"`$value` is not a valid space id.\n" +
"Space ids are the same as room ids.\n" +

4
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/ThreadId.kt

@ -16,13 +16,13 @@ @@ -16,13 +16,13 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import io.element.android.libraries.androidutils.metadata.isInDebug
import java.io.Serializable
@JvmInline
value class ThreadId(val value: String) : Serializable {
init {
if (BuildConfig.DEBUG && !MatrixPatterns.isThreadId(value)) {
if (isInDebug && !MatrixPatterns.isThreadId(value)) {
error(
"`$value` is not a valid thread id.\n" +
"Thread ids are the same as event ids.\n" +

4
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/core/UserId.kt

@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
package io.element.android.libraries.matrix.api.core
import io.element.android.libraries.matrix.api.BuildConfig
import io.element.android.libraries.androidutils.metadata.isInDebug
import java.io.Serializable
/**
@ -27,7 +27,7 @@ import java.io.Serializable @@ -27,7 +27,7 @@ import java.io.Serializable
@JvmInline
value class UserId(val value: String) : Serializable {
init {
if (BuildConfig.DEBUG && !MatrixPatterns.isUserId(value)) {
if (isInDebug && !MatrixPatterns.isUserId(value)) {
error("`$value` is not a valid user id.\nExample user id: `@name:domain`.")
}
}

6
libraries/matrix/api/src/test/kotlin/io/element/android/libraries/matrix/api/permalink/PermalinkBuilderTest.kt

@ -17,10 +17,10 @@ @@ -17,10 +17,10 @@
package io.element.android.libraries.matrix.api.permalink
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.androidutils.metadata.withReleaseBehavior
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.tests.testutils.assertThrowsInDebug
import io.element.android.tests.testutils.isInDebug
import org.junit.Test
class PermalinkBuilderTest {
@ -40,7 +40,7 @@ class PermalinkBuilderTest { @@ -40,7 +40,7 @@ class PermalinkBuilderTest {
@Test
fun `building a permalink for an invalid user id returns failure when not verifying the id`() {
if (!isInDebug()) {
withReleaseBehavior {
val userId = UserId("some invalid user id")
assertThat(PermalinkBuilder.permalinkForUser(userId).isFailure).isTrue()
}
@ -48,7 +48,7 @@ class PermalinkBuilderTest { @@ -48,7 +48,7 @@ class PermalinkBuilderTest {
@Test
fun `building a permalink for an invalid room id returns failure when not verifying the id`() {
if (!isInDebug()) {
withReleaseBehavior {
val roomId = RoomId("some invalid room id")
assertThat(PermalinkBuilder.permalinkForRoomId(roomId).isFailure).isTrue()
}

1
tests/testutils/build.gradle.kts

@ -30,6 +30,7 @@ dependencies { @@ -30,6 +30,7 @@ dependencies {
implementation(libs.test.junit)
implementation(libs.test.truth)
implementation(libs.coroutines.test)
implementation(projects.libraries.androidutils)
implementation(projects.libraries.architecture)
implementation(projects.libraries.core)
implementation(projects.libraries.uiStrings)

3
tests/testutils/src/main/kotlin/io/element/android/tests/testutils/AssertThrowInDebug.kt

@ -16,13 +16,14 @@ @@ -16,13 +16,14 @@
package io.element.android.tests.testutils
import io.element.android.libraries.androidutils.metadata.isInDebug
import org.junit.Assert.assertThrows
/**
* Assert that the lambda throws only on debug mode.
*/
fun assertThrowsInDebug(lambda: () -> Any?) {
if (BuildConfig.DEBUG) {
if (isInDebug) {
assertThrows(IllegalStateException::class.java) {
lambda()
}

22
tests/testutils/src/main/kotlin/io/element/android/tests/testutils/IsInDebug.kt

@ -1,22 +0,0 @@ @@ -1,22 +0,0 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.tests.testutils
/**
* Returns true if the app is in debug mode.
*/
fun isInDebug() = BuildConfig.DEBUG
Loading…
Cancel
Save