Skip to content

Commit 6a2af45

Browse files
runningcodeclaude
andauthored
feat(snapshots): Add generateTests flag to disable preview scanning (EME-1039) (#1146)
Customers who already have Paparazzi snapshot tests and just want to upload them can now set `generateTests = false` to prevent the plugin from auto-discovering Compose previews and generating test code. The upload task remains wired to recordPaparazzi regardless of this setting. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bb29e02 commit 6a2af45

3 files changed

Lines changed: 90 additions & 35 deletions

File tree

plugin-build/src/main/kotlin/io/sentry/android/gradle/AndroidComponentsConfig.kt

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -488,45 +488,47 @@ private fun ApplicationVariant.configureSnapshotsTasks(
488488

489489
// Wire Paparazzi test generation and upload task when the Paparazzi plugin is applied
490490
project.pluginManager.withPlugin("app.cash.paparazzi") {
491-
val android = project.extensions.getByType(BaseExtension::class.java)
491+
if (extension.snapshots.generateTests.get()) {
492+
val android = project.extensions.getByType(BaseExtension::class.java)
492493

493-
project.dependencies.add(
494-
"testImplementation",
495-
"io.github.sergio-sastre.ComposablePreviewScanner:android:0.8.1",
496-
)
497-
498-
val paparazziMajorVersion =
499-
project.provider {
500-
val dep =
501-
project.configurations.findByName("testImplementation")?.allDependencies?.find {
502-
it.group == "app.cash.paparazzi" && it.name == "paparazzi"
503-
}
504-
parseMajorVersion(dep?.version)
505-
}
506-
507-
val generateTask =
508-
GenerateSnapshotTestsTask.register(
509-
project,
510-
extension.snapshots,
511-
android,
512-
this@configureSnapshotsTasks,
513-
paparazziMajorVersion,
494+
project.dependencies.add(
495+
"testImplementation",
496+
"io.github.sergio-sastre.ComposablePreviewScanner:android:0.8.1",
514497
)
515498

516-
if (AgpVersions.isAGP90(AgpVersions.CURRENT)) {
517-
hostTests[UNIT_TEST_TYPE]?.apply {
518-
sources.java?.addGeneratedSourceDirectory(
519-
generateTask,
520-
GenerateSnapshotTestsTask::outputDir,
521-
)
522-
}
523-
} else {
524-
@Suppress("DEPRECATION_ERROR")
525-
unitTest?.apply {
526-
sources.java?.addGeneratedSourceDirectory(
527-
generateTask,
528-
GenerateSnapshotTestsTask::outputDir,
499+
val paparazziMajorVersion =
500+
project.provider {
501+
val dep =
502+
project.configurations.findByName("testImplementation")?.allDependencies?.find {
503+
it.group == "app.cash.paparazzi" && it.name == "paparazzi"
504+
}
505+
parseMajorVersion(dep?.version)
506+
}
507+
508+
val generateTask =
509+
GenerateSnapshotTestsTask.register(
510+
project,
511+
extension.snapshots,
512+
android,
513+
this@configureSnapshotsTasks,
514+
paparazziMajorVersion,
529515
)
516+
517+
if (AgpVersions.isAGP90(AgpVersions.CURRENT)) {
518+
hostTests[UNIT_TEST_TYPE]?.apply {
519+
sources.java?.addGeneratedSourceDirectory(
520+
generateTask,
521+
GenerateSnapshotTestsTask::outputDir,
522+
)
523+
}
524+
} else {
525+
@Suppress("DEPRECATION_ERROR")
526+
unitTest?.apply {
527+
sources.java?.addGeneratedSourceDirectory(
528+
generateTask,
529+
GenerateSnapshotTestsTask::outputDir,
530+
)
531+
}
530532
}
531533
}
532534

plugin-build/src/main/kotlin/io/sentry/android/gradle/extensions/SnapshotsExtension.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ open class SnapshotsExtension @Inject constructor(objects: ObjectFactory) {
1111

1212
val enabled: Property<Boolean> = objects.property(Boolean::class.java).convention(false)
1313

14+
val generateTests: Property<Boolean> = objects.property(Boolean::class.java).convention(true)
15+
1416
val includePrivatePreviews: Property<Boolean> =
1517
objects.property(Boolean::class.java).convention(true)
1618

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.sentry.android.gradle.extensions
2+
3+
import kotlin.test.assertFalse
4+
import kotlin.test.assertTrue
5+
import org.gradle.testfixtures.ProjectBuilder
6+
import org.junit.Test
7+
8+
class SnapshotsExtensionTest {
9+
10+
@Test
11+
fun `enabled is false by default`() {
12+
val project = ProjectBuilder.builder().build()
13+
val extension = project.objects.newInstance(SnapshotsExtension::class.java)
14+
15+
assertFalse(extension.enabled.get())
16+
}
17+
18+
@Test
19+
fun `generateTests is true by default`() {
20+
val project = ProjectBuilder.builder().build()
21+
val extension = project.objects.newInstance(SnapshotsExtension::class.java)
22+
23+
assertTrue(extension.generateTests.get())
24+
}
25+
26+
@Test
27+
fun `generateTests can be set to false`() {
28+
val project = ProjectBuilder.builder().build()
29+
val extension = project.objects.newInstance(SnapshotsExtension::class.java)
30+
31+
extension.generateTests.set(false)
32+
33+
assertFalse(extension.generateTests.get())
34+
}
35+
36+
@Test
37+
fun `includePrivatePreviews is true by default`() {
38+
val project = ProjectBuilder.builder().build()
39+
val extension = project.objects.newInstance(SnapshotsExtension::class.java)
40+
41+
assertTrue(extension.includePrivatePreviews.get())
42+
}
43+
44+
@Test
45+
fun `packageTrees is empty by default`() {
46+
val project = ProjectBuilder.builder().build()
47+
val extension = project.objects.newInstance(SnapshotsExtension::class.java)
48+
49+
assertTrue(extension.packageTrees.get().isEmpty())
50+
}
51+
}

0 commit comments

Comments
 (0)