-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
SentryPluginTest.kt
335 lines (276 loc) · 9.74 KB
/
SentryPluginTest.kt
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package io.sentry.android.gradle
import io.sentry.android.gradle.extensions.InstrumentationFeature
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue
import org.junit.Assert.assertThrows
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
@Suppress("FunctionName")
@RunWith(Parameterized::class)
class SentryPluginTest(
androidGradlePluginVersion: String,
gradleVersion: String
) : BaseSentryPluginTest(androidGradlePluginVersion, gradleVersion) {
@Test
fun `plugin can be applied`() {
appBuildFile.writeText(
// language=Groovy
"""
plugins {
id "com.android.application"
id "io.sentry.android.gradle"
}
sentry {
autoUploadProguardMapping = false
tracingInstrumentation {
enabled = false
}
}
""".trimIndent()
)
runner.build()
}
@Test
fun `plugin does not configure tasks`() {
val prefix = "task-configured-for-test: "
appBuildFile.writeText(
// language=Groovy
"""
plugins {
id "com.android.application"
id "io.sentry.android.gradle"
}
project.tasks.configureEach { Task task -> println("$prefix" + task.path) }
""".trimIndent()
)
val result = runner.withArguments("help").build()
val configuredTasks = result.output.lines()
.filter { it.startsWith(prefix) }
.map { it.removePrefix(prefix) }
.sorted()
.toMutableList()
// AGP 7.2.x configures the 'clean' task, so ignore it
configuredTasks.remove(":app:clean")
assertTrue(configuredTasks.isEmpty())
}
@Test
fun `regenerates UUID every build`() {
runner.appendArguments(":app:assembleRelease")
runner.build()
val uuid1 = verifyProguardUuid(testProjectDir.root)
runner.build()
val uuid2 = verifyProguardUuid(testProjectDir.root)
assertNotEquals(uuid1, uuid2)
}
@Test
fun `includes a UUID in the APK`() {
runner
.appendArguments(":app:assembleRelease")
.build()
verifyProguardUuid(testProjectDir.root)
}
@Test
fun `does not include a UUID in the APK`() {
// isMinifyEnabled is disabled by default in debug builds
runner
.appendArguments(":app:assembleDebug")
.build()
assertThrows(AssertionError::class.java) {
verifyProguardUuid(testProjectDir.root, variant = "debug", signed = false)
}
}
@Test
fun `does not include a UUID in the APK if includeProguardMapping is off`() {
appBuildFile.writeText(
// language=Groovy
"""
plugins {
id "com.android.application"
id "io.sentry.android.gradle"
}
sentry {
includeProguardMapping = false
}
""".trimIndent()
)
runner
.appendArguments(":app:assembleRelease")
.build()
assertThrows(AssertionError::class.java) {
verifyProguardUuid(testProjectDir.root)
}
}
@Test
fun `creates uploadSentryNativeSymbols task if uploadNativeSymbols is enabled`() {
applyUploadNativeSymbols()
val build = runner
.appendArguments(":app:assembleRelease", "--dry-run")
.build()
assertTrue(":app:uploadSentryNativeSymbolsForRelease" in build.output)
}
@Test
fun `does not create uploadSentryNativeSymbols task if non debuggable app`() {
applyUploadNativeSymbols()
val build = runner
.appendArguments(":app:assembleDebug", "--dry-run")
.build()
assertFalse(":app:uploadSentryNativeSymbolsForDebug" in build.output)
}
@Test
fun `skips variant if set with ignoredVariants`() {
applyIgnores(ignoredVariant = "release")
val build = runner
.appendArguments(":app:assembleRelease", "--dry-run")
.build()
assertFalse(":app:uploadSentryProguardMappingsRelease" in build.output)
}
@Test
fun `does not skip variant if ignoredVariants specifies another value`() {
applyIgnores(ignoredVariant = "debug")
val build = runner
.appendArguments(":app:assembleRelease", "--dry-run")
.build()
assertTrue(":app:uploadSentryProguardMappingsRelease" in build.output)
}
@Test
fun `skips tracing instrumentation if tracingInstrumentation is disabled`() {
applyTracingInstrumentation(false)
val build = runner
.appendArguments(":app:assembleRelease", "--dry-run")
.build()
assertFalse(":app:transformReleaseClassesWithAsm" in build.output)
}
@Test
fun `register tracing instrumentation if tracingInstrumentation is enabled`() {
applyTracingInstrumentation()
val build = runner
.appendArguments(":app:assembleRelease", "--dry-run")
.build()
assertTrue(":app:transformReleaseClassesWithAsm" in build.output)
}
@Test
fun `applies only DB instrumentables when only DATABASE feature enabled`() {
applyTracingInstrumentation(features = setOf(InstrumentationFeature.DATABASE))
val build = runner
.appendArguments(":app:assembleDebug", "--debug")
.build()
assertTrue {
"[sentry] Instrumentables: AndroidXSQLiteDatabase, AndroidXSQLiteStatement," +
" AndroidXRoomDao" in build.output
}
}
@Test
fun `applies only FILE_IO instrumentables when only FILE_IO feature enabled`() {
applyTracingInstrumentation(features = setOf(InstrumentationFeature.FILE_IO))
val build = runner
.appendArguments(":app:assembleDebug", "--debug")
.build()
assertTrue {
"[sentry] Instrumentables: ChainedInstrumentable" in build.output
}
}
@Test
fun `applies all instrumentables when all features enabled`() {
applyTracingInstrumentation(
features = setOf(InstrumentationFeature.DATABASE, InstrumentationFeature.FILE_IO)
)
val build = runner
.appendArguments(":app:assembleDebug", "--debug")
.build()
assertTrue {
"[sentry] Instrumentables: AndroidXSQLiteDatabase, AndroidXSQLiteStatement," +
" AndroidXRoomDao, ChainedInstrumentable" in build.output
}
}
@Test
fun `instruments okhttp v3`() {
applyTracingInstrumentation(features = setOf(InstrumentationFeature.OKHTTP), debug = true)
appBuildFile.appendText(
// language=Groovy
"""
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.14.9'
implementation 'io.sentry:sentry-android-okhttp:5.5.0'
}
""".trimIndent()
)
runner
.appendArguments(":app:assembleDebug")
.build()
// since it's an integration test, we just test that the log file was created for the class
// meaning our CommonClassVisitor has visited and instrumented it
val debugOutput =
testProjectDir.root.resolve("app/build/tmp/sentry/RealCall-instrumentation.log")
assertTrue { debugOutput.exists() && debugOutput.length() > 0 }
}
private fun applyUploadNativeSymbols() {
appBuildFile.writeText(
// language=Groovy
"""
plugins {
id "com.android.application"
id "io.sentry.android.gradle"
}
sentry {
autoUploadProguardMapping = false
uploadNativeSymbols = true
tracingInstrumentation {
enabled = false
}
}
""".trimIndent()
)
}
private fun applyIgnores(ignoredVariant: String) {
appBuildFile.writeText(
// language=Groovy
"""
plugins {
id "com.android.application"
id "io.sentry.android.gradle"
}
sentry {
autoUploadProguardMapping = true
ignoredVariants = ["$ignoredVariant"]
tracingInstrumentation {
enabled = false
}
}
""".trimIndent()
)
}
private fun applyTracingInstrumentation(
tracingInstrumentation: Boolean = true,
features: Set<InstrumentationFeature> = setOf(),
debug: Boolean = false
) {
appBuildFile.writeText(
// language=Groovy
"""
plugins {
id "com.android.application"
id "io.sentry.android.gradle"
}
dependencies {
implementation 'io.sentry:sentry-android:5.5.0'
}
sentry {
autoUploadProguardMapping = false
tracingInstrumentation {
forceInstrumentDependencies = true
enabled = $tracingInstrumentation
debug = $debug
features = ${
features.joinToString(
prefix = "[",
postfix = "]"
) { "${it::class.java.canonicalName}.${it.name}" }
}
}
}
""".trimIndent()
)
}
}