Skip to content

Don't depend on apollo-testing-support-internal #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ atomicfu = "0.23.1" # Must be the same version as the one used by apollo-testing
sqldelight = "2.0.1"
androidx-sqlite = "2.3.1"
librarian = "0.0.8-SNAPSHOT-cd822254de75426f8f047a50bae467da872cc912"
kotlinx-coroutines = "1.9.0"

[libraries]
apollo-api = { group = "com.apollographql.apollo", name = "apollo-api", version.ref = "apollo" }
Expand All @@ -24,6 +25,7 @@ atomicfu-plugin = { group = "org.jetbrains.kotlinx", name = "atomicfu-gradle-plu
kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test" } # the Kotlin plugin resolves the version
kotlin-test-junit = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit" } # the Kotlin plugin resolves the version
okio = { group = "com.squareup.okio", name = "okio", version.ref = "okio" }
okio-nodefilesystem = { group = "com.squareup.okio", name = "okio-nodefilesystem", version.ref = "okio" }
uuid = "com.benasher44:uuid:0.8.2"
sqldelight-android = { group = "app.cash.sqldelight", name = "android-driver", version.ref = "sqldelight" }
sqldelight-jvm = { group = "app.cash.sqldelight", name = "sqlite-driver", version.ref = "sqldelight" }
Expand All @@ -37,6 +39,7 @@ androidx-sqlite-framework = { group = "androidx.sqlite", name = "sqlite-framewor
androidx-startup-runtime = "androidx.startup:startup-runtime:1.1.1"
kotlin-poet = { group = "com.squareup", name = "kotlinpoet", version = "1.18.1" }
kotlin-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin-plugin" }
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
librarian = { module = "com.gradleup.librarian:librarian-gradle-plugin", version.ref = "librarian"}
android-plugin = { module = "com.android.tools.build:gradle", version.ref = "android-plugin"}
sqldelight-plugin = { module = "app.cash.sqldelight:gradle-plugin", version.ref = "sqldelight"}
Expand Down
2 changes: 1 addition & 1 deletion normalized-cache-incubating/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ kotlin {
getByName("commonTest") {
dependencies {
implementation(libs.kotlin.test)
implementation(libs.apollo.testing.support)
implementation(project(":test-utils"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.apollographql.cache.normalized.internal

import com.apollographql.apollo.testing.internal.runTest
import com.apollographql.cache.normalized.memory.internal.LruCache
import com.apollographql.cache.normalized.testing.runTest
import kotlinx.coroutines.delay
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
1 change: 0 additions & 1 deletion normalized-cache-sqlite-incubating/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ kotlin {
getByName("commonTest") {
dependencies {
implementation(libs.kotlin.test)
implementation(libs.apollo.testing.support)
implementation(project(":test-utils"))
}
}
Expand Down
45 changes: 45 additions & 0 deletions test-utils/api/test-utils.api
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
public final class com/apollographql/cache/normalized/testing/-FileSystemCommon {
public static final fun pathToJsonReader (Ljava/lang/String;)Lcom/apollographql/apollo/api/json/JsonReader;
public static final fun pathToUtf8 (Ljava/lang/String;)Ljava/lang/String;
}

public final class com/apollographql/cache/normalized/testing/CacheKeyKt {
public static final fun append-eNSUWrY (Ljava/lang/String;[Ljava/lang/String;)Ljava/lang/String;
public static final fun fieldKey-eNSUWrY (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
public static final fun keyToString-pWl1Des (Ljava/lang/String;)Ljava/lang/String;
}

public final class com/apollographql/cache/normalized/testing/ChannelsKt {
public static final fun assertNoElement (Lkotlinx/coroutines/channels/Channel;JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun assertNoElement$default (Lkotlinx/coroutines/channels/Channel;JLkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun awaitElement (Lkotlinx/coroutines/channels/Channel;JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun awaitElement$default (Lkotlinx/coroutines/channels/Channel;JLkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
}

public final class com/apollographql/cache/normalized/testing/CurrentThreadId_jvmCommonKt {
public static final fun currentThreadId ()Ljava/lang/String;
}

public final class com/apollographql/cache/normalized/testing/ErrorsKt {
public static final fun assertErrorsEquals (Lcom/apollographql/apollo/api/Error;Lcom/apollographql/apollo/api/Error;)V
public static final fun assertErrorsEquals (Ljava/lang/Iterable;Ljava/lang/Iterable;)V
}

public final class com/apollographql/cache/normalized/testing/Platform : java/lang/Enum {
public static final field Js Lcom/apollographql/cache/normalized/testing/Platform;
public static final field Jvm Lcom/apollographql/cache/normalized/testing/Platform;
public static final field Native Lcom/apollographql/cache/normalized/testing/Platform;
public static final field WasmJs Lcom/apollographql/cache/normalized/testing/Platform;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/apollographql/cache/normalized/testing/Platform;
public static fun values ()[Lcom/apollographql/cache/normalized/testing/Platform;
}

public final class com/apollographql/cache/normalized/testing/Platform_jvmCommonKt {
public static final fun platform ()Lcom/apollographql/cache/normalized/testing/Platform;
}

public final class com/apollographql/cache/normalized/testing/ReadFile_concurrentKt {
public static final fun getHostFileSystem ()Lokio/FileSystem;
}

public final class com/apollographql/cache/normalized/testing/ReadFile_jvmKt {
public static final fun getTestsPath ()Ljava/lang/String;
public static final fun shouldUpdateTestFixtures ()Z
}

public final class com/apollographql/cache/normalized/testing/RunTestKt {
public static final fun runTest (Lkotlin/jvm/functions/Function2;)V
public static final fun runTest (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)V
public static synthetic fun runTest$default (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
}

38 changes: 38 additions & 0 deletions test-utils/api/test-utils.klib.api
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
// Klib ABI Dump
// Targets: [iosArm64, iosSimulatorArm64, iosX64, js, macosArm64, macosX64, tvosArm64, tvosSimulatorArm64, tvosX64, wasmJs, watchosArm32, watchosArm64, watchosSimulatorArm64]
// Alias: apple => [iosArm64, iosSimulatorArm64, iosX64, macosArm64, macosX64, tvosArm64, tvosSimulatorArm64, tvosX64, watchosArm32, watchosArm64, watchosSimulatorArm64]
// Rendering settings:
// - Signature version: 2
// - Show manifest properties: true
// - Show declarations: true

// Library unique name: <com.apollographql.cache:test-utils>
final enum class com.apollographql.cache.normalized.testing/Platform : kotlin/Enum<com.apollographql.cache.normalized.testing/Platform> { // com.apollographql.cache.normalized.testing/Platform|null[0]
enum entry Js // com.apollographql.cache.normalized.testing/Platform.Js|null[0]
enum entry Jvm // com.apollographql.cache.normalized.testing/Platform.Jvm|null[0]
enum entry Native // com.apollographql.cache.normalized.testing/Platform.Native|null[0]
enum entry WasmJs // com.apollographql.cache.normalized.testing/Platform.WasmJs|null[0]

final val entries // com.apollographql.cache.normalized.testing/Platform.entries|#static{}entries[0]
final fun <get-entries>(): kotlin.enums/EnumEntries<com.apollographql.cache.normalized.testing/Platform> // com.apollographql.cache.normalized.testing/Platform.entries.<get-entries>|<get-entries>#static(){}[0]

final fun valueOf(kotlin/String): com.apollographql.cache.normalized.testing/Platform // com.apollographql.cache.normalized.testing/Platform.valueOf|valueOf#static(kotlin.String){}[0]
final fun values(): kotlin/Array<com.apollographql.cache.normalized.testing/Platform> // com.apollographql.cache.normalized.testing/Platform.values|values#static(){}[0]
}

final val com.apollographql.cache.normalized.testing/HostFileSystem // com.apollographql.cache.normalized.testing/HostFileSystem|{}HostFileSystem[0]
final fun <get-HostFileSystem>(): okio/FileSystem // com.apollographql.cache.normalized.testing/HostFileSystem.<get-HostFileSystem>|<get-HostFileSystem>(){}[0]
final val com.apollographql.cache.normalized.testing/testsPath // com.apollographql.cache.normalized.testing/testsPath|{}testsPath[0]
final fun <get-testsPath>(): kotlin/String // com.apollographql.cache.normalized.testing/testsPath.<get-testsPath>|<get-testsPath>(){}[0]

final fun (com.apollographql.cache.normalized.api/CacheKey).com.apollographql.cache.normalized.testing/append(kotlin/Array<out kotlin/String>...): com.apollographql.cache.normalized.api/CacheKey // com.apollographql.cache.normalized.testing/append|append@com.apollographql.cache.normalized.api.CacheKey(kotlin.Array<out|kotlin.String>...){}[0]
final fun (com.apollographql.cache.normalized.api/CacheKey).com.apollographql.cache.normalized.testing/fieldKey(kotlin/String): kotlin/String // com.apollographql.cache.normalized.testing/fieldKey|fieldKey@com.apollographql.cache.normalized.api.CacheKey(kotlin.String){}[0]
final fun (com.apollographql.cache.normalized.api/CacheKey).com.apollographql.cache.normalized.testing/keyToString(): kotlin/String // com.apollographql.cache.normalized.testing/keyToString|keyToString@com.apollographql.cache.normalized.api.CacheKey(){}[0]
final fun com.apollographql.cache.normalized.testing/assertErrorsEquals(com.apollographql.apollo.api/Error?, com.apollographql.apollo.api/Error?) // com.apollographql.cache.normalized.testing/assertErrorsEquals|assertErrorsEquals(com.apollographql.apollo.api.Error?;com.apollographql.apollo.api.Error?){}[0]
final fun com.apollographql.cache.normalized.testing/assertErrorsEquals(kotlin.collections/Iterable<com.apollographql.apollo.api/Error>?, kotlin.collections/Iterable<com.apollographql.apollo.api/Error>?) // com.apollographql.cache.normalized.testing/assertErrorsEquals|assertErrorsEquals(kotlin.collections.Iterable<com.apollographql.apollo.api.Error>?;kotlin.collections.Iterable<com.apollographql.apollo.api.Error>?){}[0]
final fun com.apollographql.cache.normalized.testing/currentThreadId(): kotlin/String // com.apollographql.cache.normalized.testing/currentThreadId|currentThreadId(){}[0]
final fun com.apollographql.cache.normalized.testing/pathToJsonReader(kotlin/String): com.apollographql.apollo.api.json/JsonReader // com.apollographql.cache.normalized.testing/pathToJsonReader|pathToJsonReader(kotlin.String){}[0]
final fun com.apollographql.cache.normalized.testing/pathToUtf8(kotlin/String): kotlin/String // com.apollographql.cache.normalized.testing/pathToUtf8|pathToUtf8(kotlin.String){}[0]
final fun com.apollographql.cache.normalized.testing/platform(): com.apollographql.cache.normalized.testing/Platform // com.apollographql.cache.normalized.testing/platform|platform(){}[0]
final fun com.apollographql.cache.normalized.testing/shouldUpdateTestFixtures(): kotlin/Boolean // com.apollographql.cache.normalized.testing/shouldUpdateTestFixtures|shouldUpdateTestFixtures(){}[0]
final suspend fun <#A: kotlin/Any?> (kotlinx.coroutines.channels/Channel<#A>).com.apollographql.cache.normalized.testing/assertNoElement(kotlin/Long = ...) // com.apollographql.cache.normalized.testing/assertNoElement|assertNoElement@kotlinx.coroutines.channels.Channel<0:0>(kotlin.Long){0§<kotlin.Any?>}[0]
final suspend fun <#A: kotlin/Any?> (kotlinx.coroutines.channels/Channel<#A>).com.apollographql.cache.normalized.testing/awaitElement(kotlin/Long = ...): #A // com.apollographql.cache.normalized.testing/awaitElement|awaitElement@kotlinx.coroutines.channels.Channel<0:0>(kotlin.Long){0§<kotlin.Any?>}[0]

// Targets: [apple]
final fun com.apollographql.cache.normalized.testing/runTest(kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit> = ..., kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit> = ..., kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit>) // com.apollographql.cache.normalized.testing/runTest|runTest(kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>){}[0]

// Targets: [apple]
final fun com.apollographql.cache.normalized.testing/runTest(kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit>) // com.apollographql.cache.normalized.testing/runTest|runTest(kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>){}[0]

// Targets: [js, wasmJs]
final fun com.apollographql.cache.normalized.testing/runTest(kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit> = ..., kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit> = ..., kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit>): kotlinx.coroutines.test.internal/JsPromiseInterfaceForTesting // com.apollographql.cache.normalized.testing/runTest|runTest(kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>){}[0]

// Targets: [js, wasmJs]
final fun com.apollographql.cache.normalized.testing/runTest(kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, kotlin/Unit>): kotlinx.coroutines.test.internal/JsPromiseInterfaceForTesting // com.apollographql.cache.normalized.testing/runTest|runTest(kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>){}[0]
6 changes: 6 additions & 0 deletions test-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ kotlin {
sourceSets {
getByName("commonMain") {
dependencies {
api(libs.kotlinx.coroutines.test)
api(project(":normalized-cache-incubating"))
implementation(libs.kotlin.test)
}
}
getByName("jsMain") {
dependencies {
api(libs.okio.nodefilesystem)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.apollographql.cache.normalized.testing

import kotlinx.cinterop.ExperimentalForeignApi
import platform.posix.pthread_self

@OptIn(ExperimentalForeignApi::class)
actual fun currentThreadId(): String {
return pthread_self()?.rawValue.toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@file:Suppress("DEPRECATION")

package com.apollographql.cache.normalized.testing

/**
* The current platform. This is used from tests because Double.toString() doesn't behave the same on JS and other platforms.
* Prefer more specific functions like `assertMainThreadOnNative` when possible instead of checking the platform.
*/
actual fun platform() = Platform.Native
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.apollographql.cache.normalized.testing

actual fun shouldUpdateTestFixtures(): Boolean = false

actual val testsPath: String = "../"
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@file:JvmName("-FileSystemCommon")

package com.apollographql.cache.normalized.testing

import com.apollographql.apollo.api.json.JsonReader
import com.apollographql.apollo.api.json.jsonReader
import okio.Path
import okio.Path.Companion.toPath
import okio.buffer
import kotlin.jvm.JvmName


private fun String.toTestsPath(): Path {
@Suppress("DEPRECATION")
return testsPath.toPath().resolve(this.toPath())
}

/**
* @param path: the path to the file, from the "tests" directory
*/
fun pathToUtf8(path: String): String {
@Suppress("DEPRECATION")
return HostFileSystem.openReadOnly(path.toTestsPath()).source().buffer().readUtf8()
}

/**
* @param path: the path to the file, from the "tests" directory
*/
fun pathToJsonReader(path: String): JsonReader {
@Suppress("DEPRECATION")
return HostFileSystem.openReadOnly(path.toTestsPath()).source().buffer().jsonReader()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.apollographql.cache.normalized.testing

import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.withTimeout

suspend fun <T> Channel<T>.awaitElement(timeoutMillis: Long = 30000) = withTimeout(timeoutMillis) {
receive()
}

suspend fun <T> Channel<T>.assertNoElement(timeoutMillis: Long = 300): Unit {
try {
withTimeout(timeoutMillis) {
receive()
}
error("An item was unexpectedly received")
} catch (_: TimeoutCancellationException) {
// nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.apollographql.cache.normalized.testing

expect fun currentThreadId(): String
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@file:Suppress("DEPRECATION")

package com.apollographql.cache.normalized.testing

enum class Platform {
Jvm,
Native,
Js,
WasmJs
}

expect fun platform(): Platform
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.apollographql.cache.normalized.testing

import okio.FileSystem

/**
* The host filesystem
*/
expect val HostFileSystem: FileSystem

expect fun shouldUpdateTestFixtures(): Boolean

/**
* The path to the "tests" directory. This assumes all tests are run from a predictable place relative to "tests"
* We need this for JS tests where the CWD is not properly set at the beginning of tests
*/
expect val testsPath: String

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.apollographql.cache.normalized.testing

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.withContext

fun runTest(
block: suspend CoroutineScope.() -> Unit,
) = runTest({}, {}, block)

fun runTest(
before: suspend CoroutineScope.() -> Unit = {},
after: suspend CoroutineScope.() -> Unit = {},
block: suspend CoroutineScope.() -> Unit,
): TestResult {
return kotlinx.coroutines.test.runTest {
withContext(Dispatchers.Default.limitedParallelism(1)) {
before()
block()
after()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.apollographql.cache.normalized.testing

import okio.FileSystem
import okio.SYSTEM

/**
* The host filesystem
*/
actual val HostFileSystem: FileSystem
get() = FileSystem.SYSTEM
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.apollographql.cache.normalized.testing

actual fun currentThreadId(): String {
return "js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@file:Suppress("DEPRECATION")

package com.apollographql.cache.normalized.testing

/**
* The current platform. This is used from tests because Double.toString() doesn't behave the same on JS and other platforms.
* Prefer more specific functions like `assertMainThreadOnNative` when possible instead of checking the platform.
*/
actual fun platform() = Platform.Js

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.apollographql.cache.normalized.testing

import okio.FileSystem
import okio.NodeJsFileSystem

actual val HostFileSystem: FileSystem = NodeJsFileSystem

actual fun shouldUpdateTestFixtures(): Boolean = false

// Workaround for https://youtrack.jetbrains.com/issue/KT-49125
actual val testsPath: String = "../../../../../tests/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.apollographql.cache.normalized.testing

actual fun currentThreadId(): String {
@Suppress("DEPRECATION")
return Thread.currentThread().id.toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@file:Suppress("DEPRECATION")

package com.apollographql.cache.normalized.testing

/**
* The current platform. This is used from tests because Double.toString() doesn't behave the same on JS and other platforms.
* Prefer more specific functions like `assertMainThreadOnNative` when possible instead of checking the platform.
*/
actual fun platform() = Platform.Jvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.apollographql.cache.normalized.testing

actual fun shouldUpdateTestFixtures(): Boolean {
if (System.getenv("updateTestFixtures") != null) {
return true
}

return when (System.getProperty("updateTestFixtures")?.trim()) {
"on", "true", "1" -> true
else -> false
}
}

actual val testsPath: String = "../"
Loading