From 05037d851df368414f978180980602b1bcf9650a Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Mon, 13 Nov 2023 10:49:23 -0500 Subject: [PATCH] Add Spotless (#123) --- .editorconfig | 3 + Dockerfile | 2 +- build.gradle | 11 + gradle/libs.versions.toml | 3 + src/main/kotlin/watch/dependency/config.kt | 58 ++-- src/main/kotlin/watch/dependency/database.kt | 4 +- .../watch/dependency/dependencyAwait.kt | 30 +- .../watch/dependency/dependencyNotifier.kt | 2 +- src/main/kotlin/watch/dependency/main.kt | 21 +- src/main/kotlin/watch/dependency/maven.kt | 2 +- src/main/kotlin/watch/dependency/notify.kt | 2 +- src/main/kotlin/watch/dependency/util.kt | 8 +- .../kotlin/watch/dependency/ConfigTest.kt | 297 +++++++++++++----- .../kotlin/watch/dependency/DatabaseTest.kt | 2 +- .../dependency/DependencyNotifierTest.kt | 72 +++-- .../dependency/HttpMaven2RepositoryTest.kt | 34 +- src/test/kotlin/watch/dependency/testUtil.kt | 3 - 17 files changed, 369 insertions(+), 185 deletions(-) diff --git a/.editorconfig b/.editorconfig index 864bb18..991cce8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,3 +10,6 @@ insert_final_newline = true [*.yaml] indent_style = space + +[*.{kt,kts}] +ij_kotlin_imports_layout=* diff --git a/Dockerfile b/Dockerfile index a4f76f1..9987257 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM azul/zulu-openjdk:21-latest AS build ENV GRADLE_OPTS="-Dorg.gradle.daemon=false -Dkotlin.incremental=false" WORKDIR /app -COPY gradlew settings.gradle ./ +COPY gradlew settings.gradle .editorconfig ./ COPY gradle ./gradle RUN ./gradlew --version diff --git a/build.gradle b/build.gradle index 091d836..270e459 100644 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,7 @@ buildscript { dependencies { classpath libs.kotlin.gradlePlugin classpath libs.kotlin.serialization + classpath libs.spotless.gradlePlugin } } @@ -70,3 +71,13 @@ test { exceptionFormat = 'full' } } + +apply plugin: 'com.diffplug.spotless' +spotless { + kotlin { + target("src/**/*.kt") + ktlint(libs.ktlint.get().version).editorConfigOverride([ + 'ktlint_standard_filename': 'disabled', + ]) + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8e3e1c4..f9db55d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -10,6 +10,9 @@ kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", ve kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" } kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test" } +spotless-gradlePlugin = "com.diffplug.spotless:spotless-plugin-gradle:6.22.0" +ktlint = "com.pinterest.ktlint:ktlint-cli:1.0.1" + kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" } diff --git a/src/main/kotlin/watch/dependency/config.kt b/src/main/kotlin/watch/dependency/config.kt index c1668dd..eb34fe9 100644 --- a/src/main/kotlin/watch/dependency/config.kt +++ b/src/main/kotlin/watch/dependency/config.kt @@ -4,18 +4,18 @@ import okhttp3.HttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl import org.tomlj.Toml import org.tomlj.TomlTable -import watch.dependency.RepositoryConfig.Companion.GoogleMavenHost -import watch.dependency.RepositoryConfig.Companion.GoogleMavenId -import watch.dependency.RepositoryConfig.Companion.GoogleMavenName -import watch.dependency.RepositoryConfig.Companion.MavenCentralHost -import watch.dependency.RepositoryConfig.Companion.MavenCentralId -import watch.dependency.RepositoryConfig.Companion.MavenCentralName +import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_HOST +import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_ID +import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_NAME +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_HOST +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_ID +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_NAME import watch.dependency.RepositoryType.Maven2 fun MavenRepository.Factory.parseWellKnownIdOrUrl(value: String): MavenRepository { return when (value) { - MavenCentralId -> maven2(MavenCentralName, MavenCentralHost) - GoogleMavenId -> maven2(GoogleMavenName, GoogleMavenHost) + MAVEN_CENTRAL_ID -> maven2(MAVEN_CENTRAL_NAME, MAVEN_CENTRAL_HOST) + GOOGLE_MAVEN_ID -> maven2(GOOGLE_MAVEN_NAME, GOOGLE_MAVEN_HOST) else -> maven2("Maven Repository", value.toHttpUrl()) } } @@ -27,16 +27,16 @@ data class RepositoryConfig( val coordinates: List, ) { companion object { - const val MavenCentralId = "MavenCentral" - const val MavenCentralName = "Maven Central" - val MavenCentralHost = "https://repo1.maven.org/maven2/".toHttpUrl() - const val GoogleMavenId = "GoogleMaven" - const val GoogleMavenName = "Google Maven" - val GoogleMavenHost = "https://maven.google.com/".toHttpUrl() - private const val TomlKeyName = "name" - private const val TomlKeyHost = "host" - private const val TomlKeyType = "type" - private const val TomlKeyCoordinates = "coordinates" + const val MAVEN_CENTRAL_ID = "MavenCentral" + const val MAVEN_CENTRAL_NAME = "Maven Central" + val MAVEN_CENTRAL_HOST = "https://repo1.maven.org/maven2/".toHttpUrl() + const val GOOGLE_MAVEN_ID = "GoogleMaven" + const val GOOGLE_MAVEN_NAME = "Google Maven" + val GOOGLE_MAVEN_HOST = "https://maven.google.com/".toHttpUrl() + private const val TOML_KEY_NAME = "name" + private const val TOML_KEY_HOST = "host" + private const val TOML_KEY_TYPE = "type" + private const val TOML_KEY_COORDINATES = "coordinates" private fun TomlTable.getCoordinates(key: String): List { val coordinateArray = getArray(key)!! @@ -49,14 +49,14 @@ data class RepositoryConfig( var coordinates: List? = null for (key in keySet()) { when (key) { - TomlKeyCoordinates -> coordinates = getCoordinates(key) - TomlKeyName, TomlKeyHost, TomlKeyType -> { + TOML_KEY_COORDINATES -> coordinates = getCoordinates(key) + TOML_KEY_NAME, TOML_KEY_HOST, TOML_KEY_TYPE -> { throw IllegalArgumentException("'$self' table must not define a '$key' key") } else -> throw IllegalArgumentException("'$self' table contains unknown '$key' key") } } - requireNotNull(coordinates) { "'$self' table missing required '$TomlKeyCoordinates' key" } + requireNotNull(coordinates) { "'$self' table missing required '$TOML_KEY_COORDINATES' key" } return RepositoryConfig(name, host, Maven2, coordinates) } @@ -67,15 +67,15 @@ data class RepositoryConfig( var coordinates: List? = null for (key in keySet()) { when (key) { - TomlKeyName -> name = getString(key)!! - TomlKeyHost -> host = getString(key)!!.toHttpUrl() - TomlKeyType -> type = RepositoryType.valueOf(getString(key)!!) - TomlKeyCoordinates -> coordinates = getCoordinates(key) + TOML_KEY_NAME -> name = getString(key)!! + TOML_KEY_HOST -> host = getString(key)!!.toHttpUrl() + TOML_KEY_TYPE -> type = RepositoryType.valueOf(getString(key)!!) + TOML_KEY_COORDINATES -> coordinates = getCoordinates(key) else -> throw IllegalArgumentException("'$self' table contains unknown key '$key'") } } - requireNotNull(host) { "'$self' table missing required '$TomlKeyHost' key" } - requireNotNull(coordinates) { "'$self' table missing required '$TomlKeyCoordinates' key" } + requireNotNull(host) { "'$self' table missing required '$TOML_KEY_HOST' key" } + requireNotNull(coordinates) { "'$self' table missing required '$TOML_KEY_COORDINATES' key" } return RepositoryConfig(name, host, type, coordinates) } @@ -87,8 +87,8 @@ data class RepositoryConfig( for (key in parseResult.keySet()) { val table = parseResult.getTable(key)!! this += when (key) { - MavenCentralId -> table.tryParseWellKnown(MavenCentralId, MavenCentralName, MavenCentralHost) - GoogleMavenId -> table.tryParseWellKnown(GoogleMavenId, GoogleMavenName, GoogleMavenHost) + MAVEN_CENTRAL_ID -> table.tryParseWellKnown(MAVEN_CENTRAL_ID, MAVEN_CENTRAL_NAME, MAVEN_CENTRAL_HOST) + GOOGLE_MAVEN_ID -> table.tryParseWellKnown(GOOGLE_MAVEN_ID, GOOGLE_MAVEN_NAME, GOOGLE_MAVEN_HOST) else -> table.tryParseCustom(key) } } diff --git a/src/main/kotlin/watch/dependency/database.kt b/src/main/kotlin/watch/dependency/database.kt index dcc87fb..86fdb93 100644 --- a/src/main/kotlin/watch/dependency/database.kt +++ b/src/main/kotlin/watch/dependency/database.kt @@ -44,14 +44,14 @@ class FileSystemDatabase(private val root: Path) : Database { override fun coordinateVersionSeen( coordinate: MavenCoordinate, - version: String + version: String, ): Boolean { return path(coordinate, version).exists() } override fun markCoordinateVersionSeen( coordinate: MavenCoordinate, - version: String + version: String, ) { val path = path(coordinate, version) if (path.notExists()) { diff --git a/src/main/kotlin/watch/dependency/dependencyAwait.kt b/src/main/kotlin/watch/dependency/dependencyAwait.kt index 6a3bd20..c64ccfe 100644 --- a/src/main/kotlin/watch/dependency/dependencyAwait.kt +++ b/src/main/kotlin/watch/dependency/dependencyAwait.kt @@ -4,7 +4,7 @@ import java.io.PrintStream import kotlin.time.Duration import kotlinx.coroutines.delay -internal const val cursorUpAndClearLine = "\u001B[F\u001B[K" +internal const val CURSOR_UP_AND_CLEAR_LINE = "\u001B[F\u001B[K" class DependencyAwait( private val mavenRepository: MavenRepository, @@ -16,7 +16,7 @@ class DependencyAwait( ) { suspend fun await( coordinate: MavenCoordinate, - version: String + version: String, ) { var needsClear = false while (true) { @@ -29,14 +29,16 @@ class DependencyAwait( } if (progress != null) { - progress.print(buildString { - if (needsClear) { - append(cursorUpAndClearLine) - } - append("Last checked: ") - append(timestampSource.now()) - append('\n') - }) + progress.print( + buildString { + if (needsClear) { + append(CURSOR_UP_AND_CLEAR_LINE) + } + append("Last checked: ") + append(timestampSource.now()) + append('\n') + }, + ) progress.flush() } needsClear = true @@ -46,9 +48,11 @@ class DependencyAwait( } if (progress != null && needsClear) { - progress.print(buildString { - append(cursorUpAndClearLine) - }) + progress.print( + buildString { + append(CURSOR_UP_AND_CLEAR_LINE) + }, + ) progress.flush() } diff --git a/src/main/kotlin/watch/dependency/dependencyNotifier.kt b/src/main/kotlin/watch/dependency/dependencyNotifier.kt index 8c8b21e..c26bd58 100644 --- a/src/main/kotlin/watch/dependency/dependencyNotifier.kt +++ b/src/main/kotlin/watch/dependency/dependencyNotifier.kt @@ -83,7 +83,7 @@ private class DependencyChecker( supervisorScope { for (coordinates in coordinates) { launch(start = UNDISPATCHED) { - debug.log { "Fetching metadata for $coordinates..." } + debug.log { "Fetching metadata for $coordinates..." } val versions = mavenRepository.versions(coordinates) debug.log { "$coordinates $versions" } diff --git a/src/main/kotlin/watch/dependency/main.kt b/src/main/kotlin/watch/dependency/main.kt index 530f467..bee1687 100644 --- a/src/main/kotlin/watch/dependency/main.kt +++ b/src/main/kotlin/watch/dependency/main.kt @@ -25,7 +25,7 @@ import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor.Level.BASIC -import watch.dependency.RepositoryConfig.Companion.MavenCentralId +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_ID fun main(vararg args: String) { NoOpCliktCommand(name = "dependency-watch") @@ -38,7 +38,7 @@ fun main(vararg args: String) { private abstract class DependencyWatchCommand( name: String, - help: String = "" + help: String = "", ) : CliktCommand(name = name, help = help) { protected val debug by option(hidden = true) .switch(mapOf("--debug" to Debug.Console)) @@ -91,12 +91,14 @@ private class AwaitCommand : DependencyWatchCommand( help = "Wait for an artifact to appear in a Maven repository then exit", ) { private val repo by option("--repo", metavar = "URL") - .help(""" + .help( + """ |URL or well-known ID of maven repository to check (default is "MavenCentral"). |Available well-known IDs: "MavenCentral", "GoogleMaven". - |""".trimMargin() + | + """.trimMargin(), ) - .default(MavenCentralId) + .default(MAVEN_CENTRAL_ID) private val quiet by option("--quiet", "-q") .flag() @@ -129,13 +131,14 @@ private class AwaitCommand : DependencyWatchCommand( } private class NotifyCommand( - fs: FileSystem + fs: FileSystem, ) : DependencyWatchCommand( name = "notify", help = "Monitor Maven coordinates in a Maven repository for new versions", ) { private val configPath by argument("CONFIG") - .help(""" + .help( + """ |TOML file containing repositories and coordinates to watch | |Format: @@ -163,7 +166,9 @@ private class NotifyCommand( |"MavenCentral" and "GoogleMaven" are two optional well-known repositories |which only require a list of coordinates. Other repositories also require |a host and can specify an optional name. - |""".trimMargin()) + | + """.trimMargin(), + ) .path(fileSystem = fs) @Suppress("USELESS_CAST") // Needed to keep the type abstract. diff --git a/src/main/kotlin/watch/dependency/maven.kt b/src/main/kotlin/watch/dependency/maven.kt index ba63050..734e325 100644 --- a/src/main/kotlin/watch/dependency/maven.kt +++ b/src/main/kotlin/watch/dependency/maven.kt @@ -85,7 +85,7 @@ private class HttpMaven2Repository( val metadata = xmlFormat.decodeFromString(ArtifactMetadata.serializer(), body) return Versions( latest = metadata.versioning.release, - all = metadata.versioning.versions.toSet() + all = metadata.versioning.versions.toSet(), ) } diff --git a/src/main/kotlin/watch/dependency/notify.kt b/src/main/kotlin/watch/dependency/notify.kt index 404ed6b..84c4b4a 100644 --- a/src/main/kotlin/watch/dependency/notify.kt +++ b/src/main/kotlin/watch/dependency/notify.kt @@ -59,7 +59,7 @@ class IftttVersionNotifier( private data class PostBody( val value1: String? = null, val value2: String? = null, - val value3: String? = null + val value3: String? = null, ) { fun toJson(): RequestBody { val json = format.encodeToString(serializer(), this) diff --git a/src/main/kotlin/watch/dependency/util.kt b/src/main/kotlin/watch/dependency/util.kt index 83bbdbf..66ad30d 100644 --- a/src/main/kotlin/watch/dependency/util.kt +++ b/src/main/kotlin/watch/dependency/util.kt @@ -2,13 +2,13 @@ package watch.dependency +import java.io.IOException +import kotlin.coroutines.resume +import kotlin.coroutines.resumeWithException import kotlinx.coroutines.suspendCancellableCoroutine import okhttp3.Call import okhttp3.Callback import okhttp3.Response -import java.io.IOException -import kotlin.coroutines.resume -import kotlin.coroutines.resumeWithException class HttpException(val code: Int, message: String) : RuntimeException("$code $message") @@ -22,7 +22,7 @@ suspend fun Call.await(): String { continuation.resume(body) } else { continuation.resumeWithException( - HttpException(response.code, response.message) + HttpException(response.code, response.message), ) } } diff --git a/src/test/kotlin/watch/dependency/ConfigTest.kt b/src/test/kotlin/watch/dependency/ConfigTest.kt index b8c2b6b..5bcb40c 100644 --- a/src/test/kotlin/watch/dependency/ConfigTest.kt +++ b/src/test/kotlin/watch/dependency/ConfigTest.kt @@ -6,10 +6,10 @@ import okhttp3.HttpUrl.Companion.toHttpUrl import org.junit.Assert.assertEquals import org.junit.Test import org.tomlj.TomlInvalidTypeException -import watch.dependency.RepositoryConfig.Companion.GoogleMavenHost -import watch.dependency.RepositoryConfig.Companion.GoogleMavenName -import watch.dependency.RepositoryConfig.Companion.MavenCentralHost -import watch.dependency.RepositoryConfig.Companion.MavenCentralName +import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_HOST +import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_NAME +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_HOST +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_NAME import watch.dependency.RepositoryType.Maven2 class ConfigTest { @@ -21,274 +21,369 @@ class ConfigTest { @Test fun mavenCentral() { val expected = listOf( - RepositoryConfig(MavenCentralName, MavenCentralHost, Maven2, listOf( - MavenCoordinate("com.example", "example-a"), - MavenCoordinate("com.example", "example-b")))) - val actual = RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig( + MAVEN_CENTRAL_NAME, + MAVEN_CENTRAL_HOST, + Maven2, + listOf( + MavenCoordinate("com.example", "example-a"), + MavenCoordinate("com.example", "example-b"), + ), + ), + ) + val actual = RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) assertEquals(expected, actual) } @Test fun mavenCentralNonTableThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |MavenCentral = "foo" - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun mavenCentralNoCoordinatesThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'MavenCentral' table missing required 'coordinates' key") } @Test fun mavenCentralNonArrayCoordinatesThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |coordinates = "foo" - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun mavenCentralNonStringCoordinatesThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |coordinates = [1, 2, 3] - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun mavenCentralNameThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |name = "Custom Name" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'MavenCentral' table must not define a 'name' key") } @Test fun mavenCentralHostThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |host = "https://example.com/" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'MavenCentral' table must not define a 'host' key") } @Test fun mavenCentralTypeThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |type = "Maven2" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'MavenCentral' table must not define a 'type' key") } @Test fun mavenCentralUnknownKeyThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |foo = "bar" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'MavenCentral' table contains unknown 'foo' key") } @Test fun mavenCentralChildTableThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] |[MavenCentral.Foo] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'MavenCentral' table contains unknown 'Foo' key") } @Test fun googleMaven() { val expected = listOf( - RepositoryConfig(GoogleMavenName, GoogleMavenHost, Maven2, listOf( - MavenCoordinate("com.example", "example-a"), - MavenCoordinate("com.example", "example-b")))) - val actual = RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig( + GOOGLE_MAVEN_NAME, + GOOGLE_MAVEN_HOST, + Maven2, + listOf( + MavenCoordinate("com.example", "example-a"), + MavenCoordinate("com.example", "example-b"), + ), + ), + ) + val actual = RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) assertEquals(expected, actual) } @Test fun googleMavenNonTableThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |GoogleMaven = "foo" - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun googleMavenNoCoordinatesThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'GoogleMaven' table missing required 'coordinates' key") } @Test fun googleMavenNonArrayCoordinatesThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |coordinates = "foo" - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun googleMavenNonStringCoordinatesThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |coordinates = [1, 2, 3] - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun googleMavenNameThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |name = "Custom Name" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'GoogleMaven' table must not define a 'name' key") } @Test fun googleMavenHostThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |host = "https://example.com/" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'GoogleMaven' table must not define a 'host' key") } @Test fun googleMavenTypeThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |type = "Maven2" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'GoogleMaven' table must not define a 'type' key") } @Test fun googleMavenUnknownKeyThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |foo = "bar" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'GoogleMaven' table contains unknown 'foo' key") } @Test fun googleMavenChildTableThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[GoogleMaven] |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] |[GoogleMaven.Foo] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'GoogleMaven' table contains unknown 'Foo' key") } @Test fun customRepo() { val expected = listOf( - RepositoryConfig("CustomRepo", "https://example.com/".toHttpUrl(), Maven2, listOf( - MavenCoordinate("com.example", "example-a"), - MavenCoordinate("com.example", "example-b")))) - val actual = RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig( + "CustomRepo", + "https://example.com/".toHttpUrl(), + Maven2, + listOf( + MavenCoordinate("com.example", "example-a"), + MavenCoordinate("com.example", "example-b"), + ), + ), + ) + val actual = RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |host = "https://example.com/" |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) assertEquals(expected, actual) } @Test fun customRepoNonTableThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |CustomRepo = "foo" - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun customRepoWithName() { val expected = listOf( - RepositoryConfig("Custom Repo", "https://example.com/".toHttpUrl(), Maven2, listOf( - MavenCoordinate("com.example", "example-a"), - MavenCoordinate("com.example", "example-b")))) - val actual = RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig( + "Custom Repo", + "https://example.com/".toHttpUrl(), + Maven2, + listOf( + MavenCoordinate("com.example", "example-a"), + MavenCoordinate("com.example", "example-b"), + ), + ), + ) + val actual = RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |name = "Custom Repo" |host = "https://example.com/" @@ -296,13 +391,16 @@ class ConfigTest { | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) assertEquals(expected, actual) } @Test fun customRepoNonStringNameThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |name = 1 |host = "https://example.com/" @@ -310,16 +408,26 @@ class ConfigTest { | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun customRepoWithType() { val expected = listOf( - RepositoryConfig("CustomRepo", "https://example.com/".toHttpUrl(), Maven2, listOf( - MavenCoordinate("com.example", "example-a"), - MavenCoordinate("com.example", "example-b")))) - val actual = RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig( + "CustomRepo", + "https://example.com/".toHttpUrl(), + Maven2, + listOf( + MavenCoordinate("com.example", "example-a"), + MavenCoordinate("com.example", "example-b"), + ), + ), + ) + val actual = RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |host = "https://example.com/" |type = "Maven2" @@ -327,13 +435,16 @@ class ConfigTest { | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) assertEquals(expected, actual) } @Test fun customRepoNonStringTypeThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |host = "https://example.com/" |type = 1 @@ -341,67 +452,85 @@ class ConfigTest { | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun customRepoNoHostThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'CustomRepo' table missing required 'host' key") } @Test fun customRepoNonStringHostThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |host = 1 |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun customRepoNoCoordinatesThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |host = "https://example.com/" - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("'CustomRepo' table missing required 'coordinates' key") } @Test fun customRepoNonArrayCoordinatesThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |coordinates = "foo" - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun customRepoNonStringCoordinatesThrows() { assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |coordinates = [1, 2, 3] - |""".trimMargin()) + | + """.trimMargin(), + ) } } @Test fun customRepoUnknownTypeThrows() { val t = assertFailsWith { - RepositoryConfig.parseConfigsFromToml(""" + RepositoryConfig.parseConfigsFromToml( + """ |[CustomRepo] |host = "https://example.com/" |type = "MavenTwo" @@ -409,7 +538,9 @@ class ConfigTest { | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) } assertThat(t).hasMessageThat().isEqualTo("No enum constant watch.dependency.RepositoryType.MavenTwo") } diff --git a/src/test/kotlin/watch/dependency/DatabaseTest.kt b/src/test/kotlin/watch/dependency/DatabaseTest.kt index bcf68a7..147ecc5 100644 --- a/src/test/kotlin/watch/dependency/DatabaseTest.kt +++ b/src/test/kotlin/watch/dependency/DatabaseTest.kt @@ -43,7 +43,7 @@ class DatabaseTest( @Parameters(name = "{0}") fun data() = listOf( arrayOf("memory", InMemoryDatabase()), - arrayOf("fs", FileSystemDatabase(Jimfs.newFileSystem(unix()).rootDirectory)) + arrayOf("fs", FileSystemDatabase(Jimfs.newFileSystem(unix()).rootDirectory)), ) } } diff --git a/src/test/kotlin/watch/dependency/DependencyNotifierTest.kt b/src/test/kotlin/watch/dependency/DependencyNotifierTest.kt index 0728287..97d637b 100644 --- a/src/test/kotlin/watch/dependency/DependencyNotifierTest.kt +++ b/src/test/kotlin/watch/dependency/DependencyNotifierTest.kt @@ -11,8 +11,8 @@ import kotlinx.coroutines.test.runTest import okhttp3.HttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl import org.junit.Test -import watch.dependency.RepositoryConfig.Companion.MavenCentralHost -import watch.dependency.RepositoryConfig.Companion.MavenCentralName +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_HOST +import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_NAME class DependencyNotifierTest { private val config = Jimfs.newFileSystem().rootDirectory.resolve("config.toml") @@ -30,17 +30,20 @@ class DependencyNotifierTest { ) @Test fun run() = runTest { - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", |] - |""".trimMargin()) + | + """.trimMargin(), + ) notifier.run() assertThat(versionNotifier.notifications).isEmpty() - val mavenCentral = mavenRepositories[MavenCentralHost]!! + val mavenCentral = mavenRepositories[MAVEN_CENTRAL_HOST]!! mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.0") notifier.run() @@ -51,7 +54,8 @@ class DependencyNotifierTest { @Test fun runChecksMultipleRepos() = runTest { val customHost = "https://example.com/".toHttpUrl() - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", @@ -62,12 +66,14 @@ class DependencyNotifierTest { |coordinates = [ | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) notifier.run() assertThat(versionNotifier.notifications).isEmpty() - val mavenCentral = mavenRepositories[MavenCentralHost]!! + val mavenCentral = mavenRepositories[MAVEN_CENTRAL_HOST]!! mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.0") val customRepository = mavenRepositories[customHost]!! customRepository.addArtifact(MavenCoordinate("com.example", "example-b"), "1.0") @@ -80,12 +86,15 @@ class DependencyNotifierTest { } @Test fun notifyNotifiesOnceAvailable() = runTest { - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", |] - |""".trimMargin()) + | + """.trimMargin(), + ) val monitorJob = launch { notifier.monitor(5.seconds) @@ -94,7 +103,7 @@ class DependencyNotifierTest { runCurrent() assertThat(versionNotifier.notifications).isEmpty() - val mavenCentral = mavenRepositories[MavenCentralHost]!! + val mavenCentral = mavenRepositories[MAVEN_CENTRAL_HOST]!! mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.0") advanceTimeBy(5.seconds) @@ -108,7 +117,8 @@ class DependencyNotifierTest { @Test fun notifyChecksMultipleReposAndNotifiesOnceAvailable() = runTest { val customHost = "https://example.com/".toHttpUrl() - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", @@ -119,7 +129,9 @@ class DependencyNotifierTest { |coordinates = [ | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) val monitorJob = launch { notifier.monitor(5.seconds) @@ -128,7 +140,7 @@ class DependencyNotifierTest { runCurrent() assertThat(versionNotifier.notifications).isEmpty() - val mavenCentral = mavenRepositories[MavenCentralHost]!! + val mavenCentral = mavenRepositories[MAVEN_CENTRAL_HOST]!! mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.0") val customRepository = mavenRepositories[customHost]!! customRepository.addArtifact(MavenCoordinate("com.example", "example-b"), "1.0") @@ -144,12 +156,14 @@ class DependencyNotifierTest { } @Test fun monitorNotifiesLatestFirstTime() = runTest { - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", |] - """.trimMargin()) + """.trimMargin(), + ) val monitorJob = launch { notifier.monitor(5.seconds) @@ -158,7 +172,7 @@ class DependencyNotifierTest { runCurrent() assertThat(versionNotifier.notifications).isEmpty() - val mavenCentral = mavenRepositories[MavenCentralHost]!! + val mavenCentral = mavenRepositories[MAVEN_CENTRAL_HOST]!! mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.0") mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.1") mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.2") @@ -174,12 +188,14 @@ class DependencyNotifierTest { } @Test fun monitorNotifiesAllNewSecondTime() = runTest { - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", |] - """.trimMargin()) + """.trimMargin(), + ) val monitorJob = launch { notifier.monitor(5.seconds) @@ -188,7 +204,7 @@ class DependencyNotifierTest { runCurrent() assertThat(versionNotifier.notifications).isEmpty() - val mavenCentral = mavenRepositories[MavenCentralHost]!! + val mavenCentral = mavenRepositories[MAVEN_CENTRAL_HOST]!! mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.0") mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.1") @@ -213,15 +229,18 @@ class DependencyNotifierTest { } @Test fun monitorReadsConfigForEachCheck() = runTest { - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", |] - |""".trimMargin()) + | + """.trimMargin(), + ) // Create and write the repo to the map so it's available on first run. - val mavenCentral = FakeMavenRepository(MavenCentralName).also { mavenRepositories[MavenCentralHost] = it } + val mavenCentral = FakeMavenRepository(MAVEN_CENTRAL_NAME).also { mavenRepositories[MAVEN_CENTRAL_HOST] = it } mavenCentral.addArtifact(MavenCoordinate("com.example", "example-a"), "1.0") val monitorJob = launch { @@ -241,13 +260,16 @@ class DependencyNotifierTest { "Maven Central com.example:example-a:1.0", ) - config.writeText(""" + config.writeText( + """ |[MavenCentral] |coordinates = [ | "com.example:example-a", | "com.example:example-b", |] - |""".trimMargin()) + | + """.trimMargin(), + ) advanceTimeBy(5.seconds) runCurrent() assertThat(versionNotifier.notifications).containsExactly( diff --git a/src/test/kotlin/watch/dependency/HttpMaven2RepositoryTest.kt b/src/test/kotlin/watch/dependency/HttpMaven2RepositoryTest.kt index 3021aed..8cefdb2 100644 --- a/src/test/kotlin/watch/dependency/HttpMaven2RepositoryTest.kt +++ b/src/test/kotlin/watch/dependency/HttpMaven2RepositoryTest.kt @@ -15,8 +15,10 @@ class HttpMaven2RepositoryTest { MavenRepository.Factory.Http(OkHttpClient()).maven2("MWS", server.url("/")) @Test fun simple() = runBlocking { - server.enqueue(MockResponse() - .setBody(""" + server.enqueue( + MockResponse() + .setBody( + """ | | com.example | example @@ -34,19 +36,25 @@ class HttpMaven2RepositoryTest { | 20200520162908 | | - |""".trimMargin())) + | + """.trimMargin(), + ), + ) val versions = repository.versions(MavenCoordinate("com.example", "example")) - assertThat(versions).isEqualTo(Versions( - latest = "1.1.0", - all = setOf( - "1.0.0-alpha1", - "1.0.0-alpha2", - "1.0.0-beta3", - "1.0.0-beta4", - "1.0.0", - "1.1.0", - ))) + assertThat(versions).isEqualTo( + Versions( + latest = "1.1.0", + all = setOf( + "1.0.0-alpha1", + "1.0.0-alpha2", + "1.0.0-beta3", + "1.0.0-beta4", + "1.0.0", + "1.1.0", + ), + ), + ) val request = server.takeRequest() assertThat(request.requestUrl) diff --git a/src/test/kotlin/watch/dependency/testUtil.kt b/src/test/kotlin/watch/dependency/testUtil.kt index 52ba2ff..9452385 100644 --- a/src/test/kotlin/watch/dependency/testUtil.kt +++ b/src/test/kotlin/watch/dependency/testUtil.kt @@ -2,8 +2,5 @@ package watch.dependency import java.nio.file.FileSystem import java.nio.file.Path -import kotlin.time.Duration -import kotlinx.coroutines.test.TestScope -import kotlinx.coroutines.test.advanceTimeBy val FileSystem.rootDirectory: Path get() = rootDirectories.single()