Skip to content

Commit

Permalink
IT Android with DGPv2
Browse files Browse the repository at this point in the history
- implement new JUnit extension to help set up and re-run DGP integration tests with different versions
  • Loading branch information
adam-enko committed Oct 24, 2024
1 parent ead23da commit a3919ed
Show file tree
Hide file tree
Showing 65 changed files with 9,792 additions and 150 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ docs-developer/** linguist-doc
examples/** linguist-documentation
dokka-integration-tests/gradle/projects/** linguist-documentation

# exclude test data
dokka-integration-tests/gradle/src/*/expectedData linguist-generated

# Tell Git not to export certain files or directories when generating an archive.
# Since an archive doesn't contain git data, also exclude git metadata files.
.gitattributes export-ignore
Expand Down
10 changes: 9 additions & 1 deletion dokka-integration-tests/gradle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ dependencies {
api(libs.kotlin.test)
api(libs.junit.jupiterApi)
api(libs.junit.jupiterParams)

api(libs.kotest.assertionsCore)
api(gradleTestKit())

val dokkaVersion = project.version.toString()

api(testFixtures("org.jetbrains.dokka:dokka-gradle-plugin:$dokkaVersion"))

// We're using Gradle included-builds and dependency substitution, so we
// need to use the Gradle project name, *not* the published Maven artifact-id
devPublication("org.jetbrains.dokka:plugin-all-modules-page:$dokkaVersion")
Expand Down Expand Up @@ -80,6 +83,11 @@ registerTestProjectSuite(
dokkaBuild.androidSdkDir.orNull?.let { androidSdkDir ->
environment("ANDROID_HOME", androidSdkDir)
}

systemProperty.inputDirectory(
"baseExpectedDataDir",
layout.projectDirectory.dir("src/testTemplateProjectAndroid/expectedData"),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id("com.android.library") /* %{AGP_VERSION} */
id("org.jetbrains.dokka") /* %{DGP_VERSION} */
kotlin("android") /* %{KGP_VERSION} */
}

android {
namespace = "org.jetbrains.dokka.it.android"
defaultConfig {
minSdkVersion(21)
setCompileSdkVersion(29)
}
}

dependencies {
implementation("androidx.appcompat:appcompat:1.1.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
rootProject.name = "it-android-0"

pluginManagement {
repositories {
/* %{DOKKA_IT_MAVEN_REPO}% */
mavenCentral()
google()
gradlePluginPortal()
}
}

@Suppress("UnstableApiUsage")
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
/* %{DOKKA_IT_MAVEN_REPO}% */

mavenCentral()
google()

//region Declare the Node.js & Yarn download repositories - workaround for https://youtrack.jetbrains.com/issue/KT-51379
ivy("https://nodejs.org/dist/") {
name = "Node Distributions at $url"
patternLayout { artifact("v[revision]/[artifact](-v[revision]-[classifier]).[ext]") }
metadataSources { artifact() }
content { includeModule("org.nodejs", "node") }
}
ivy("https://github.com/yarnpkg/yarn/releases/download") {
name = "Yarn Distributions at $url"
patternLayout { artifact("v[revision]/[artifact](-v[revision]).[ext]") }
metadataSources { artifact() }
content { includeModule("com.yarnpkg", "yarn") }
}
//endregion

//region Declare Kotlin/Native dependencies - workaround for https://youtrack.jetbrains.com/issue/KT-51379
// Remove this repo when the only supported KGP version is above 2.0.0
ivy("https://download.jetbrains.com/kotlin/native/builds") {
name = "Kotlin Native"
patternLayout {

// example download URLs:
// https://download.jetbrains.com/kotlin/native/builds/releases/1.7.20/linux-x86_64/kotlin-native-prebuilt-linux-x86_64-1.7.20.tar.gz
// https://download.jetbrains.com/kotlin/native/builds/releases/1.7.20/windows-x86_64/kotlin-native-prebuilt-windows-x86_64-1.7.20.zip
// https://download.jetbrains.com/kotlin/native/builds/releases/1.7.20/macos-x86_64/kotlin-native-prebuilt-macos-x86_64-1.7.20.tar.gz
listOf(
"macos-x86_64",
"macos-aarch64",
"osx-x86_64",
"osx-aarch64",
"linux-x86_64",
"windows-x86_64"
).forEach { os ->
listOf("dev", "releases").forEach { stage ->
artifact("$stage/[revision]/$os/[artifact]-[revision].[ext]")
}
}
}
metadataSources { artifact() }
content { includeModuleByRegex(".*", ".*kotlin-native-prebuilt.*") }
}
//endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--
~ Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
-->

<manifest package="org.jetbrains.dokka.it.android"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@file:Suppress("unused")

package it.android

import android.content.Context
import android.util.SparseIntArray
import android.view.View

/**
* This class is specific to android and uses android classes like:
* [Context], [SparseIntArray] or [View]
*/
class AndroidSpecificClass {
fun sparseIntArray() = SparseIntArray()
fun createView(context: Context): View = View(context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package it.android

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

/**
* Some Activity implementing [AppCompatActivity] from android x
*/
class IntegrationTestActivity : AppCompatActivity() {
/**
* Will show a small happy text
*/
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val textView = TextView(this)
textView.text = "I am so happy :)"
setContentView(textView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import java.io.File
import java.net.URI
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.copyTo
import kotlin.io.path.copyToRecursively
import kotlin.io.path.exists
import kotlin.io.path.invariantSeparatorsPathString
import kotlin.io.path.*
import kotlin.test.BeforeTest
import kotlin.time.Duration.Companion.seconds

Expand All @@ -37,7 +34,7 @@ abstract class AbstractGradleIntegrationTest : AbstractIntegrationTest() {
) {
templateProjectDir.copyToRecursively(destination.toPath(), followLinks = false, overwrite = true)
templateSettingsGradleKts.copyTo(destination.resolve("template.settings.gradle.kts").toPath(), overwrite = true)
destination.updateProjectLocalMavenDir()
destination.toPath().updateProjectLocalMavenDir()
}

fun createGradleRunner(
Expand Down Expand Up @@ -84,19 +81,7 @@ abstract class AbstractGradleIntegrationTest : AbstractIntegrationTest() {
.withJetBrainsCachedGradleVersion(buildVersions.gradleVersion)
.withTestKitDir(File("build", "gradle-test-kit").absoluteFile)
.withDebug(TestEnvironment.isEnabledDebug)
.apply {
withEnvironment(
buildMap {
// `withEnvironment()` will wipe all existing environment variables,
// which breaks things like ANDROID_HOME and PATH, so re-add them.
putAll(System.getenv())

if (hostGradleDependenciesCache.exists()) {
put("GRADLE_RO_DEP_CACHE", hostGradleDependenciesCache.invariantSeparatorsPathString)
}
}
)
}
.withReadOnlyDependencyCache()
.withArguments(
buildList {

Expand Down Expand Up @@ -190,7 +175,7 @@ abstract class AbstractGradleIntegrationTest : AbstractIntegrationTest() {
* Note: Currently all Gradle versions store caches in `$GRADLE_USER_HOME/caches/`,
* but this might change. Check the docs.
*/
private val hostGradleDependenciesCache: Path by lazy {
internal val hostGradleDependenciesCache: Path by lazy {
hostGradleUserHome.resolve("caches")
}

Expand Down Expand Up @@ -239,14 +224,14 @@ abstract class AbstractGradleIntegrationTest : AbstractIntegrationTest() {
""".trimMargin()
}

fun File.updateProjectLocalMavenDir() {
fun Path.updateProjectLocalMavenDir() {

val dokkaMavenRepoMarker = "/* %{DOKKA_IT_MAVEN_REPO}% */"

// Exclusive repository containing local Dokka artifacts.
// Must be compatible with both Groovy and Kotlin DSL.

walk().filter { it.isFile }.forEach { file ->
walk().filter { it.isRegularFile() }.forEach { file ->
val fileText = file.readText()

if (dokkaMavenRepoMarker in fileText) {
Expand All @@ -260,10 +245,30 @@ abstract class AbstractGradleIntegrationTest : AbstractIntegrationTest() {
}

private fun GradleRunner.withJetBrainsCachedGradleVersion(version: GradleVersion): GradleRunner =
withJetBrainsCachedGradleVersion(version.version)

internal fun GradleRunner.withJetBrainsCachedGradleVersion(version: String): GradleRunner =
withGradleDistribution(
URI("https://cache-redirector.jetbrains.com/services.gradle.org/distributions/gradle-${version.version}-bin.zip")
URI("https://cache-redirector.jetbrains.com/services.gradle.org/distributions/gradle-${version}-bin.zip")
)

internal fun GradleRunner.withReadOnlyDependencyCache(
hostGradleDependenciesCache: Path = AbstractGradleIntegrationTest.hostGradleDependenciesCache,
): GradleRunner =
apply {
withEnvironment(
buildMap {
// `withEnvironment()` will wipe all existing environment variables,
// which breaks things like ANDROID_HOME and PATH, so re-add them.
putAll(System.getenv())

if (hostGradleDependenciesCache.exists()) {
put("GRADLE_RO_DEP_CACHE", hostGradleDependenciesCache.invariantSeparatorsPathString)
}
}
)
}

private fun Throwable.withAllCauses(): Sequence<Throwable> {
val root = this
return sequence {
Expand Down
Loading

0 comments on commit a3919ed

Please sign in to comment.