From 7690d3133d1568c1f58aed83576df7d1df1a5263 Mon Sep 17 00:00:00 2001 From: "Sergey.Shanshin" Date: Wed, 15 Nov 2023 16:52:02 +0100 Subject: [PATCH] Added Kover dependency check If an artifact that completely matches the attributes is not found in the dependency, then a random artifact is taken, the processing of which can lead to unpredictable errors. An additional check of the dependency resolution is needed to exclude such a situation. Fixes #478 --- .../functional/cases/NoDependencyTests.kt | 39 ++++++++++++ .../app/build.gradle.kts | 47 ++++++++++++++ .../app/src/main/AndroidManifest.xml | 24 +++++++ .../kotlinx/kover/test/android/DebugUtil.kt | 7 +++ .../kover/test/android/MainActivity.kt | 13 ++++ .../java/kotlinx/kover/test/android/Maths.kt | 13 ++++ .../app/src/main/res/layout/activity_main.xml | 36 +++++++++++ .../app/src/main/res/values/colors.xml | 10 +++ .../app/src/main/res/values/strings.xml | 3 + .../app/src/main/res/values/themes.xml | 4 ++ .../kotlinx/kover/test/android/LocalTests.kt | 13 ++++ .../no-dependency-android/build.gradle.kts | 6 ++ .../no-dependency-android/gradle.properties | 23 +++++++ .../no-dependency-android/settings.gradle.kts | 19 ++++++ .../subproject/build.gradle.kts | 3 + .../builds/no-dependency-jvm/build.gradle.kts | 12 ++++ .../no-dependency-jvm/settings.gradle.kts | 10 +++ .../subproject/build.gradle.kts | 7 +++ .../app-extra/build.gradle.kts | 45 ++++++++++++++ .../app-extra/src/main/AndroidManifest.xml | 24 +++++++ .../kotlinx/kover/test/android/ExtraUtil.kt | 7 +++ .../src/main/res/layout/activity_main.xml | 36 +++++++++++ .../app-extra/src/main/res/values/colors.xml | 10 +++ .../app-extra/src/main/res/values/strings.xml | 3 + .../app-extra/src/main/res/values/themes.xml | 4 ++ .../kotlinx/kover/test/android/LocalTests.kt | 13 ++++ .../gradle/plugin/DependencyCheckListener.kt | 62 +++++++++++++++++++ .../kover/gradle/plugin/KoverGradlePlugin.kt | 18 ++++++ .../appliers/reports/AndroidVariantApplier.kt | 2 + .../appliers/reports/DefaultVariantApplier.kt | 7 ++- .../appliers/reports/ReportsVariantApplier.kt | 2 + .../kover/gradle/plugin/commons/Artifacts.kt | 2 +- .../gradle/plugin/commons/Configurations.kt | 12 ++++ 33 files changed, 534 insertions(+), 2 deletions(-) create mode 100644 kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/build.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/AndroidManifest.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/DebugUtil.kt create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/MainActivity.kt create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/Maths.kt create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/layout/activity_main.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/colors.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/strings.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/themes.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/test/java/kotlinx/kover/test/android/LocalTests.kt create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/build.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/gradle.properties create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/settings.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/subproject/build.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-jvm/build.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-jvm/settings.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-jvm/subproject/build.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/build.gradle.kts create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/src/main/AndroidManifest.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/src/main/java/kotlinx/kover/test/android/ExtraUtil.kt create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/src/main/res/layout/activity_main.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/src/main/res/values/colors.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/src/main/res/values/strings.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/src/main/res/values/themes.xml create mode 100644 kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-variant-android/app-extra/src/test/java/kotlinx/kover/test/android/LocalTests.kt create mode 100644 kover-gradle-plugin/src/main/kotlin/kotlinx/kover/gradle/plugin/DependencyCheckListener.kt diff --git a/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt new file mode 100644 index 00000000..6662fa4e --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/kotlin/kotlinx/kover/gradle/plugin/test/functional/cases/NoDependencyTests.kt @@ -0,0 +1,39 @@ +package kotlinx.kover.gradle.plugin.test.functional.cases + +import kotlinx.kover.gradle.plugin.test.functional.framework.runner.buildFromTemplate +import kotlinx.kover.gradle.plugin.test.functional.framework.runner.runWithParams +import org.junit.jupiter.api.Test +import kotlin.test.assertContains +import kotlin.test.assertFalse + +/** + * Tests on dependency check https://github.com/Kotlin/kotlinx-kover/issues/478. + */ +class NoDependencyTests { + @Test + fun testJvmNotApplied() { + val buildSource = buildFromTemplate("no-dependency-jvm") + val build = buildSource.generate() + val buildResult = build.runWithParams( "koverHtmlReport") + assertFalse(buildResult.isSuccessful) + assertContains(buildResult.output, "Kover plugin is not applied") + } + + @Test + fun testAndroidNotApplied() { + val buildSource = buildFromTemplate("no-dependency-android") + val build = buildSource.generate() + val buildResult = build.runWithParams( ":app:koverHtmlReportDebug") + assertFalse(buildResult.isSuccessful) + assertContains(buildResult.output, "Kover plugin is not applied") + } + + @Test + fun testAndroidNoVariant() { + val buildSource = buildFromTemplate("no-dependency-variant-android") + val build = buildSource.generate() + val buildResult = build.runWithParams( ":app-extra:koverHtmlReportExtra") + assertFalse(buildResult.isSuccessful) + assertContains(buildResult.output, "Kover android variant 'extra' was not matched with any variant from dependency") + } +} \ No newline at end of file diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/build.gradle.kts b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/build.gradle.kts new file mode 100644 index 00000000..fc439e6b --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/build.gradle.kts @@ -0,0 +1,47 @@ +plugins { + id ("org.jetbrains.kotlinx.kover") + id ("com.android.application") + id ("org.jetbrains.kotlin.android") +} + +android { + namespace = "kotlinx.kover.test.android" + compileSdk = 32 + + defaultConfig { + applicationId = "kotlinx.kover.test.android" + minSdk = 21 + targetSdk = 31 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = true + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = "1.8" + } + buildFeatures { + viewBinding = true + } +} + +dependencies { + // error dependency + kover(project(":subproject")) + + implementation("androidx.core:core-ktx:1.8.0") + implementation("androidx.appcompat:appcompat:1.5.0") + implementation("com.google.android.material:material:1.6.1") + implementation("androidx.constraintlayout:constraintlayout:2.1.4") + testImplementation("junit:junit:4.13.2") +} diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/AndroidManifest.xml b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..358fac25 --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/DebugUtil.kt b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/DebugUtil.kt new file mode 100644 index 00000000..efe82040 --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/DebugUtil.kt @@ -0,0 +1,7 @@ +package kotlinx.kover.test.android + +object DebugUtil { + fun log(message: String) { + println("DEBUG: $message") + } +} \ No newline at end of file diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/MainActivity.kt b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/MainActivity.kt new file mode 100644 index 00000000..1caaf5b6 --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/MainActivity.kt @@ -0,0 +1,13 @@ +package kotlinx.kover.test.android + +import android.os.Bundle +import android.app.Activity + +class MainActivity : Activity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + } + +} diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/Maths.kt b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/Maths.kt new file mode 100644 index 00000000..03edc099 --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/java/kotlinx/kover/test/android/Maths.kt @@ -0,0 +1,13 @@ +package kotlinx.kover.test.android + +object Maths { + fun sum(a: Int, b: Int): Int { + DebugUtil.log("invoked sum") + return a + b + } + + fun sub(a: Int, b: Int): Int { + DebugUtil.log("invoked sub") + return a - b + } +} \ No newline at end of file diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/layout/activity_main.xml b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..ee57d166 --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,36 @@ + + + + + + + + \ No newline at end of file diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/colors.xml b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/colors.xml new file mode 100644 index 00000000..f8c6127d --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/strings.xml b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/strings.xml new file mode 100644 index 00000000..fa43411e --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Android Test + \ No newline at end of file diff --git a/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/themes.xml b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/themes.xml new file mode 100644 index 00000000..0b4cfc4c --- /dev/null +++ b/kover-gradle-plugin/src/functionalTest/templates/builds/no-dependency-android/app/src/main/res/values/themes.xml @@ -0,0 +1,4 @@ + + +