|
| 1 | +package datadog.gradle.plugin.version |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions.assertThat |
| 4 | +import org.gradle.testkit.runner.GradleRunner |
| 5 | +import org.junit.jupiter.api.Test |
| 6 | +import org.junit.jupiter.api.io.CleanupMode |
| 7 | +import org.junit.jupiter.api.io.TempDir |
| 8 | +import java.io.File |
| 9 | +import java.io.IOException |
| 10 | + |
| 11 | + |
| 12 | +class TracerVersionIntegrationTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun `should use default version when not under a git clone`(@TempDir projectDir: File) { |
| 16 | + assertTracerVersion(projectDir, "0.1.0-SNAPSHOT") |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + fun `should use default version when no git tags`(@TempDir projectDir: File) { |
| 21 | + assertTracerVersion( |
| 22 | + projectDir, |
| 23 | + "0.1.0-SNAPSHOT", |
| 24 | + beforeGradle = { |
| 25 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 26 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 27 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 28 | + exec(projectDir, "git", "add", "-A") |
| 29 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 30 | + } |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `should ignore dirtiness when no git tags`(@TempDir projectDir: File) { |
| 36 | + assertTracerVersion( |
| 37 | + projectDir, |
| 38 | + "0.1.0-SNAPSHOT", |
| 39 | + beforeGradle = { |
| 40 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 41 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 42 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 43 | + exec(projectDir, "git", "add", "-A") |
| 44 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 45 | + |
| 46 | + File(projectDir, "settings.gradle.kts").appendText(""" |
| 47 | + |
| 48 | + // uncommitted change this file, |
| 49 | + """.trimIndent()) |
| 50 | + } |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `should use default version when unmatching git tags`(@TempDir projectDir: File) { |
| 56 | + assertTracerVersion( |
| 57 | + projectDir, |
| 58 | + "0.1.0-SNAPSHOT", |
| 59 | + beforeGradle = { |
| 60 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 61 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 62 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 63 | + exec(projectDir, "git", "add", "-A") |
| 64 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 65 | + exec(projectDir, "git", "tag", "something1.40.1", "-m", "Not our tag") |
| 66 | + } |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `should use exact version when on tag`(@TempDir projectDir: File) { |
| 72 | + assertTracerVersion( |
| 73 | + projectDir, |
| 74 | + "1.52.0", |
| 75 | + beforeGradle = { |
| 76 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 77 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 78 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 79 | + exec(projectDir, "git", "add", "-A") |
| 80 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 81 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 82 | + } |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `should increment minor and mark dirtiness`(@TempDir projectDir: File) { |
| 88 | + assertTracerVersion( |
| 89 | + projectDir, |
| 90 | + "1.53.0-SNAPSHOT-DIRTY", |
| 91 | + beforeGradle = { |
| 92 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 93 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 94 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 95 | + exec(projectDir, "git", "add", "-A") |
| 96 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 97 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 98 | + |
| 99 | + File(projectDir, "settings.gradle.kts").appendText(""" |
| 100 | + |
| 101 | + // uncommitted change this file, |
| 102 | + """.trimIndent()) |
| 103 | + } |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `should ignore dirtiness if CI env`(@TempDir projectDir: File) { // CI patch some tracked files |
| 109 | + assertTracerVersion( |
| 110 | + projectDir, |
| 111 | + "1.52.0", |
| 112 | + beforeGradle = { |
| 113 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 114 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 115 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 116 | + exec(projectDir, "git", "add", "-A") |
| 117 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 118 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 119 | + |
| 120 | + // dirty file ignored |
| 121 | + File(projectDir, "settings.gradle.kts").appendText(""" |
| 122 | + |
| 123 | + // uncommitted change this file, |
| 124 | + """.trimIndent()) |
| 125 | + }, |
| 126 | + additionalEnv = mapOf("CI" to "true") |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + fun `should increment minor with added commits after version tag`(@TempDir projectDir: File) { |
| 132 | + assertTracerVersion( |
| 133 | + projectDir, |
| 134 | + "1.53.0-SNAPSHOT", |
| 135 | + beforeGradle = { |
| 136 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 137 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 138 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 139 | + exec(projectDir, "git", "add", "-A") |
| 140 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 141 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 142 | + |
| 143 | + File(projectDir, "settings.gradle.kts").appendText( |
| 144 | + """ |
| 145 | + |
| 146 | + // Committed change this file, |
| 147 | + """.trimIndent() |
| 148 | + ) |
| 149 | + exec(projectDir, "git", "commit", "-am", "Another commit") |
| 150 | + } |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + fun `should increment minor with snapshot and dirtiness with added commits after version tag and dirty`(@TempDir projectDir: File) { |
| 156 | + assertTracerVersion( |
| 157 | + projectDir, |
| 158 | + "1.53.0-SNAPSHOT-DIRTY", |
| 159 | + beforeGradle = { |
| 160 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 161 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 162 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 163 | + exec(projectDir, "git", "add", "-A") |
| 164 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 165 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 166 | + |
| 167 | + val settingsFile = File(projectDir, "settings.gradle.kts") |
| 168 | + settingsFile.appendText(""" |
| 169 | + |
| 170 | + // uncommitted change |
| 171 | + """.trimIndent()) |
| 172 | + |
| 173 | + exec(projectDir, "git", "commit", "-am", "Another commit") |
| 174 | + |
| 175 | + settingsFile.appendText(""" |
| 176 | + // An uncommitted modification |
| 177 | + """.trimIndent()) |
| 178 | + } |
| 179 | + ) |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + fun `should increment patch on release branch and no patch release tag`(@TempDir projectDir: File) { |
| 184 | + assertTracerVersion( |
| 185 | + projectDir, |
| 186 | + "1.52.1-SNAPSHOT", |
| 187 | + beforeGradle = { |
| 188 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 189 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 190 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 191 | + exec(projectDir, "git", "add", "-A") |
| 192 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 193 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 194 | + |
| 195 | + val settingsFile = File(projectDir, "settings.gradle.kts") |
| 196 | + settingsFile.appendText(""" |
| 197 | + |
| 198 | + // Committed change |
| 199 | + """.trimIndent()) |
| 200 | + |
| 201 | + exec(projectDir, "git", "commit", "-am", "Another commit") |
| 202 | + exec(projectDir, "git", "switch", "-c", "release/v1.52.x") |
| 203 | + } |
| 204 | + ) |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + fun `should increment patch on release branch and with previous patch release tag`(@TempDir projectDir: File) { |
| 209 | + assertTracerVersion( |
| 210 | + projectDir, |
| 211 | + "1.52.2-SNAPSHOT", |
| 212 | + beforeGradle = { |
| 213 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 214 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 215 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 216 | + exec(projectDir, "git", "add", "-A") |
| 217 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 218 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 219 | + |
| 220 | + val settingsFile = File(projectDir, "settings.gradle.kts") |
| 221 | + settingsFile.appendText(""" |
| 222 | + |
| 223 | + // Committed change |
| 224 | + """.trimIndent()) |
| 225 | + exec(projectDir, "git", "commit", "-am", "Another commit") |
| 226 | + exec(projectDir, "git", "tag", "v1.52.1", "-m", "") |
| 227 | + |
| 228 | + settingsFile.appendText(""" |
| 229 | + |
| 230 | + // Another committed change |
| 231 | + """.trimIndent()) |
| 232 | + exec(projectDir, "git", "commit", "-am", "Another commit") |
| 233 | + exec(projectDir, "git", "switch", "-c", "release/v1.52.x") |
| 234 | + } |
| 235 | + ) |
| 236 | + } |
| 237 | + |
| 238 | + |
| 239 | + @Test |
| 240 | + fun `should compute version on worktrees`(@TempDir projectDir: File, @TempDir workTreeDir: File) { |
| 241 | + assertTracerVersion( |
| 242 | + projectDir, |
| 243 | + "1.53.0-SNAPSHOT", |
| 244 | + beforeGradle = { |
| 245 | + exec(projectDir, "git", "init", "--initial-branch", "main") |
| 246 | + exec(projectDir, "git", "config", "user.email", "[email protected]") |
| 247 | + exec(projectDir, "git", "config", "user.name", "Test") |
| 248 | + exec(projectDir, "git", "add", "-A") |
| 249 | + exec(projectDir, "git", "commit", "-m", "A commit") |
| 250 | + exec(projectDir, "git", "tag", "v1.52.0", "-m", "") |
| 251 | + |
| 252 | + exec(projectDir, "git", "commit", "-m", "Initial commit", "--allow-empty") |
| 253 | + exec(projectDir, "git", "worktree", "add", workTreeDir.absolutePath) |
| 254 | + // happening on the worktree |
| 255 | + File(workTreeDir, "settings.gradle.kts").appendText( |
| 256 | + """ |
| 257 | + |
| 258 | + // Committed change this file, |
| 259 | + """.trimIndent() |
| 260 | + ) |
| 261 | + exec(workTreeDir, "git", "commit", "-am", "Another commit") |
| 262 | + }, |
| 263 | + workingDirectory = workTreeDir |
| 264 | + ) |
| 265 | + } |
| 266 | + |
| 267 | + private fun assertTracerVersion( |
| 268 | + projectDir: File, |
| 269 | + expectedVersion: String, |
| 270 | + beforeGradle: () -> Unit = {}, |
| 271 | + workingDirectory: File = projectDir, |
| 272 | + additionalEnv: Map<String, String> = mapOf("CI" to "false"), |
| 273 | + ) { |
| 274 | + File(projectDir, "settings.gradle.kts").writeText( |
| 275 | + """ |
| 276 | + rootProject.name = "test-project" |
| 277 | + """.trimIndent() |
| 278 | + ) |
| 279 | + File(projectDir, "build.gradle.kts").writeText( |
| 280 | + """ |
| 281 | + plugins { |
| 282 | + id("tracer-version") |
| 283 | + } |
| 284 | + |
| 285 | + tasks.register("printVersion") { |
| 286 | + logger.quiet(project.version.toString()) |
| 287 | + } |
| 288 | +
|
| 289 | + group = "datadog.tracer.version.test" |
| 290 | + """.trimIndent() |
| 291 | + ) |
| 292 | + |
| 293 | + beforeGradle() |
| 294 | + |
| 295 | + val buildResult = GradleRunner.create() |
| 296 | + .forwardOutput() |
| 297 | + // .withGradleVersion(gradleVersion) // Use current gradle version |
| 298 | + .withPluginClasspath() |
| 299 | + .withArguments("printVersion", "--quiet") |
| 300 | + .withEnvironment(System.getenv() + additionalEnv) |
| 301 | + .withProjectDir(workingDirectory) |
| 302 | + // .withDebug(true) |
| 303 | + .build() |
| 304 | + |
| 305 | + assertThat(buildResult.output).isEqualToIgnoringNewLines(expectedVersion) |
| 306 | + } |
| 307 | + |
| 308 | + private fun exec(workingDirectory: File, vararg args: String) { |
| 309 | + val exitCode = ProcessBuilder() |
| 310 | + .command(*args) |
| 311 | + .directory(workingDirectory) |
| 312 | + .inheritIO() |
| 313 | + .start() |
| 314 | + .waitFor() |
| 315 | + |
| 316 | + if (exitCode != 0) { |
| 317 | + throw IOException(String.format("Process failed: %s Exit code %d", args.joinToString(" "), exitCode)) |
| 318 | + } |
| 319 | + } |
| 320 | +} |
0 commit comments