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>
This commit is contained in:
committed by
GitHub
parent
62799e1854
commit
eabdb9ae4d
@@ -20,6 +20,10 @@ plugins {
|
||||
|
||||
android {
|
||||
namespace = "io.element.android.libraries.androidutils"
|
||||
|
||||
buildFeatures {
|
||||
buildConfig = true
|
||||
}
|
||||
}
|
||||
|
||||
anvil {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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`.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
* - "@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")
|
||||
}
|
||||
|
||||
@@ -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`.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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" +
|
||||
|
||||
@@ -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" +
|
||||
|
||||
@@ -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
|
||||
@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`.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@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 {
|
||||
|
||||
@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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user