|
| 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