From 04a471aca42f9a7bfca90af3ecab0c0cf7c88161 Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Fri, 13 Dec 2024 09:40:24 +0100 Subject: [PATCH] build: add test for caching of rust build tasks (#39) Signed-off-by: Jendrik Johannes --- .../org/hiero/gradle/test/RustTasksTest.kt | 79 ++++++++++++++++--- .../gradle/test/fixtures/GradleProject.kt | 7 +- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/test/kotlin/org/hiero/gradle/test/RustTasksTest.kt b/src/test/kotlin/org/hiero/gradle/test/RustTasksTest.kt index bc0b118..b1f0aab 100644 --- a/src/test/kotlin/org/hiero/gradle/test/RustTasksTest.kt +++ b/src/test/kotlin/org/hiero/gradle/test/RustTasksTest.kt @@ -9,25 +9,57 @@ import org.junit.jupiter.api.Test class RustTasksTest { - @Test - fun `installRustToolchains installs all toolchains defined in CargoToolchain`() { - val p = GradleProject().withMinimalStructure() - val rustToolchainsDir = p.file("product/module-a/build/rust-toolchains/rustup/toolchains") - p.moduleBuildFile("""plugins { id("org.hiero.gradle.feature.rust") }""") - p.toolchainVersionsFile( - """ + private val toolchainVersions = + """ jdk=17.0.12 rust=1.81.0 cargo-zigbuild=0.19.5 zig=0.13.0 xwin=0.6.5 """ - .trimIndent() + .trimIndent() + + private val cargoToml = + """ + [package] + name = "module-a" + version = "0.0.1" + edition = "2021" + [lib] + path = "src/main/rust/lib.rs" + crate-type = ["cdylib"] + """ + .trimIndent() + + // Test asserts multiple things in one method as installing the toolchains is expensive. + @Test + fun `rust toolchain installation and caching works`() { + val push = GradleProject().withMinimalStructure() + val pull = GradleProject().withMinimalStructure() + push.settingsFile.appendText( + """buildCache.local.directory = File("${push.file("build-cache").absolutePath}")""" ) + pull.settingsFile.appendText( + """buildCache.local.directory = File("${push.file("build-cache").absolutePath}")""" + ) + push.moduleBuildFile("""plugins { id("org.hiero.gradle.feature.rust") }""") + pull.moduleBuildFile("""plugins { id("org.hiero.gradle.feature.rust") }""") + push.toolchainVersionsFile(toolchainVersions) + pull.toolchainVersionsFile(toolchainVersions) + push.file("product/module-a/src/main/rust/lib.rs", "pub fn public_api() {}") + pull.file("product/module-a/src/main/rust/lib.rs", "pub fn public_api() {}") + push.file("product/module-a/Cargo.toml", cargoToml) + pull.file("product/module-a/Cargo.toml", cargoToml) + + val rustToolchainsDir = + push.file("product/module-a/build/rust-toolchains/rustup/toolchains") - val result = p.run("installRustToolchains") + val pushResult = push.run("assemble --build-cache") + val pullResult = pull.run("assemble --build-cache -PskipInstallRustToolchains=true") - assertThat(result.output).doesNotContain("warning: Force-skipping unavailable component") + // installRustToolchains installs all toolchains defined in CargoToolchain + assertThat(pushResult.output) + .doesNotContain("warning: Force-skipping unavailable component") CargoToolchain.values().forEach { toolchain -> val toolchainDir = rustToolchainsDir @@ -36,7 +68,32 @@ class RustTasksTest { .single() assertThat(toolchainDir).isNotEmptyDirectory() } - assertThat(result.task(":module-a:installRustToolchains")?.outcome) + assertThat(pushResult.task(":module-a:installRustToolchains")?.outcome) + .isEqualTo(TaskOutcome.SUCCESS) + + // rust build results are taken FROM-CACHE and installRustToolchains can be skipped + assertThat(pushResult.task(":module-a:installRustToolchains")?.outcome) + .isEqualTo(TaskOutcome.SUCCESS) + assertThat(pullResult.task(":module-a:installRustToolchains")).isNull() + assertThat(pushResult.task(":module-a:cargoBuildAarch64Darwin")?.outcome) + .isEqualTo(TaskOutcome.SUCCESS) + assertThat(pullResult.task(":module-a:cargoBuildAarch64Darwin")?.outcome) + .isEqualTo(TaskOutcome.FROM_CACHE) + assertThat(pushResult.task(":module-a:cargoBuildAarch64Linux")?.outcome) + .isEqualTo(TaskOutcome.SUCCESS) + assertThat(pullResult.task(":module-a:cargoBuildAarch64Linux")?.outcome) + .isEqualTo(TaskOutcome.FROM_CACHE) + assertThat(pushResult.task(":module-a:cargoBuildX86Darwin")?.outcome) + .isEqualTo(TaskOutcome.SUCCESS) + assertThat(pullResult.task(":module-a:cargoBuildX86Darwin")?.outcome) + .isEqualTo(TaskOutcome.FROM_CACHE) + assertThat(pushResult.task(":module-a:cargoBuildX86Linux")?.outcome) + .isEqualTo(TaskOutcome.SUCCESS) + assertThat(pullResult.task(":module-a:cargoBuildX86Linux")?.outcome) + .isEqualTo(TaskOutcome.FROM_CACHE) + assertThat(pushResult.task(":module-a:cargoBuildX86Windows")?.outcome) .isEqualTo(TaskOutcome.SUCCESS) + assertThat(pullResult.task(":module-a:cargoBuildX86Windows")?.outcome) + .isEqualTo(TaskOutcome.FROM_CACHE) } } diff --git a/src/test/kotlin/org/hiero/gradle/test/fixtures/GradleProject.kt b/src/test/kotlin/org/hiero/gradle/test/fixtures/GradleProject.kt index 07f46da..ce6472b 100644 --- a/src/test/kotlin/org/hiero/gradle/test/fixtures/GradleProject.kt +++ b/src/test/kotlin/org/hiero/gradle/test/fixtures/GradleProject.kt @@ -31,10 +31,7 @@ class GradleProject { private val expectedHeader = "// SPDX-License-Identifier: Apache-2.0\n" - private val env = - mutableMapOf().also { - it["PATH"] = System.getenv("PATH") // to find 'git' command - } + private val env = mutableMapOf() fun withMinimalStructure(): GradleProject { gradlePropertiesFile.writeText( @@ -142,6 +139,6 @@ class GradleProject { .withProjectDir(projectDir) .withArguments(args + listOf("-s", "--warning-mode=all")) .withDebug(debugMode) - .let { if (debugMode) it else it.withEnvironment(env) } + .let { if (env.isEmpty() || debugMode) it else it.withEnvironment(env) } } }