Skip to content

Commit ad19582

Browse files
committed
WIP tests
1 parent 0e68f4d commit ad19582

7 files changed

+513
-4
lines changed

.github/workflows/build-verification.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ 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 }}
29+
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }} # required while injection still uses GE plugin

build.gradle.kts

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

322+
configurations.consumable("gradleScriptsConsumable") {
323+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("gradle-build-validation-scripts"))
324+
outgoing.artifact(assembleGradleScripts)
325+
}
326+
327+
configurations.consumable("mavenScriptsConsumable") {
328+
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named("maven-build-validation-scripts"))
329+
outgoing.artifact(assembleMavenScripts)
330+
}
331+
322332
val shellcheckGradleScripts by tasks.registering(Shellcheck::class) {
323333
group = "verification"
324334
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+
buildValidationTestDevelocityServer=https://ge.solutions-team.gradle.com

settings.gradle.kts

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

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

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

test/build.gradle.kts

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

0 commit comments

Comments
 (0)