Skip to content

Commit 57e7b80

Browse files
committed
generate a cached-responses file on-demand
1 parent 1688435 commit 57e7b80

File tree

373 files changed

+185
-10252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

373 files changed

+185
-10252
lines changed

build.gradle.kts

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@file:Suppress("UnstableApiUsage")
22

3+
import com.fasterxml.jackson.databind.json.JsonMapper
34
import org.jetbrains.dokka.gradle.DokkaTask
45
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType
56
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
@@ -190,6 +191,18 @@ kotlin {
190191
}
191192
}
192193

194+
val commonJvmLikeTest by creating {
195+
dependencies {
196+
implementation(kotlin("test-junit"))
197+
implementation("com.sparkjava:spark-core:$sparkVersion")
198+
runtimeOnly(kotlin("reflect"))
199+
}
200+
}
201+
202+
val commonNonJvmTargetsTest by creating {
203+
dependsOn(commonTest)
204+
}
205+
193206
val jvmMain by getting {
194207
dependsOn(commonJvmLikeMain)
195208
repositories {
@@ -202,11 +215,7 @@ kotlin {
202215
}
203216

204217
val jvmTest by getting {
205-
dependencies {
206-
implementation(kotlin("test-junit"))
207-
implementation("com.sparkjava:spark-core:$sparkVersion")
208-
runtimeOnly(kotlin("reflect"))
209-
}
218+
dependsOn(commonJvmLikeTest)
210219
}
211220

212221
val jsMain by getting {
@@ -217,6 +226,8 @@ kotlin {
217226
}
218227

219228
val jsTest by getting {
229+
dependsOn(commonNonJvmTargetsTest)
230+
220231
dependencies {
221232
implementation(kotlin("test-js"))
222233
}
@@ -242,11 +253,7 @@ kotlin {
242253
}
243254

244255
val androidTest by getting {
245-
dependencies {
246-
implementation(kotlin("test-junit"))
247-
implementation("com.sparkjava:spark-core:$sparkVersion")
248-
runtimeOnly(kotlin("reflect"))
249-
}
256+
dependsOn(commonJvmLikeTest)
250257
}
251258

252259
// as kotlin/native, they require special ktor versions
@@ -267,7 +274,7 @@ kotlin {
267274
}
268275

269276
// desktop targets
270-
val desktopTest by creating { dependsOn(commonTest) }
277+
val desktopTest by creating { dependsOn(commonNonJvmTargetsTest) }
271278
val linuxX64Main by getting { dependsOn(desktopMain) }
272279
val linuxX64Test by getting { dependsOn(desktopTest) }
273280
val mingwX64Main by getting { dependsOn(desktopMain) }
@@ -276,7 +283,7 @@ kotlin {
276283
val macosX64Test by getting { dependsOn(desktopTest) }
277284

278285
// darwin targets
279-
val nativeDarwinTest by creating { dependsOn(commonTest) }
286+
val nativeDarwinTest by creating { dependsOn(commonNonJvmTargetsTest) }
280287
val iosMain by getting { dependsOn(nativeDarwinMain) }
281288
val iosTest by getting { dependsOn(nativeDarwinTest) }
282289

@@ -425,3 +432,34 @@ signing {
425432
sign(publishing.publications)
426433
}
427434
}
435+
436+
// Test tasks
437+
tasks.register("updateNonJvmTestFakes") {
438+
if (System.getenv("SPOTIFY_TOKEN_STRING") == null
439+
|| System.getenv("SHOULD_RECACHE_RESPONSES")?.toBoolean() != true
440+
) {
441+
return@register
442+
}
443+
444+
dependsOn("jvmTest")
445+
val responseCacheDir =
446+
System.getenv("RESPONSE_CACHE_DIR")?.let { File(it) }
447+
?: throw IllegalArgumentException("No response cache directory provided")
448+
val commonTestResourcesSource = projectDir.resolve("src/commonTest/resources")
449+
if (!commonTestResourcesSource.exists()) commonTestResourcesSource.mkdir()
450+
451+
val commonTestResourceFileToSet = commonTestResourcesSource.resolve("cached_responses.json")
452+
453+
if (commonTestResourceFileToSet.exists()) commonTestResourceFileToSet.delete()
454+
commonTestResourceFileToSet.createNewFile()
455+
456+
val testToOrderedResponseMap: Map<String, List<String>> = responseCacheDir.walk()
457+
.filter { it.isFile && it.name.matches("http_request_\\d+.txt".toRegex()) }
458+
.groupBy { "${it.parentFile.parentFile.name}.${it.parentFile.name}" }
459+
.map { (key, group) -> key to group.sorted().map { it.readText() } }
460+
.toMap()
461+
462+
val jsonLiteral = JsonMapper().writeValueAsString(testToOrderedResponseMap)
463+
commonTestResourceFileToSet.writeText(jsonLiteral)
464+
println(commonTestResourceFileToSet.absolutePath)
465+
}

gradle.properties

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
1-
## For more details on how to configure your build environment visit
2-
# http://www.gradle.org/docs/current/userguide/build_environment.html
3-
#
4-
# Specifies the JVM arguments used for the daemon process.
5-
# The setting is particularly useful for tweaking memory settings.
6-
# Default value: -Xmx1024m -XX:MaxPermSize=256m
7-
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
8-
#
9-
# When configured, Gradle will run in incubating parallel mode.
10-
# This option should only be used with decoupled projects. More details, visit
11-
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
12-
# org.gradle.parallel=true
13-
#Mon Aug 09 00:44:27 PDT 2021
141
systemProp.org.gradle.internal.publish.checksums.insecure=true
2+
153
org.gradle.daemon=true
16-
kotlin.js.generate.externals=true
174
org.gradle.jvmargs=-Xmx8000m
18-
kotlin.native.enableDependencyPropagation=false
5+
6+
# android target settings
197
android.useAndroidX=true
208
android.enableJetifier=true
21-
kotlin.mpp.enableGranularSourceSetsMetadata=true
9+
android.disableAutomaticComponentCreation=false
10+
11+
# language dependencies
2212
kotlinVersion=1.7.21
13+
14+
# library dependencies
2315
kotlinxDatetimeVersion=0.4.0
24-
androidBuildToolsVersion=7.2.0
2516
kotlinxSerializationVersion=1.4.1
2617
ktorVersion=2.1.3
27-
sparkVersion=2.9.4
2818
korlibsVersion=3.4.0
2919
kotlinxCoroutinesVersion=1.6.4
20+
21+
androidBuildToolsVersion=7.2.0
3022
androidSpotifyAuthVersion=1.2.5
3123
androidCryptoVersion=1.0.0
32-
androidxCompatVersion=1.5.1
24+
androidxCompatVersion=1.5.1
25+
26+
sparkVersion=2.9.4

gradle/wrapper/gradle-wrapper.jar

285 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

karma.config.d/karma.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/androidTest/kotlin/com/adamratzman/spotify/CommonImpl.kt

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/jvmTest/kotlin/com/adamratzman/spotify/CommonImpl.kt renamed to src/commonJvmLikeTest/kotlin/com/adamratzman/spotify/CommonImpl.kt

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import kotlinx.serialization.Serializable
88
import kotlinx.serialization.encodeToString
99
import kotlinx.serialization.json.Json
1010

11-
val cacheLocation = System.getenv("RESPONSE_CACHE_DIR")
11+
val cacheLocation: String? = System.getenv("RESPONSE_CACHE_DIR")
12+
val shouldRecacheRequests: Boolean = System.getenv("SHOULD_RECACHE_RESPONSES")?.toBoolean() == true
13+
1214
actual fun getTestClientId(): String? = System.getenv("SPOTIFY_CLIENT_ID")
1315
actual fun getTestClientSecret(): String? = System.getenv("SPOTIFY_CLIENT_SECRET")
1416
actual fun getTestRedirectUri(): String? = System.getenv("SPOTIFY_REDIRECT_URI")
@@ -17,7 +19,7 @@ actual fun isHttpLoggingEnabled(): Boolean = System.getenv("SPOTIFY_LOG_HTTP") =
1719
actual fun arePlayerTestsEnabled(): Boolean = System.getenv("SPOTIFY_ENABLE_PLAYER_TESTS")?.toBoolean() == true
1820
actual fun areLivePkceTestsEnabled(): Boolean = System.getenv("VERBOSE_TEST_ENABLED")?.toBoolean() ?: false
1921

20-
actual suspend fun buildSpotifyApi(): GenericSpotifyApi? {
22+
actual suspend fun buildSpotifyApi(testClassQualifiedName: String, testName: String): GenericSpotifyApi? {
2123
val clientId = getTestClientId()
2224
val clientSecret = getTestClientSecret()
2325
val tokenString = getTestTokenString()
@@ -58,54 +60,50 @@ actual suspend fun buildSpotifyApi(): GenericSpotifyApi? {
5860
}
5961
}
6062

61-
private val json = Json { prettyPrint = true }
62-
63-
actual fun getResponseCacher(): ResponseCacher? {
64-
if (cacheLocation == null) return null
65-
66-
return object : ResponseCacher {
67-
override val cachedResponsesDirectoryPath: String = cacheLocation
68-
69-
override fun cacheResponse(
70-
className: String,
71-
testName: String,
72-
responseNumber: Int,
73-
request: HttpRequest,
74-
response: HttpResponse
75-
) {
76-
val baseDirectory = File(cacheLocation)
77-
if (!baseDirectory.exists()) baseDirectory.mkdirs()
78-
val testDirectory = File(baseDirectory.absolutePath + "/$className/$testName")
79-
if (!testDirectory.exists()) testDirectory.mkdirs()
80-
81-
val responseFile = File(testDirectory.absolutePath + "/http_request_$responseNumber.txt")
82-
if (responseFile.exists()) responseFile.delete()
83-
responseFile.createNewFile()
84-
85-
val objToWrite = CachedResponse(
86-
Request(
87-
request.url,
88-
request.method.toString(),
89-
request.bodyString
90-
),
91-
Response(
92-
response.responseCode,
93-
response.headers.associate { it.key to it.value },
94-
response.body
95-
)
96-
)
9763

98-
responseFile.appendText(json.encodeToString(objToWrite))
99-
}
64+
object JvmResponseCacher : ResponseCacher {
65+
override val cachedResponsesDirectoryPath: String = cacheLocation ?: ""
66+
private val json = Json { prettyPrint = true }
67+
private val baseDirectory = File(cacheLocation)
10068

69+
init {
70+
if (baseDirectory.exists()) baseDirectory.deleteRecursively()
71+
baseDirectory.mkdirs()
10172
}
102-
}
10373

104-
@Serializable
105-
data class CachedResponse(val request: Request, val response: Response)
74+
override fun cacheResponse(
75+
className: String,
76+
testName: String,
77+
responseNumber: Int,
78+
request: HttpRequest,
79+
response: HttpResponse
80+
) {
81+
val testDirectory = File(baseDirectory.absolutePath + "/$className/$testName")
82+
if (!testDirectory.exists()) testDirectory.mkdirs()
83+
84+
val responseFile = File(testDirectory.absolutePath + "/http_request_$responseNumber.txt")
85+
if (responseFile.exists()) responseFile.delete()
86+
responseFile.createNewFile()
87+
88+
val objToWrite = CachedResponse(
89+
Request(
90+
request.url,
91+
request.method.toString(),
92+
request.bodyString
93+
),
94+
Response(
95+
response.responseCode,
96+
response.headers.associate { it.key to it.value },
97+
response.body
98+
)
99+
)
106100

107-
@Serializable
108-
data class Request(val url: String, val method: String, val body: String? = null)
101+
responseFile.appendText(json.encodeToString(objToWrite))
102+
}
109103

110-
@Serializable
111-
data class Response(val responseCode: Int, val headers: Map<String, String>, val body: String)
104+
}
105+
106+
actual fun getResponseCacher(): ResponseCacher? {
107+
if (cacheLocation == null || !shouldRecacheRequests) return null
108+
return JvmResponseCacher
109+
}

src/commonMain/kotlin/com.adamratzman.spotify/http/HttpRequest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ public class HttpRequest constructor(
128128
}
129129

130130
val body: String = response.bodyAsText()
131-
if (api?.spotifyApiOptions?.enableDebugMode == true) Console.debug("Request body: $body")
131+
if (api?.spotifyApiOptions?.enableDebugMode == true) {
132+
Console.debug("Request status: $respCode - body: $body")
133+
}
132134

133135
if (respCode == 401 && body.contains("access token") && api?.spotifyApiOptions?.automaticRefresh == true) {
134136
api.refreshToken()

src/desktopTest/kotlin/com.adamratzman.spotify/CommonImpl.kt renamed to src/commonNonJvmTargetsTest/kotlin/com.adamratzman.spotify/CommonImpl.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package com.adamratzman.spotify
33

44
actual fun areLivePkceTestsEnabled(): Boolean = false
55
actual fun arePlayerTestsEnabled(): Boolean = false
6-
6+
actual fun isHttpLoggingEnabled(): Boolean = false
7+
actual fun getTestTokenString(): String? = null
8+
actual fun getTestRedirectUri(): String? = null
79
actual fun getTestClientId(): String? = null
810
actual fun getTestClientSecret(): String? = null
911
actual fun getResponseCacher(): ResponseCacher? = null
1012

11-
actual suspend fun buildSpotifyApi(): GenericSpotifyApi? = null
13+
actual suspend fun buildSpotifyApi(testClassQualifiedName: String, testName: String): GenericSpotifyApi? = null

0 commit comments

Comments
 (0)