From 4a27646e41b917827dbea9d26740cfe6ae34511f Mon Sep 17 00:00:00 2001 From: Gijs van Veen Date: Tue, 11 Jun 2024 21:51:26 +0200 Subject: [PATCH] Fixes for JVM Some tests still failing. To discuss in ticket --- .github/workflows/pull_request.yml | 22 +++++++++++++ .../kotlin/dev/gitlive/firebase/firebase.kt | 2 +- .../kotlin/dev/gitlive/firebase/auth/auth.kt | 3 +- .../firebase/remoteconfig/RemoteConfig.kt | 4 ++- .../dev/gitlive/firebase/database/database.kt | 8 +++-- .../gitlive/firebase/firestore/firestore.kt | 11 +++++++ .../dev/gitlive/firebase/perf/performance.kt | 6 ++-- .../dev/gitlive/firebase/storage/storage.kt | 6 ++-- test-utils/build.gradle.kts | 11 ++++--- .../kotlin/dev/gitlive/firebase/TestUtils.kt | 31 +++++++++++++++++++ 10 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 firebase-firestore/src/jvmTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt create mode 100644 test-utils/src/jvmMain/kotlin/dev/gitlive/firebase/TestUtils.kt diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 131815967..03dbbeaf5 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -113,3 +113,25 @@ jobs: with: name: "Firebase Debug Log" path: "**/firebase-debug.log" + build-jvm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup test environment + uses: ./.github/actions/setup_test_action + timeout-minutes: 10 + - name: Run JVM Tests + run: ./gradlew cleanTest jvmTest + - name: Upload JVM test artifact + uses: actions/upload-artifact@v3 + if: failure() + with: + name: "JVM Test Report HTML" + path: | + **/build/reports/tests/jvmTest/ + - name: Upload Firebase Debug Log + uses: actions/upload-artifact@v3 + if: failure() + with: + name: "Firebase Debug Log" + path: "**/firebase-debug.log" \ No newline at end of file diff --git a/firebase-app/src/jvmTest/kotlin/dev/gitlive/firebase/firebase.kt b/firebase-app/src/jvmTest/kotlin/dev/gitlive/firebase/firebase.kt index 4bdd21eac..3ab2b6e7e 100644 --- a/firebase-app/src/jvmTest/kotlin/dev/gitlive/firebase/firebase.kt +++ b/firebase-app/src/jvmTest/kotlin/dev/gitlive/firebase/firebase.kt @@ -5,7 +5,7 @@ @file:JvmName("tests") package dev.gitlive.firebase -actual val context: Any = Unit +actual val context: Any = testContext @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) actual annotation class IgnoreForAndroidUnitTest diff --git a/firebase-auth/src/jvmTest/kotlin/dev/gitlive/firebase/auth/auth.kt b/firebase-auth/src/jvmTest/kotlin/dev/gitlive/firebase/auth/auth.kt index d83d421fb..87a640e75 100644 --- a/firebase-auth/src/jvmTest/kotlin/dev/gitlive/firebase/auth/auth.kt +++ b/firebase-auth/src/jvmTest/kotlin/dev/gitlive/firebase/auth/auth.kt @@ -5,10 +5,11 @@ @file:JvmName("tests") package dev.gitlive.firebase.auth +import dev.gitlive.firebase.testContext actual val emulatorHost: String = "10.0.2.2" -actual val context: Any = Unit +actual val context: Any = testContext @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) actual annotation class IgnoreForAndroidUnitTest diff --git a/firebase-config/src/jvmTest/kotlin/dev/gitlive/firebase/remoteconfig/RemoteConfig.kt b/firebase-config/src/jvmTest/kotlin/dev/gitlive/firebase/remoteconfig/RemoteConfig.kt index 5a0333346..d03ab2fb6 100644 --- a/firebase-config/src/jvmTest/kotlin/dev/gitlive/firebase/remoteconfig/RemoteConfig.kt +++ b/firebase-config/src/jvmTest/kotlin/dev/gitlive/firebase/remoteconfig/RemoteConfig.kt @@ -5,7 +5,9 @@ @file:JvmName("tests") package dev.gitlive.firebase.remoteconfig -actual val context: Any = Unit +import dev.gitlive.firebase.testContext + +actual val context: Any = testContext @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) actual annotation class IgnoreForAndroidUnitTest diff --git a/firebase-database/src/jvmTest/kotlin/dev/gitlive/firebase/database/database.kt b/firebase-database/src/jvmTest/kotlin/dev/gitlive/firebase/database/database.kt index 1ddad5625..467e76ad4 100644 --- a/firebase-database/src/jvmTest/kotlin/dev/gitlive/firebase/database/database.kt +++ b/firebase-database/src/jvmTest/kotlin/dev/gitlive/firebase/database/database.kt @@ -5,9 +5,13 @@ @file:JvmName("tests") package dev.gitlive.firebase.database -actual val emulatorHost: String = "10.0.2.2" +import dev.gitlive.firebase.testContext -actual val context: Any = Unit +actual val emulatorHost: String = "localhost" + +actual val context: Any = testContext @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) actual annotation class IgnoreForAndroidUnitTest +@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) +actual annotation class IgnoreForAndroidTest diff --git a/firebase-firestore/src/jvmTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt b/firebase-firestore/src/jvmTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt new file mode 100644 index 000000000..742bd4e9b --- /dev/null +++ b/firebase-firestore/src/jvmTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt @@ -0,0 +1,11 @@ +package dev.gitlive.firebase.firestore + +import dev.gitlive.firebase.testContext + +actual val emulatorHost: String = "localhost" + +actual val context: Any = testContext + +@Suppress("UNCHECKED_CAST") +actual fun encodedAsMap(encoded: Any?): Map = encoded as Map +actual fun Map.asEncoded(): Any = this diff --git a/firebase-perf/src/jvmTest/kotlin/dev/gitlive/firebase/perf/performance.kt b/firebase-perf/src/jvmTest/kotlin/dev/gitlive/firebase/perf/performance.kt index f8134eecb..0985f3b1f 100644 --- a/firebase-perf/src/jvmTest/kotlin/dev/gitlive/firebase/perf/performance.kt +++ b/firebase-perf/src/jvmTest/kotlin/dev/gitlive/firebase/perf/performance.kt @@ -5,9 +5,11 @@ @file:JvmName("tests") package dev.gitlive.firebase.perf -actual val emulatorHost: String = "10.0.2.2" +import dev.gitlive.firebase.testContext -actual val context: Any = Unit +actual val emulatorHost: String = "localhost" + +actual val context: Any = testContext @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) actual annotation class IgnoreForAndroidUnitTest diff --git a/firebase-storage/src/jvmTest/kotlin/dev/gitlive/firebase/storage/storage.kt b/firebase-storage/src/jvmTest/kotlin/dev/gitlive/firebase/storage/storage.kt index a030f38bd..022903848 100644 --- a/firebase-storage/src/jvmTest/kotlin/dev/gitlive/firebase/storage/storage.kt +++ b/firebase-storage/src/jvmTest/kotlin/dev/gitlive/firebase/storage/storage.kt @@ -5,9 +5,11 @@ @file:JvmName("tests") package dev.gitlive.firebase.storage -actual val emulatorHost: String = "10.0.2.2" +import dev.gitlive.firebase.testContext -actual val context: Any = Unit +actual val emulatorHost: String = "localhost" + +actual val context: Any = testContext @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) actual annotation class IgnoreForAndroidUnitTest diff --git a/test-utils/build.gradle.kts b/test-utils/build.gradle.kts index b7e052b39..f5c40d64d 100644 --- a/test-utils/build.gradle.kts +++ b/test-utils/build.gradle.kts @@ -109,14 +109,17 @@ kotlin { } } - getByName("jvmMain") { - kotlin.srcDir("src/androidMain/kotlin") - } - getByName("jsMain") { dependencies { implementation(kotlin("test-js")) } } + + getByName("jvmMain") { + dependencies { + val coroutinesVersion: String by project + api("org.jetbrains.kotlinx:kotlinx-coroutines-swing:$coroutinesVersion") + } + } } } diff --git a/test-utils/src/jvmMain/kotlin/dev/gitlive/firebase/TestUtils.kt b/test-utils/src/jvmMain/kotlin/dev/gitlive/firebase/TestUtils.kt new file mode 100644 index 000000000..56ff3e31f --- /dev/null +++ b/test-utils/src/jvmMain/kotlin/dev/gitlive/firebase/TestUtils.kt @@ -0,0 +1,31 @@ +@file:JvmName("TestUtilsJvm") +/* + * Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.gitlive.firebase + +import android.app.Application +import com.google.firebase.FirebasePlatform +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.runBlocking +import kotlin.time.Duration.Companion.minutes + +val testContext = Application().apply { + FirebasePlatform.initializeFirebasePlatform(object : FirebasePlatform() { + val storage = mutableMapOf() + override fun store(key: String, value: String) = storage.set(key, value) + override fun retrieve(key: String) = storage[key] + override fun clear(key: String) { storage.remove(key) } + override fun log(msg: String) = println(msg) + }) +} + +actual fun runTest(test: suspend CoroutineScope.() -> Unit) = kotlinx.coroutines.test.runTest(timeout = 5.minutes) { test() } +actual fun runBlockingTest(action: suspend CoroutineScope.() -> Unit) = runBlocking(block = action) + +actual fun nativeMapOf(vararg pairs: Pair): Any = mapOf(*pairs) +actual fun nativeListOf(vararg elements: Any?): Any = listOf(*elements) +actual fun nativeAssertEquals(expected: Any?, actual: Any?) { + kotlin.test.assertEquals(expected, actual) +} \ No newline at end of file