-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathBuildTimeInstrumentationPluginTest.kt
More file actions
237 lines (195 loc) · 7.55 KB
/
BuildTimeInstrumentationPluginTest.kt
File metadata and controls
237 lines (195 loc) · 7.55 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package datadog.gradle.plugin.instrument
import net.bytebuddy.utility.OpenedClassReader
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.FieldVisitor
import org.objectweb.asm.Opcodes
import java.io.File
import java.io.FileInputStream
class BuildTimeInstrumentationPluginTest {
private val buildGradle = """
plugins {
id 'java'
id 'dd-trace-java.build-time-instrumentation'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
repositories {
mavenCentral()
}
dependencies {
compileOnly group: 'net.bytebuddy', name: 'byte-buddy', version: '1.18.8' // just to build TestPlugin
}
buildTimeInstrumentation.plugins = [
'TestPlugin'
]
""".trimIndent()
private val exampleCode = """
package example;
public class ExampleCode {}
""".trimIndent()
@TempDir
lateinit var buildDir: File
@Test
fun `test instrument plugin`() {
val buildFile = File(buildDir, "build.gradle")
buildFile.writeText(buildGradle)
val srcMainJava = testPlugin("src/main/java", "ExampleCode")
val examplePackageDir = File(srcMainJava, "example").apply { mkdirs() }
File(examplePackageDir, "ExampleCode.java").writeText(exampleCode)
// Run Gradle build with TestKit
GradleRunner.create().withTestKitDir(File(buildDir, ".gradle-test-kit")) // workaround in case the global test-kit cache becomes corrupted
.withDebug(true) // avoids starting daemon which can leave undeleted files post-cleanup
.withProjectDir(buildDir)
.withArguments("build", "--stacktrace")
.withPluginClasspath()
.forwardOutput()
.build()
assertInstrumented(File(buildDir, "build/classes/java/main/example/ExampleCode.class"))
}
@Test
fun `test instrument plugin processes includeClassDirectories`() {
val buildFile = File(buildDir, "build.gradle")
buildFile.writeText("""
plugins {
id 'java'
id 'dd-trace-java.build-time-instrumentation'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
repositories {
mavenCentral()
}
dependencies {
compileOnly group: 'net.bytebuddy', name: 'byte-buddy', version: '1.18.8'
}
buildTimeInstrumentation {
plugins = ['TestPlugin']
includeClassDirectories.from(file('external-classes'))
}
""".trimIndent())
testPlugin("src/main/java", "ExternalCode")
// Pre-compile ExternalCode using ASM and place it in the external-classes directory
val externalClassesDir = File(buildDir, "external-classes").apply { mkdirs() }
precompiledClass("ExternalCode", externalClassesDir)
GradleRunner.create()
.withTestKitDir(File(buildDir, ".gradle-test-kit"))
.withDebug(true)
.withProjectDir(buildDir)
.withArguments("build", "--stacktrace")
.withPluginClasspath()
.forwardOutput()
.build()
// ExternalCode.class should have been copied from external-classes, instrumented, and placed in the output
assertInstrumented(File(buildDir, "build/classes/java/main/ExternalCode.class"))
}
@Test
fun `test rerun-tasks does not lose includeClassDirectories classes`() {
val buildFile = File(buildDir, "build.gradle")
buildFile.writeText("""
plugins {
id 'java'
id 'dd-trace-java.build-time-instrumentation'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
repositories {
mavenCentral()
}
dependencies {
compileOnly group: 'net.bytebuddy', name: 'byte-buddy', version: '1.18.8'
}
buildTimeInstrumentation {
plugins = ['TestPlugin']
includeClassDirectories.from(file('external-classes'))
}
""".trimIndent())
val srcMainJava = testPlugin("src/main/java", "ExampleCode", "ExternalCode")
val examplePackageDir = File(srcMainJava, "example").apply { mkdirs() }
File(examplePackageDir, "ExampleCode.java").writeText("package example; public class ExampleCode {}")
val externalClassesDir = File(buildDir, "external-classes").apply { mkdirs() }
precompiledClass("ExternalCode", externalClassesDir)
val runner = GradleRunner.create()
.withTestKitDir(File(buildDir, ".gradle-test-kit"))
.withDebug(true)
.withProjectDir(buildDir)
.withPluginClasspath()
.forwardOutput()
// First build
runner.withArguments("build", "--stacktrace").build()
// Second build with --rerun-tasks: compileJava wipes classesDirectory, so without
// the fix InstrumentAction would only sync freshly-compiled classes and lose ExternalCode.class
runner.withArguments("build", "--rerun-tasks", "--stacktrace").build()
assertInstrumented(File(buildDir, "build/classes/java/main/example/ExampleCode.class"))
assertInstrumented(File(buildDir, "build/classes/java/main/ExternalCode.class"))
}
private fun testPlugin(srcDir: String, vararg classNames: String): File {
val dir = File(buildDir, srcDir).apply { mkdirs() }
val conditions = classNames.joinToString(" || ") { "\"$it\".equals(name)" }
File(dir, "TestPlugin.java").writeText("""
import java.io.File;
import java.io.IOException;
import net.bytebuddy.build.Plugin;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
public class TestPlugin implements Plugin {
private final File targetDir;
public TestPlugin(File targetDir) {
this.targetDir = targetDir;
}
@Override
public boolean matches(TypeDescription target) {
String name = target.getSimpleName();
return $conditions;
}
@Override
public DynamicType.Builder<?> apply(
DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassFileLocator classFileLocator) {
return builder.defineField("__TEST__FIELD__", Void.class);
}
@Override
public void close() throws IOException {
// no-op
}
}
""".trimIndent())
return dir
}
private fun precompiledClass(className: String, targetDir: File) {
val classWriter = ClassWriter(0)
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null)
classWriter.visitEnd()
File(targetDir, "$className.class").writeBytes(classWriter.toByteArray())
}
private fun assertInstrumented(classFile: File) {
assertTrue(classFile.isFile, "${classFile.name} should be present in the output directory")
var foundInsertedField = false
FileInputStream(classFile).use { input ->
val classReader = ClassReader(input)
classReader.accept(
object : ClassVisitor(OpenedClassReader.ASM_API) {
override fun visitField(access: Int, fieldName: String?, descriptor: String?, signature: String?, value: Any?): FieldVisitor? {
if ("__TEST__FIELD__" == fieldName) foundInsertedField = true
return null
}
},
OpenedClassReader.ASM_API
)
}
assertTrue(foundInsertedField, "${classFile.name} should have been instrumented with __TEST__FIELD__")
}
}