From 67f5069cee57736a23329d461b0d3ba09790252f Mon Sep 17 00:00:00 2001 From: Kevin Galligan Date: Sat, 30 Oct 2021 21:48:23 -0400 Subject: [PATCH] TestLogWriter --- kermit-test/.gitignore | 50 ++++++++ kermit-test/api/android/kermit-test.api | 47 +++++++ kermit-test/api/jvm/kermit-test.api | 40 ++++++ kermit-test/build.gradle.kts | 116 ++++++++++++++++++ .../src/androidMain/AndroidManifest.xml | 11 ++ .../co/touchlab/kermit/TestLogWriter.kt | 57 +++++++++ kermit/src/androidMain/AndroidManifest.xml | 1 - .../co/touchlab/kermit/TestLogWriter.kt | 1 + settings.gradle.kts | 1 + 9 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 kermit-test/.gitignore create mode 100644 kermit-test/api/android/kermit-test.api create mode 100644 kermit-test/api/jvm/kermit-test.api create mode 100644 kermit-test/build.gradle.kts create mode 100644 kermit-test/src/androidMain/AndroidManifest.xml create mode 100644 kermit-test/src/commonMain/kotlin/co/touchlab/kermit/TestLogWriter.kt diff --git a/kermit-test/.gitignore b/kermit-test/.gitignore new file mode 100644 index 00000000..227a17f8 --- /dev/null +++ b/kermit-test/.gitignore @@ -0,0 +1,50 @@ +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +.idea/modules.xml +.idea/*.iml +.idea/modules/ +*.iml +*.ipr + + # Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + + # Generated files +bin/ +gen/ + + # Gradle files +.gradle +.gradle/ +build/ + +local.properties diff --git a/kermit-test/api/android/kermit-test.api b/kermit-test/api/android/kermit-test.api new file mode 100644 index 00000000..81281235 --- /dev/null +++ b/kermit-test/api/android/kermit-test.api @@ -0,0 +1,47 @@ +public final class co/touchlab/kermit/BuildConfig { + public static final field BUILD_TYPE Ljava/lang/String; + public static final field DEBUG Z + public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String; + public fun ()V +} + +public final class co/touchlab/kermit/TestConfig : co/touchlab/kermit/LoggerConfig { + public fun (Lco/touchlab/kermit/Severity;Ljava/util/List;)V + public final fun component1 ()Lco/touchlab/kermit/Severity; + public final fun component2 ()Ljava/util/List; + public final fun copy (Lco/touchlab/kermit/Severity;Ljava/util/List;)Lco/touchlab/kermit/TestConfig; + public static synthetic fun copy$default (Lco/touchlab/kermit/TestConfig;Lco/touchlab/kermit/Severity;Ljava/util/List;ILjava/lang/Object;)Lco/touchlab/kermit/TestConfig; + public fun equals (Ljava/lang/Object;)Z + public fun getLogWriterList ()Ljava/util/List; + public fun getMinSeverity ()Lco/touchlab/kermit/Severity; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public final class co/touchlab/kermit/TestLogWriter : co/touchlab/kermit/LogWriter { + public fun (Lco/touchlab/kermit/Severity;)V + public final fun assertCount (I)V + public final fun assertLast (Lkotlin/jvm/functions/Function1;)V + public final fun getLogs ()Ljava/util/List; + public fun isLoggable (Lco/touchlab/kermit/Severity;)Z + public fun log (Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)V + public final fun reset ()V +} + +public final class co/touchlab/kermit/TestLogWriter$LogEntry { + public fun (Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)V + public final fun component1 ()Lco/touchlab/kermit/Severity; + public final fun component2 ()Ljava/lang/String; + public final fun component3 ()Ljava/lang/String; + public final fun component4 ()Ljava/lang/Throwable; + public final fun copy (Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)Lco/touchlab/kermit/TestLogWriter$LogEntry; + public static synthetic fun copy$default (Lco/touchlab/kermit/TestLogWriter$LogEntry;Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;ILjava/lang/Object;)Lco/touchlab/kermit/TestLogWriter$LogEntry; + public fun equals (Ljava/lang/Object;)Z + public final fun getMessage ()Ljava/lang/String; + public final fun getSeverity ()Lco/touchlab/kermit/Severity; + public final fun getTag ()Ljava/lang/String; + public final fun getThrowable ()Ljava/lang/Throwable; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + diff --git a/kermit-test/api/jvm/kermit-test.api b/kermit-test/api/jvm/kermit-test.api new file mode 100644 index 00000000..a6a09311 --- /dev/null +++ b/kermit-test/api/jvm/kermit-test.api @@ -0,0 +1,40 @@ +public final class co/touchlab/kermit/TestConfig : co/touchlab/kermit/LoggerConfig { + public fun (Lco/touchlab/kermit/Severity;Ljava/util/List;)V + public final fun component1 ()Lco/touchlab/kermit/Severity; + public final fun component2 ()Ljava/util/List; + public final fun copy (Lco/touchlab/kermit/Severity;Ljava/util/List;)Lco/touchlab/kermit/TestConfig; + public static synthetic fun copy$default (Lco/touchlab/kermit/TestConfig;Lco/touchlab/kermit/Severity;Ljava/util/List;ILjava/lang/Object;)Lco/touchlab/kermit/TestConfig; + public fun equals (Ljava/lang/Object;)Z + public fun getLogWriterList ()Ljava/util/List; + public fun getMinSeverity ()Lco/touchlab/kermit/Severity; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public final class co/touchlab/kermit/TestLogWriter : co/touchlab/kermit/LogWriter { + public fun (Lco/touchlab/kermit/Severity;)V + public final fun assertCount (I)V + public final fun assertLast (Lkotlin/jvm/functions/Function1;)V + public final fun getLogs ()Ljava/util/List; + public fun isLoggable (Lco/touchlab/kermit/Severity;)Z + public fun log (Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)V + public final fun reset ()V +} + +public final class co/touchlab/kermit/TestLogWriter$LogEntry { + public fun (Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)V + public final fun component1 ()Lco/touchlab/kermit/Severity; + public final fun component2 ()Ljava/lang/String; + public final fun component3 ()Ljava/lang/String; + public final fun component4 ()Ljava/lang/Throwable; + public final fun copy (Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)Lco/touchlab/kermit/TestLogWriter$LogEntry; + public static synthetic fun copy$default (Lco/touchlab/kermit/TestLogWriter$LogEntry;Lco/touchlab/kermit/Severity;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;ILjava/lang/Object;)Lco/touchlab/kermit/TestLogWriter$LogEntry; + public fun equals (Ljava/lang/Object;)Z + public final fun getMessage ()Ljava/lang/String; + public final fun getSeverity ()Lco/touchlab/kermit/Severity; + public final fun getTag ()Ljava/lang/String; + public final fun getThrowable ()Ljava/lang/Throwable; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + diff --git a/kermit-test/build.gradle.kts b/kermit-test/build.gradle.kts new file mode 100644 index 00000000..b9ffa310 --- /dev/null +++ b/kermit-test/build.gradle.kts @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021 Touchlab + * 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. + */ + +plugins { + id("com.android.library") + kotlin("multiplatform") +} + +val STATELY_VERSION: String by project + +kotlin { + android { + publishAllLibraryVariants() + } + jvm() + js(BOTH) { + browser() + nodejs() + } + + macosX64() + macosArm64() + iosX64() + iosArm64() + iosArm32() + iosSimulatorArm64() + watchosArm32() + watchosArm64() + watchosSimulatorArm64() + watchosX86() + watchosX64() + tvosArm64() + tvosSimulatorArm64() + tvosX64() + + mingwX64() + mingwX86() + linuxX64() + linuxArm32Hfp() + linuxMips32() + + val commonMain by sourceSets.getting + val commonTest by sourceSets.getting + + val jsMain by sourceSets.getting + val jsTest by sourceSets.getting + + val jvmMain by sourceSets.getting { + dependsOn(commonMain) + } + + val androidMain by sourceSets.getting { + dependsOn(commonMain) + } + + commonMain.dependencies { + implementation(kotlin("test-common")) + implementation(project(":kermit")) + implementation("co.touchlab:stately-collections:$STATELY_VERSION") + } + + jsMain.dependencies { + implementation(kotlin("stdlib-js")) + implementation(kotlin("test-js")) + } + + jvmMain.dependencies { + implementation(kotlin("test")) + implementation(kotlin("test-junit")) + } + + androidMain.dependencies { + implementation(kotlin("test")) + implementation(kotlin("test-junit")) + } +} + +android { + compileSdkVersion(30) + defaultConfig { + minSdkVersion(15) + } + + val main by sourceSets.getting { + manifest.srcFile("src/androidMain/AndroidManifest.xml") + } +} + +tasks.register("publishWindows") { + if (tasks.findByName("publish") != null && + tasks.findByName("publishMingwX64PublicationToMavenRepository") != null) { + dependsOn( + "publishMingwX64PublicationToMavenRepository", + "publishMingwX86PublicationToMavenRepository" + ) + } +} + +tasks.register("publishLinux") { + if (tasks.findByName("publish") != null && + tasks.findByName("publishLinuxMips32PublicationToMavenRepository") != null) { + dependsOn("publishLinuxMips32PublicationToMavenRepository") + } +} + +apply(from = "../gradle/gradle-mvn-mpp-push.gradle") diff --git a/kermit-test/src/androidMain/AndroidManifest.xml b/kermit-test/src/androidMain/AndroidManifest.xml new file mode 100644 index 00000000..02ebb406 --- /dev/null +++ b/kermit-test/src/androidMain/AndroidManifest.xml @@ -0,0 +1,11 @@ + + + diff --git a/kermit-test/src/commonMain/kotlin/co/touchlab/kermit/TestLogWriter.kt b/kermit-test/src/commonMain/kotlin/co/touchlab/kermit/TestLogWriter.kt new file mode 100644 index 00000000..86ac5df9 --- /dev/null +++ b/kermit-test/src/commonMain/kotlin/co/touchlab/kermit/TestLogWriter.kt @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 Touchlab + * 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 co.touchlab.kermit + +import co.touchlab.stately.collections.frozenLinkedList +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +data class TestConfig( + override val minSeverity: Severity, + override val logWriterList: List, +) : LoggerConfig + +class TestLogWriter(private val loggable: Severity) : LogWriter() { + data class LogEntry( + val severity: Severity, + val message: String, + val tag: String?, + val throwable: Throwable? + ) + + @Suppress("DEPRECATION") + private val _logs = frozenLinkedList() + val logs: List + get() = _logs.toList() + + fun assertCount(count: Int) { + assertEquals(count, _logs.size) + } + + fun assertLast(check: LogEntry.() -> Boolean) { + assertTrue(_logs.last().check()) + } + + fun reset() { + _logs.clear() + } + + override fun isLoggable(severity: Severity): Boolean { + return severity.ordinal >= loggable.ordinal + } + + override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) { + _logs.add(LogEntry(severity, message, tag, throwable)) + } +} \ No newline at end of file diff --git a/kermit/src/androidMain/AndroidManifest.xml b/kermit/src/androidMain/AndroidManifest.xml index 67777a00..02ebb406 100644 --- a/kermit/src/androidMain/AndroidManifest.xml +++ b/kermit/src/androidMain/AndroidManifest.xml @@ -8,5 +8,4 @@ ~ ~ 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. --> - diff --git a/kermit/src/commonTest/kotlin/co/touchlab/kermit/TestLogWriter.kt b/kermit/src/commonTest/kotlin/co/touchlab/kermit/TestLogWriter.kt index 9195e221..86ac5df9 100644 --- a/kermit/src/commonTest/kotlin/co/touchlab/kermit/TestLogWriter.kt +++ b/kermit/src/commonTest/kotlin/co/touchlab/kermit/TestLogWriter.kt @@ -30,6 +30,7 @@ class TestLogWriter(private val loggable: Severity) : LogWriter() { val throwable: Throwable? ) + @Suppress("DEPRECATION") private val _logs = frozenLinkedList() val logs: List get() = _logs.toList() diff --git a/settings.gradle.kts b/settings.gradle.kts index 71066851..b7387938 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -15,6 +15,7 @@ include(":kermit") include(":kermit-crashlytics") include(":kermit-crashlytics-test") include(":kermit-bugsnag") +include(":kermit-test") include(":kermit-gradle-plugin") include(":kermit-ir-plugin")