[Gradle] Implement KT50161AndroidBuildCacheTest to cover KT-50161

This commit is contained in:
Sebastian Sellmair
2022-08-25 15:46:53 +02:00
committed by Space
parent 7a7954b1e2
commit ab73251af0
10 changed files with 206 additions and 0 deletions
@@ -0,0 +1,71 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.android
import org.gradle.api.logging.configuration.WarningMode
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.BuildTask
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.fail
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
@AndroidGradlePluginTests
@DisplayName("Android Build Cache Test for KT-50161")
class KT50161AndroidBuildCacheTest : KGPBaseTest() {
@GradleAndroidTest
@DisplayName("test build cache w/ enabled variant filter")
fun testBuildCacheWithVariantFilter(
gradleVersion: GradleVersion,
agpVersion: String,
jdkVersion: JdkVersions.ProvidedJdk,
@TempDir localBuildCacheDir: Path
) {
project(
"kt-50161-androidBuildCacheWithVariantFilter",
gradleVersion,
defaultBuildOptions.copy(
androidVersion = agpVersion,
buildCacheEnabled = true,
warningMode = WarningMode.Summary
),
buildJdk = jdkVersion.location,
) {
enableLocalBuildCache(localBuildCacheDir)
build("assembleDebug") {
assertTasksExecuted(":app:assembleDebug")
getCompileKotlinTasks().forEach { task ->
assertTasksExecuted(task.path)
}
}
build("clean")
build("assembleDebug") {
getCompileKotlinTasks().forEach { task ->
assertTasksFromCache(task.path)
}
}
build("clean")
build("assembleDebug", "-PenableVariantFilter") {
assertOutputContains("enableVariantFilter")
getCompileKotlinTasks().forEach { task ->
assertTasksFromCache(task.path)
}
}
}
}
private fun BuildResult.getCompileKotlinTasks(): List<BuildTask> {
val compileTasks = tasks.filter { it.path.lowercase().contains("compile") && it.path.lowercase().contains("kotlin") }
if (compileTasks.isEmpty()) fail("Expected at least one compile task to be executed. Found $tasks")
return compileTasks
}
}
@@ -0,0 +1,26 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
}
repositories {
mavenLocal()
google()
mavenCentral()
}
android {
compileSdkVersion 31
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,24 @@
package com.revolut.cmdemo
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.revolut.cmdemo", appContext.packageName)
}
}
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.revolut.cmdemo">
</manifest>
@@ -0,0 +1,8 @@
package com.revolut.cmdemo
class Demo {
fun sayIt() {
println("Hello World!")
}
}
@@ -0,0 +1,17 @@
package com.revolut.cmdemo
import org.junit.Test
import org.junit.Assert.*
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
@@ -0,0 +1,31 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenLocal()
google()
mavenCentral()
}
dependencies {
classpath('com.android.tools.build:gradle:' + android_tools_version)
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
subprojects { project ->
project.plugins.withId('com.android.application') {
boolean enableVariantFilter = properties.containsKey("enableVariantFilter")
if (enableVariantFilter) {
logger.quiet("enableVariantFilter")
android {
variantFilter { variant ->
variant.ignore = variant.buildType.name != 'debug'
}
}
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
@@ -0,0 +1,3 @@
android.useAndroidX=true
android.enableJetifier=true
kotlin.code.style=official