Skip to content

Commit f66f516

Browse files
committed
WIP add testing
1 parent 632519d commit f66f516

File tree

7 files changed

+518
-4
lines changed

7 files changed

+518
-4
lines changed

.github/workflows/build-verification.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ jobs:
1717
with:
1818
java-version: '8'
1919
distribution: 'adopt'
20-
- name: Set up Gradle
21-
uses: gradle/actions/setup-gradle@v4
22-
with:
23-
develocity-access-key: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }}
20+
# @todo the init script from setup-gradle interferes with the BVS tests
21+
# - name: Set up Gradle
22+
# uses: gradle/actions/setup-gradle@v4
23+
# with:
24+
# develocity-access-key: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }}
2425
- name: Build with Gradle
2526
run: ./gradlew build
27+
env:
28+
DEVELOCITY_ACCESS_KEY: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }}

build.gradle.kts

+10
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ tasks.assemble {
317317
dependsOn(assembleGradleScripts, assembleMavenScripts, assembleLegacyGradleScripts, assembleLegacyMavenScripts)
318318
}
319319

320+
configurations.consumable("gradleScriptsConsumable") {
321+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("gradle-build-validation-scripts"))
322+
outgoing.artifact(assembleGradleScripts)
323+
}
324+
325+
configurations.consumable("mavenScriptsConsumable") {
326+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("maven-build-validation-scripts"))
327+
outgoing.artifact(assembleMavenScripts)
328+
}
329+
320330
val shellcheckGradleScripts by tasks.registering(Shellcheck::class) {
321331
group = "verification"
322332
description = "Perform quality checks on Gradle build validation scripts using Shellcheck."

gradle.properties

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ org.gradle.parallel=true
44
org.gradle.caching=true
55
org.gradle.configuration-cache=true
66
org.gradle.jvmargs=-Duser.language=en -Duser.country=US -Dfile.encoding=UTF-8
7+
8+
develocityTestingServer=https://ge.solutions-team.gradle.com

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ buildCache {
2727
rootProject.name = "build-validation-scripts"
2828

2929
include("components/configure-gradle-enterprise-maven-extension")
30+
include("tests")
3031

3132
project(":components/configure-gradle-enterprise-maven-extension").name = "configure-gradle-enterprise-maven-extension"

tests/build.gradle.kts

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
4+
plugins {
5+
id("groovy")
6+
id("jvm-test-suite")
7+
}
8+
9+
java {
10+
toolchain {
11+
languageVersion = JavaLanguageVersion.of(8)
12+
vendor = JvmVendorSpec.AZUL
13+
}
14+
}
15+
16+
val gradleScripts = configurations.dependencyScope("gradleScripts") {
17+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("gradle-build-validation-scripts"))
18+
}.get()
19+
20+
val gradleScriptsResolvable = configurations.resolvable("${gradleScripts.name}Resolvable") {
21+
extendsFrom(gradleScripts)
22+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("gradle-build-validation-scripts"))
23+
}
24+
25+
val mavenScripts = configurations.dependencyScope("mavenScripts") {
26+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("maven-build-validation-scripts"))
27+
}.get()
28+
29+
val mavenScriptsResolvable = configurations.resolvable("${mavenScripts.name}Resolvable") {
30+
extendsFrom(mavenScripts)
31+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("maven-build-validation-scripts"))
32+
}
33+
34+
repositories {
35+
mavenCentral()
36+
}
37+
38+
dependencies {
39+
gradleScripts(project(":"))
40+
mavenScripts(project(":"))
41+
}
42+
43+
val test by testing.suites.getting(JvmTestSuite::class) {
44+
useSpock()
45+
dependencies {
46+
implementation(gradleTestKit())
47+
}
48+
49+
targets.configureEach {
50+
testTask {
51+
val develocityKeysFile = gradle.gradleUserHomeDir.resolve("develocity/keys.properties")
52+
val develocityKeysEnv = providers.environmentVariable("DEVELOCITY_ACCESS_KEY")
53+
val develocityTestingServer = providers.gradleProperty("develocityTestingServer")
54+
55+
onlyIf("Has credentials for Develocity testing server") {
56+
val develocityTestingServerHost = develocityTestingServer.get().removePrefix("https://")
57+
(develocityKeysFile.exists() && develocityKeysFile.readText().contains(develocityTestingServerHost))
58+
|| develocityKeysEnv.map { it.contains(develocityTestingServerHost) }.getOrElse(false)
59+
}
60+
61+
jvmArgumentProviders.add(objects.newInstance<Jdk8HomeArgumentProvider>().apply {
62+
jdk8HomeDirectory = javaLauncher.map { it.metadata.installationPath }
63+
})
64+
65+
jvmArgumentProviders.add(objects.newInstance<DevelocityTestUrlArgumentProvider>().apply {
66+
this.develocityTestingServer = develocityTestingServer
67+
})
68+
}
69+
}
70+
}
71+
72+
tasks.processTestResources {
73+
from(gradleScriptsResolvable)
74+
from(mavenScriptsResolvable)
75+
}
76+
77+
abstract class Jdk8HomeArgumentProvider : CommandLineArgumentProvider {
78+
79+
// JDK version is already an input to the test task.
80+
// Its location on disk doesn't matter.
81+
@get:Internal
82+
abstract val jdk8HomeDirectory: DirectoryProperty
83+
84+
override fun asArguments(): List<String> {
85+
return listOf("-Djdk8.home=${jdk8HomeDirectory.get().asFile.absolutePath}")
86+
}
87+
88+
}
89+
90+
abstract class DevelocityTestUrlArgumentProvider : CommandLineArgumentProvider {
91+
92+
@get:Input
93+
abstract val develocityTestingServer: Property<String>
94+
95+
override fun asArguments(): List<String> {
96+
return listOf("-Ddevelocity.testing-server=${develocityTestingServer.get()}")
97+
}
98+
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.gradle
2+
3+
import spock.lang.Shared
4+
import spock.lang.Specification
5+
import spock.lang.TempDir
6+
7+
import java.nio.file.Files
8+
import java.util.concurrent.TimeUnit
9+
10+
abstract class BaseScriptsTest extends Specification {
11+
12+
@TempDir
13+
@Shared
14+
private File workingDirectory
15+
String develocityTestUrl = System.getProperty("develocity.testing-server")
16+
17+
// Common 'where' conditions
18+
boolean hasDevelocityConfigured = true
19+
20+
// Test project components
21+
@TempDir
22+
File testProjectDirectory
23+
File gitignore
24+
25+
// Script arguments
26+
String[] tasks = ["build"]
27+
String[] goals = ["verify"]
28+
private File gitRepo
29+
30+
// Outcomes
31+
int exitCode
32+
String output
33+
34+
void setupSpec() {
35+
unpackGradleScripts()
36+
}
37+
38+
private void unpackGradleScripts() {
39+
def gradleScriptsResource = new File(this.class.getResource("/develocity-gradle-build-validation-dev.zip").toURI())
40+
def gradleScriptsArchive = new File(workingDirectory, "develocity-gradle-build-validation-dev.zip")
41+
copy(gradleScriptsResource, gradleScriptsArchive)
42+
unzip(gradleScriptsArchive)
43+
}
44+
45+
void setup() {
46+
gitignore = new File(testProjectDirectory, ".gitignore")
47+
gitRepo = testProjectDirectory
48+
}
49+
50+
String ifDevelocityConfigured(String value) {
51+
return hasDevelocityConfigured ? value : ""
52+
}
53+
54+
static enum Experiment {
55+
GRADLE_EXP_1("01-validate-incremental-building", "exp1-gradle"),
56+
GRADLE_EXP_2("02-validate-local-build-caching-same-location", "exp2-gradle"),
57+
GRADLE_EXP_3("03-validate-local-build-caching-different-locations", "exp3-gradle"),
58+
MAVEN_EXP_1("01-validate-local-build-caching-same-location", "exp1-maven"),
59+
MAVEN_EXP_2("02-validate-local-build-caching-different-locations", "exp2-maven");
60+
61+
static final List<Experiment> ALL_GRADLE_EXPERIMENTS = [GRADLE_EXP_1, GRADLE_EXP_2, GRADLE_EXP_3]
62+
static final List<Experiment> ALL_MAVEN_EXPERIMENTS = [MAVEN_EXP_1, MAVEN_EXP_2]
63+
64+
private final String scriptName
65+
private final String shortName
66+
67+
Experiment(String scriptName, String shortName) {
68+
this.scriptName = scriptName
69+
this.shortName = shortName
70+
}
71+
72+
boolean isGradle() {
73+
return [GRADLE_EXP_1, GRADLE_EXP_2, GRADLE_EXP_3].contains(this)
74+
}
75+
76+
String getContainingDirectory() {
77+
return "develocity-${isGradle() ? "gradle" : "maven"}-build-validation"
78+
}
79+
80+
@Override
81+
String toString() {
82+
return shortName
83+
}
84+
85+
}
86+
87+
void run(Experiment experiment, String... args) {
88+
buildTestProject()
89+
initializeTestProjectRepository()
90+
91+
String[] command = new String[] {
92+
"./${experiment.scriptName}.sh",
93+
"--git-repo", "file://${gitRepo.absolutePath}"
94+
}
95+
command += experiment.isGradle() ? ["--tasks", tasks.join(" ")] : ["--goals", goals.join(" ")]
96+
command += args
97+
println("\n\$ ${command.join(" ")}")
98+
99+
def result = runProcess(new File(workingDirectory, experiment.containingDirectory), command)
100+
exitCode = result.exitCode
101+
output = result.output
102+
}
103+
104+
abstract void buildTestProject()
105+
106+
private void initializeTestProjectRepository() {
107+
runProcess(testProjectDirectory, "git", "init")
108+
runProcess(testProjectDirectory, "git", "config", "user.email", "[email protected]")
109+
runProcess(testProjectDirectory, "git", "config", "user.name", "Bill D. Tual")
110+
runProcess(testProjectDirectory, "git", "add", ".")
111+
runProcess(testProjectDirectory, "git", "commit", "-m", "'Create project'")
112+
}
113+
114+
private static ProcessResult runProcess(File workingDirectory, String... args) {
115+
def processBuilder = new ProcessBuilder(args).directory(workingDirectory).redirectErrorStream(true)
116+
processBuilder.environment()["JAVA_HOME"] = System.getProperty("jdk8.home")
117+
def process = processBuilder.start()
118+
def output = new StringBuilder()
119+
try (def reader = new BufferedReader(new InputStreamReader(process.inputStream))) {
120+
reader.eachLine {
121+
println(it)
122+
output.append(it).append('\n')
123+
}
124+
}
125+
process.waitFor(3, TimeUnit.SECONDS)
126+
return new ProcessResult(process.exitValue(), output.toString())
127+
}
128+
129+
private static void copy(File target, File destination) {
130+
Files.copy(target.toPath(), destination.toPath())
131+
}
132+
133+
private static void unzip(File target) {
134+
runProcess(target.parentFile, "unzip", "-q", "-o", target.name)
135+
}
136+
137+
private static class ProcessResult {
138+
139+
final int exitCode
140+
final String output
141+
142+
ProcessResult(int exitCode, String output) {
143+
this.exitCode = exitCode
144+
this.output = output
145+
}
146+
}
147+
148+
void scriptCompletesSuccessfullyWithSummary() {
149+
assert exitCode == 0
150+
assert output.contains("Summary")
151+
assert output.contains("Performance Characteristics")
152+
assert output.contains("Investigation Quick Links")
153+
}
154+
155+
}

0 commit comments

Comments
 (0)