-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathGradleFixture.kt
More file actions
87 lines (80 loc) · 3.12 KB
/
GradleFixture.kt
File metadata and controls
87 lines (80 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package datadog.gradle.plugin
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.UnexpectedBuildResultException
import org.intellij.lang.annotations.Language
import org.w3c.dom.Document
import java.io.File
import javax.xml.parsers.DocumentBuilderFactory
/**
* Base fixture for Gradle plugin integration tests.
* Provides common functionality for setting up test projects and running Gradle builds.
*/
internal open class GradleFixture(protected val projectDir: File) {
/**
* Runs Gradle with the specified arguments.
*
* @param args Gradle task names and arguments
* @param expectFailure Whether the build is expected to fail
* @param env Environment variables to set (merged with system environment)
* @return The build result
*/
fun run(vararg args: String, expectFailure: Boolean = false, env: Map<String, String> = emptyMap()): BuildResult {
val runner = GradleRunner.create()
// Use a testkit dir scoped to this fixture's projectDir. The Tooling API always uses a
// daemon and ignores org.gradle.daemon=false. By giving each test its own testkit dir,
// we force a fresh daemon per test — ensuring withEnvironment() vars (e.g.
// MAVEN_REPOSITORY_PROXY) are correctly set on the daemon JVM and not inherited from
// a previously-started daemon with a different test's environment.
.withTestKitDir(file(".testkit"))
.withPluginClasspath()
.withProjectDir(projectDir)
.withEnvironment(System.getenv() + env)
.withArguments(*args)
return try {
if (expectFailure) runner.buildAndFail() else runner.build()
} catch (e: UnexpectedBuildResultException) {
e.buildResult
}
}
/**
* Adds a subproject to the build.
* Updates settings.gradle and creates the build script for the subproject.
*
* @param projectPath The project path (e.g., "dd-java-agent:instrumentation:other")
* @param buildScript The build script content for the subproject
*/
fun addSubproject(projectPath: String, @Language("Groovy") buildScript: String) {
// Add to settings.gradle
val settingsFile = file("settings.gradle")
if (settingsFile.exists()) {
settingsFile.appendText("\ninclude ':$projectPath'")
} else {
settingsFile.writeText("include ':$projectPath'")
}
file("${projectPath.replace(':', '/')}/build.gradle")
.writeText(buildScript.trimIndent())
}
/**
* Writes the root project's build.gradle file.
*
* @param buildScript The build script content for the root project
*/
fun writeRootProject(@Language("Groovy") buildScript: String) {
file("build.gradle").writeText(buildScript.trimIndent())
}
/**
* Parses an XML file into a DOM Document.
*/
fun parseXml(xmlFile: File): Document {
val builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
return builder.parse(xmlFile)
}
/**
* Creates or gets a file in the project directory, ensuring parent directories exist.
*/
protected fun file(path: String): File =
File(projectDir, path).also { file ->
file.parentFile?.mkdirs()
}
}