-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Build tests once and copy to other platforms to run #650
Merged
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4f6251e
WIP
JakeWharton 9199b4c
WIP
JakeWharton 7a8b1f5
WIP
JakeWharton 1743d75
WIP
JakeWharton c1195ba
WIP
JakeWharton 19dfcb3
WIP
JakeWharton 124eb89
WIP
JakeWharton f1bc401
WIP
JakeWharton e2a77ed
WIP
JakeWharton 7f245ea
WIP
JakeWharton f74e632
WIP
JakeWharton f11f3c7
WIP
JakeWharton ce81437
WIP
JakeWharton 6fde470
WIP
JakeWharton 30d49a4
WIP
JakeWharton 2f3a82b
WIP
JakeWharton 2742180
WIP
JakeWharton a6b7c48
WIP
JakeWharton 8f9a581
WIP
JakeWharton e5402be
WIP
JakeWharton 2db8fce
WIP
JakeWharton 11f07ca
WIP
JakeWharton f8b8e04
WIP
JakeWharton 494074e
WIP
JakeWharton 8c6bd45
WIP
JakeWharton a46e736
WIP
JakeWharton 76d0a7a
WIP
JakeWharton 443fbca
WIP
JakeWharton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile | ||
|
||
buildscript { | ||
dependencies { | ||
classpath libs.kotlin.plugin.core | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
} | ||
|
||
apply plugin: 'org.jetbrains.kotlin.jvm' | ||
apply plugin: 'java-gradle-plugin' | ||
|
||
gradlePlugin { | ||
plugins { | ||
mosaicBuild { | ||
id = 'com.jakewharton.mosaic.build' | ||
displayName = "Mosaic Build plugin" | ||
description = "Gradle plugin for Mosaic build things" | ||
implementationClass = "com.jakewharton.mosaic.buildsupport.MosaicBuildPlugin" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly(libs.kotlin.plugin.core) | ||
} | ||
|
||
kotlin { | ||
explicitApi() | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
tasks.withType(JavaCompile).configureEach { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
tasks.withType(KotlinJvmCompile).configureEach { | ||
compilerOptions.jvmTarget = JvmTarget.JVM_1_8 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
rootProject.name = 'build-support' | ||
|
||
dependencyResolutionManagement { | ||
versionCatalogs { | ||
libs { | ||
from(files('../gradle/libs.versions.toml')) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
build-support/src/main/kotlin/com/jakewharton/mosaic/buildsupport/MosaicBuildExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.jakewharton.mosaic.buildsupport | ||
|
||
public interface MosaicBuildExtension { | ||
public fun jvmTestDistribution() | ||
} |
154 changes: 154 additions & 0 deletions
154
...d-support/src/main/kotlin/com/jakewharton/mosaic/buildsupport/MosaicBuildExtensionImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.jakewharton.mosaic.buildsupport | ||
|
||
import java.io.File | ||
import org.gradle.api.Project | ||
import org.gradle.api.file.FileTree | ||
import org.gradle.api.internal.file.FileCollectionFactory | ||
import org.gradle.api.internal.tasks.testing.TestClassProcessor | ||
import org.gradle.api.internal.tasks.testing.TestClassRunInfo | ||
import org.gradle.api.internal.tasks.testing.TestResultProcessor | ||
import org.gradle.api.internal.tasks.testing.detection.ClassFileExtractionManager | ||
import org.gradle.api.internal.tasks.testing.detection.DefaultTestClassScanner | ||
import org.gradle.api.internal.tasks.testing.junit.JUnitDetector | ||
import org.gradle.api.plugins.BasePluginExtension | ||
import org.gradle.api.tasks.Copy | ||
import org.gradle.api.tasks.application.CreateStartScripts | ||
import org.gradle.api.tasks.bundling.Jar | ||
import org.gradle.api.tasks.bundling.Zip | ||
import org.gradle.internal.Factory | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.TEST_COMPILATION_NAME | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType | ||
|
||
internal class MosaicBuildExtensionImpl( | ||
private val project: Project, | ||
) : MosaicBuildExtension { | ||
override fun jvmTestDistribution() { | ||
var gotMpp = false | ||
project.afterEvaluate { | ||
check(gotMpp) { | ||
"JVM test distribution requires the Kotlin multiplatform plugin" | ||
} | ||
} | ||
project.pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { | ||
gotMpp = true | ||
|
||
val gradleSupport: GradleSupport = Gradle_8_10_Support() | ||
|
||
val base = project.extensions.getByType(BasePluginExtension::class.java) | ||
val kotlin = project.extensions.getByType(KotlinMultiplatformExtension::class.java) | ||
kotlin.targets.configureEach { target -> | ||
if (target.platformType != KotlinPlatformType.jvm) return@configureEach | ||
|
||
val name = target.name + "Test" | ||
val nameUpper = name.replaceFirstChar(Char::uppercase) | ||
|
||
val mainJarProvider = project.tasks.named(target.artifactsTaskName) | ||
|
||
val testCompilation = target.compilations.named(TEST_COMPILATION_NAME) | ||
val testClassesProvider = testCompilation.map { it.output.allOutputs } | ||
val testDependenciesProvider = testCompilation.map { | ||
it.runtimeDependencyFiles?.filter { it.isFile } | ||
?: FileCollectionFactory.empty() | ||
} | ||
|
||
val testJarProvider = project.tasks.register("jar$nameUpper", Jar::class.java) { | ||
it.from(testClassesProvider) | ||
it.archiveAppendix.set(target.name) | ||
it.archiveClassifier.set("tests") | ||
} | ||
|
||
val testScriptsProvider = project.tasks.register("scripts$nameUpper", CreateStartScripts::class.java) { | ||
it.outputDir = project.layout.buildDirectory.dir("scripts/$name").get().asFile | ||
it.applicationName = base.archivesName.get() + "-test" | ||
|
||
// The classpath property is not lazy, so we need explicit dependencies here. | ||
it.dependsOn(mainJarProvider) | ||
it.dependsOn(testJarProvider) | ||
it.dependsOn(testDependenciesProvider) | ||
// However, this 'plus' result will be live, and can still be set at configuration time. | ||
val classpath = mainJarProvider.get().outputs.files | ||
.plus(testJarProvider.get().outputs.files) | ||
.plus(testDependenciesProvider.get()) | ||
it.classpath = classpath | ||
|
||
it.mainClass.set( | ||
testClassesProvider.zip(testDependenciesProvider) { testClasses, testDependencies -> | ||
val testFqcns = gradleSupport.detectTestClassNames( | ||
testClasses.asFileTree, | ||
testClasses.files.toList(), | ||
testDependencies.files.toList() | ||
) | ||
"org.junit.runner.JUnitCore ${testFqcns.joinToString(" ") { """"$it"""" }}" | ||
} | ||
) | ||
} | ||
|
||
val installProvider = project.tasks.register("install${nameUpper}Distribution", Copy::class.java) { | ||
it.group = "distribution" | ||
it.description = "Installs $name as a distribution as-is." | ||
|
||
it.into("bin") { | ||
it.from(testScriptsProvider) | ||
} | ||
it.into("lib") { | ||
it.from(testJarProvider) | ||
it.from(mainJarProvider) | ||
it.from(testDependenciesProvider) | ||
} | ||
it.destinationDir = project.layout.buildDirectory.dir("install/$name").get().asFile | ||
} | ||
|
||
project.tasks.register("zip${nameUpper}Distribution", Zip::class.java) { | ||
it.group = "distribution" | ||
it.description = "Bundles $name as a distribution." | ||
|
||
it.from(installProvider) | ||
it.destinationDirectory.set(project.layout.buildDirectory.dir("dist")) | ||
it.archiveAppendix.set(target.name) | ||
it.archiveClassifier.set("tests") | ||
} | ||
|
||
// TODO add to archives? | ||
} | ||
} | ||
} | ||
|
||
interface GradleSupport { | ||
fun detectTestClassNames( | ||
testClasses: FileTree, | ||
testClassDirectories: List<File>, | ||
testClasspath: List<File>, | ||
): List<String> | ||
} | ||
|
||
class Gradle_8_10_Support : GradleSupport { | ||
override fun detectTestClassNames( | ||
testClasses: FileTree, | ||
testClassDirectories: List<File>, | ||
testClasspath: List<File>, | ||
): List<String> { | ||
val detector = JUnitDetector(ClassFileExtractionManager(object : Factory<File> { | ||
override fun create() = File.createTempFile("gradle", "test-class-detection").apply { | ||
deleteOnExit() | ||
} | ||
})) | ||
detector.setTestClasses(testClassDirectories) | ||
detector.setTestClasspath(testClasspath) | ||
|
||
val testFqcns = mutableListOf<String>() | ||
val testClassProcessor = object : TestClassProcessor { | ||
override fun processTestClass(testClass: TestClassRunInfo) { | ||
testFqcns += testClass.testClassName | ||
} | ||
override fun startProcessing(resultProcessor: TestResultProcessor) {} | ||
override fun stop() {} | ||
override fun stopNow() {} | ||
} | ||
|
||
DefaultTestClassScanner(testClasses, detector, testClassProcessor).run() | ||
|
||
return testFqcns | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
build-support/src/main/kotlin/com/jakewharton/mosaic/buildsupport/MosaicBuildPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.jakewharton.mosaic.buildsupport | ||
|
||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
|
||
@Suppress("unused") // Invoked reflectively by Gradle. | ||
public class MosaicBuildPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
target.extensions.add( | ||
MosaicBuildExtension::class.java, | ||
"mosaicBuild", | ||
MosaicBuildExtensionImpl(target), | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're on Gradle 8.12 now, but when I originally wrote this it worked on Gradle 8.10...