From 3a3d1210c52f389fc82b2f99303f10f9421f1065 Mon Sep 17 00:00:00 2001
From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com>
Date: Tue, 20 Dec 2022 18:13:31 +0100
Subject: [PATCH] Refactor build scripts (#2116)

* Introduce a flag in gradle.properties to enable/disable testing (and the compilation of the associated dependencies).

* Feature flag testing in all non-test-only build.gradle.kts files.

* Remove dokka
---
 aws/sdk-codegen/build.gradle.kts       | 61 +++++++++++++-----------
 build.gradle.kts                       |  1 -
 codegen-client/build.gradle.kts        | 65 +++++++++++++-------------
 codegen-core/build.gradle.kts          | 65 +++++++++++++-------------
 codegen-server/build.gradle.kts        | 34 ++++++++------
 codegen-server/python/build.gradle.kts | 34 ++++++++------
 gradle.properties                      |  2 +
 7 files changed, 142 insertions(+), 120 deletions(-)

diff --git a/aws/sdk-codegen/build.gradle.kts b/aws/sdk-codegen/build.gradle.kts
index dd5c081b9..bb6f2925a 100644
--- a/aws/sdk-codegen/build.gradle.kts
+++ b/aws/sdk-codegen/build.gradle.kts
@@ -19,18 +19,14 @@ group = "software.amazon.software.amazon.smithy.rust.codegen.smithy"
 version = "0.1.0"
 
 val smithyVersion: String by project
-val kotestVersion: String by project
 
 dependencies {
     implementation(project(":codegen-core"))
     implementation(project(":codegen-client"))
-    runtimeOnly(project(":aws:rust-runtime"))
     implementation("org.jsoup:jsoup:1.14.3")
     implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
     implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
     implementation("software.amazon.smithy:smithy-rules-engine:$smithyVersion")
-    testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
-    testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
 }
 
 val generateAwsRuntimeCrateVersion by tasks.registering {
@@ -52,10 +48,6 @@ tasks.compileKotlin {
     dependsOn(generateAwsRuntimeCrateVersion)
 }
 
-tasks.compileTestKotlin {
-    kotlinOptions.jvmTarget = "1.8"
-}
-
 // Reusable license copySpec
 val licenseSpec = copySpec {
     from("${project.rootDir}/LICENSE")
@@ -78,29 +70,44 @@ val sourcesJar by tasks.creating(Jar::class) {
     from(sourceSets.getByName("main").allSource)
 }
 
-tasks.test {
-    useJUnitPlatform()
-    testLogging {
-        events("passed", "skipped", "failed")
-        exceptionFormat = TestExceptionFormat.FULL
-        showCauses = true
-        showExceptions = true
-        showStackTraces = true
-        showStandardStreams = true
+val isTestingEnabled: String by project
+if (isTestingEnabled.toBoolean()) {
+    val kotestVersion: String by project
+
+    dependencies {
+        runtimeOnly(project(":aws:rust-runtime"))
+        testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
+        testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
     }
-}
 
-// Configure jacoco (code coverage) to generate an HTML report
-tasks.jacocoTestReport {
-    reports {
-        xml.required.set(false)
-        csv.required.set(false)
-        html.outputLocation.set(file("$buildDir/reports/jacoco"))
+    tasks.compileTestKotlin {
+        kotlinOptions.jvmTarget = "1.8"
     }
-}
 
-// Always run the jacoco test report after testing.
-tasks["test"].finalizedBy(tasks["jacocoTestReport"])
+    tasks.test {
+        useJUnitPlatform()
+        testLogging {
+            events("passed", "skipped", "failed")
+            exceptionFormat = TestExceptionFormat.FULL
+            showCauses = true
+            showExceptions = true
+            showStackTraces = true
+            showStandardStreams = true
+        }
+    }
+
+    // Configure jacoco (code coverage) to generate an HTML report
+    tasks.jacocoTestReport {
+        reports {
+            xml.required.set(false)
+            csv.required.set(false)
+            html.outputLocation.set(file("$buildDir/reports/jacoco"))
+        }
+    }
+
+    // Always run the jacoco test report after testing.
+    tasks["test"].finalizedBy(tasks["jacocoTestReport"])
+}
 
 publishing {
     publications {
diff --git a/build.gradle.kts b/build.gradle.kts
index 440e4ca41..316b1c8d9 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -16,7 +16,6 @@ buildscript {
 
 plugins {
     kotlin("jvm") version "1.3.72" apply false
-    id("org.jetbrains.dokka") version "1.7.10"
 }
 
 allprojects {
diff --git a/codegen-client/build.gradle.kts b/codegen-client/build.gradle.kts
index 340051006..ba6ac6ac1 100644
--- a/codegen-client/build.gradle.kts
+++ b/codegen-client/build.gradle.kts
@@ -7,7 +7,6 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat
 
 plugins {
     kotlin("jvm")
-    id("org.jetbrains.dokka")
     jacoco
     `maven-publish`
 }
@@ -20,7 +19,6 @@ group = "software.amazon.smithy.rust.codegen"
 version = "0.1.0"
 
 val smithyVersion: String by project
-val kotestVersion: String by project
 
 dependencies {
     implementation(project(":codegen-core"))
@@ -30,13 +28,6 @@ dependencies {
     implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
     implementation("software.amazon.smithy:smithy-waiters:$smithyVersion")
     implementation("software.amazon.smithy:smithy-rules-engine:$smithyVersion")
-    runtimeOnly(project(":rust-runtime"))
-    testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
-    testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
-}
-
-tasks.compileTestKotlin {
-    kotlinOptions.jvmTarget = "1.8"
 }
 
 tasks.compileKotlin {
@@ -65,36 +56,44 @@ val sourcesJar by tasks.creating(Jar::class) {
     from(sourceSets.getByName("main").allSource)
 }
 
-tasks.test {
-    useJUnitPlatform()
-    testLogging {
-        events("passed", "skipped", "failed")
-        exceptionFormat = TestExceptionFormat.FULL
-        showCauses = true
-        showExceptions = true
-        showStackTraces = true
-        showStandardStreams = true
+val isTestingEnabled: String by project
+if (isTestingEnabled.toBoolean()) {
+    val kotestVersion: String by project
+
+    dependencies {
+        runtimeOnly(project(":rust-runtime"))
+        testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
+        testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
     }
-}
 
-tasks.dokkaHtml.configure {
-    outputDirectory.set(buildDir.resolve("javadoc"))
-}
+    tasks.compileTestKotlin {
+        kotlinOptions.jvmTarget = "1.8"
+    }
 
-// Always build documentation
-tasks["build"].finalizedBy(tasks["dokkaHtml"])
+    tasks.test {
+        useJUnitPlatform()
+        testLogging {
+            events("passed", "skipped", "failed")
+            exceptionFormat = TestExceptionFormat.FULL
+            showCauses = true
+            showExceptions = true
+            showStackTraces = true
+            showStandardStreams = true
+        }
+    }
 
-// Configure jacoco (code coverage) to generate an HTML report
-tasks.jacocoTestReport {
-    reports {
-        xml.required.set(false)
-        csv.required.set(false)
-        html.outputLocation.set(file("$buildDir/reports/jacoco"))
+    // Configure jacoco (code coverage) to generate an HTML report
+    tasks.jacocoTestReport {
+        reports {
+            xml.required.set(false)
+            csv.required.set(false)
+            html.outputLocation.set(file("$buildDir/reports/jacoco"))
+        }
     }
-}
 
-// Always run the jacoco test report after testing.
-tasks["test"].finalizedBy(tasks["jacocoTestReport"])
+    // Always run the jacoco test report after testing.
+    tasks["test"].finalizedBy(tasks["jacocoTestReport"])
+}
 
 publishing {
     publications {
diff --git a/codegen-core/build.gradle.kts b/codegen-core/build.gradle.kts
index d705a62fa..393eb4dfa 100644
--- a/codegen-core/build.gradle.kts
+++ b/codegen-core/build.gradle.kts
@@ -8,7 +8,6 @@ import java.io.ByteArrayOutputStream
 
 plugins {
     kotlin("jvm")
-    id("org.jetbrains.dokka")
     jacoco
     `maven-publish`
 }
@@ -21,7 +20,6 @@ group = "software.amazon.smithy.rust.codegen"
 version = "0.1.0"
 
 val smithyVersion: String by project
-val kotestVersion: String by project
 
 dependencies {
     implementation(kotlin("stdlib-jdk8"))
@@ -31,9 +29,6 @@ dependencies {
     implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
     implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
     implementation("software.amazon.smithy:smithy-waiters:$smithyVersion")
-    runtimeOnly(project(":rust-runtime"))
-    testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
-    testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
 }
 
 fun gitCommitHash(): String {
@@ -78,10 +73,6 @@ tasks.compileKotlin {
     dependsOn(generateSmithyRuntimeCrateVersion)
 }
 
-tasks.compileTestKotlin {
-    kotlinOptions.jvmTarget = "1.8"
-}
-
 // Reusable license copySpec
 val licenseSpec = copySpec {
     from("${project.rootDir}/LICENSE")
@@ -104,36 +95,44 @@ val sourcesJar by tasks.creating(Jar::class) {
     from(sourceSets.getByName("main").allSource)
 }
 
-tasks.test {
-    useJUnitPlatform()
-    testLogging {
-        events("passed", "skipped", "failed")
-        exceptionFormat = TestExceptionFormat.FULL
-        showCauses = true
-        showExceptions = true
-        showStackTraces = true
-        showStandardStreams = true
+val isTestingEnabled: String by project
+if (isTestingEnabled.toBoolean()) {
+    val kotestVersion: String by project
+
+    dependencies {
+        runtimeOnly(project(":rust-runtime"))
+        testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
+        testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
     }
-}
 
-tasks.dokkaHtml.configure {
-    outputDirectory.set(buildDir.resolve("javadoc"))
-}
+    tasks.compileTestKotlin {
+        kotlinOptions.jvmTarget = "1.8"
+    }
 
-// Always build documentation
-tasks["build"].finalizedBy(tasks["dokkaHtml"])
+    tasks.test {
+        useJUnitPlatform()
+        testLogging {
+            events("passed", "skipped", "failed")
+            exceptionFormat = TestExceptionFormat.FULL
+            showCauses = true
+            showExceptions = true
+            showStackTraces = true
+            showStandardStreams = true
+        }
+    }
 
-// Configure jacoco (code coverage) to generate an HTML report
-tasks.jacocoTestReport {
-    reports {
-        xml.required.set(false)
-        csv.required.set(false)
-        html.outputLocation.set(file("$buildDir/reports/jacoco"))
+    // Configure jacoco (code coverage) to generate an HTML report
+    tasks.jacocoTestReport {
+        reports {
+            xml.required.set(false)
+            csv.required.set(false)
+            html.outputLocation.set(file("$buildDir/reports/jacoco"))
+        }
     }
-}
 
-// Always run the jacoco test report after testing.
-tasks["test"].finalizedBy(tasks["jacocoTestReport"])
+    // Always run the jacoco test report after testing.
+    tasks["test"].finalizedBy(tasks["jacocoTestReport"])
+}
 
 publishing {
     publications {
diff --git a/codegen-server/build.gradle.kts b/codegen-server/build.gradle.kts
index d9cea7851..50aa27549 100644
--- a/codegen-server/build.gradle.kts
+++ b/codegen-server/build.gradle.kts
@@ -21,19 +21,15 @@ group = "software.amazon.smithy.rust.codegen.server.smithy"
 version = "0.1.0"
 
 val smithyVersion: String by project
-val kotestVersion: String by project
 
 dependencies {
     implementation(project(":codegen-core"))
     implementation(project(":codegen-client"))
     implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
     implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
-    testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
-    testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
 }
 
 tasks.compileKotlin { kotlinOptions.jvmTarget = "1.8" }
-tasks.compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
 
 // Reusable license copySpec
 val licenseSpec = copySpec {
@@ -55,15 +51,27 @@ val sourcesJar by tasks.creating(Jar::class) {
     from(sourceSets.getByName("main").allSource)
 }
 
-tasks.test {
-    useJUnitPlatform()
-    testLogging {
-        events("passed", "skipped", "failed")
-        exceptionFormat = TestExceptionFormat.FULL
-        showCauses = true
-        showExceptions = true
-        showStackTraces = true
-        showStandardStreams = true
+val isTestingEnabled: String by project
+if (isTestingEnabled.toBoolean()) {
+    val kotestVersion: String by project
+
+    dependencies {
+        testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
+        testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
+    }
+
+    tasks.compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
+
+    tasks.test {
+        useJUnitPlatform()
+        testLogging {
+            events("passed", "skipped", "failed")
+            exceptionFormat = TestExceptionFormat.FULL
+            showCauses = true
+            showExceptions = true
+            showStackTraces = true
+            showStandardStreams = true
+        }
     }
 }
 
diff --git a/codegen-server/python/build.gradle.kts b/codegen-server/python/build.gradle.kts
index 40dba2024..5a23bd5d7 100644
--- a/codegen-server/python/build.gradle.kts
+++ b/codegen-server/python/build.gradle.kts
@@ -21,7 +21,6 @@ group = "software.amazon.smithy.rust.codegen.server.python.smithy"
 version = "0.1.0"
 
 val smithyVersion: String by project
-val kotestVersion: String by project
 
 dependencies {
     implementation(project(":codegen-core"))
@@ -29,12 +28,9 @@ dependencies {
     implementation(project(":codegen-server"))
     implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
     implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
-    testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
-    testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
 }
 
 tasks.compileKotlin { kotlinOptions.jvmTarget = "1.8" }
-tasks.compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
 
 // Reusable license copySpec
 val licenseSpec = copySpec {
@@ -56,15 +52,27 @@ val sourcesJar by tasks.creating(Jar::class) {
     from(sourceSets.getByName("main").allSource)
 }
 
-tasks.test {
-    useJUnitPlatform()
-    testLogging {
-        events("passed", "skipped", "failed")
-        exceptionFormat = TestExceptionFormat.FULL
-        showCauses = true
-        showExceptions = true
-        showStackTraces = true
-        showStandardStreams = true
+val isTestingEnabled: String by project
+if (isTestingEnabled.toBoolean()) {
+    val kotestVersion: String by project
+
+    dependencies {
+        testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
+        testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
+    }
+
+    tasks.compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
+
+    tasks.test {
+        useJUnitPlatform()
+        testLogging {
+            events("passed", "skipped", "failed")
+            exceptionFormat = TestExceptionFormat.FULL
+            showCauses = true
+            showExceptions = true
+            showStackTraces = true
+            showStandardStreams = true
+        }
     }
 }
 
diff --git a/gradle.properties b/gradle.properties
index 319485872..502ab6a3b 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -23,6 +23,8 @@ kotlinVersion=1.6.21
 # testing/utility
 ktlintVersion=0.46.1
 kotestVersion=5.2.3
+# Avoid registering dependencies/plugins/tasks that are only used for testing purposes
+isTestingEnabled=true
 
 # TODO(https://github.com/awslabs/smithy-rs/issues/1068): Once doc normalization
 # is completed, warnings can be prohibited in rustdoc.
-- 
GitLab