Skip to content

Commit 66c0f4c

Browse files
committed
Add support for snapshots for android multiplatform
1 parent 8286509 commit 66c0f4c

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import org.gradle.internal.os.OperatingSystem
5353
import org.gradle.language.base.plugins.LifecycleBasePlugin.VERIFICATION_GROUP
5454
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
5555
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
56+
import java.io.File
5657
import java.util.Locale
5758
import javax.inject.Inject
5859
import kotlin.io.encoding.Base64
@@ -97,7 +98,30 @@ public class PaparazziPlugin @Inject constructor(
9798
project.addTestDependency()
9899
val layoutlibNativeRuntimeFileCollection = project.setupLayoutlibRuntimeDependency()
99100
val layoutlibResourcesFileCollection = project.setupLayoutlibResourcesDependency()
100-
val snapshotOutputDir = project.layout.projectDirectory.dir("src/test/snapshots")
101+
val testSourceSetProvider = project.objects.directoryProperty()
102+
testSourceSetProvider.set(project.layout.projectDirectory.dir("src/test"))
103+
104+
project.plugins.withId("org.jetbrains.kotlin.multiplatform") {
105+
val kmpExtension = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
106+
kmpExtension.sourceSets.all { sourceSet ->
107+
if (sourceSet.name == "androidUnitTest" || sourceSet.name == "androidTest") {
108+
testSourceSetProvider.set(
109+
sourceSet.kotlin.srcDirs.map {
110+
File(it.parent)
111+
}.firstOrNull()
112+
)
113+
}
114+
}
115+
}
116+
117+
val defaultUnitTestDir = project.layout.projectDirectory.dir("src/test")
118+
val snapshotOutputDir = testSourceSetProvider.map {
119+
if (it.asFile.exists() && !defaultUnitTestDir.asFile.exists()) {
120+
it.dir("snapshots")
121+
} else {
122+
defaultUnitTestDir.dir("snapshots")
123+
}
124+
}
101125

102126
// Create anchor tasks for all variants.
103127
val verifyVariants = project.tasks.register("verifyPaparazzi") {
@@ -216,7 +240,7 @@ public class PaparazziPlugin @Inject constructor(
216240
test.systemProperties["paparazzi.project.dir"] = projectDirectory.toString()
217241
test.systemProperties["paparazzi.build.dir"] = buildDirectory.get().toString()
218242
test.systemProperties["paparazzi.report.dir"] = reportOutputDir.get().toString()
219-
test.systemProperties["paparazzi.snapshot.dir"] = snapshotOutputDir.toString()
243+
test.systemProperties["paparazzi.snapshot.dir"] = snapshotOutputDir.get().toString()
220244
test.systemProperties["paparazzi.artifacts.cache.dir"] = gradleUserHomeDir.path
221245
test.systemProperties.putAll(project.properties.filterKeys { it.startsWith("app.cash.paparazzi") })
222246

@@ -241,19 +265,15 @@ public class PaparazziPlugin @Inject constructor(
241265

242266
test.inputs.dir(
243267
isVerifyRun.flatMap {
244-
project.objects.directoryProperty().apply {
245-
set(if (it) snapshotOutputDir else null)
246-
}
268+
if (it) snapshotOutputDir else project.objects.directoryProperty()
247269
}
248270
).withPropertyName("paparazzi.snapshot.input.dir")
249271
.withPathSensitivity(PathSensitivity.RELATIVE)
250272
.optional()
251273

252274
test.outputs.dir(
253275
isRecordRun.flatMap {
254-
project.objects.directoryProperty().apply {
255-
set(if (it) snapshotOutputDir else null)
256-
}
276+
if (it) snapshotOutputDir else project.objects.directoryProperty()
257277
}
258278
).withPropertyName("paparazzi.snapshots.output.dir")
259279
.optional()

paparazzi-gradle-plugin/src/test/java/app/cash/paparazzi/gradle/PaparazziPluginTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ class PaparazziPluginTest {
9292
val fixtureRoot = File("src/test/projects/multiplatform-plugin-with-android")
9393

9494
val result = gradleRunner
95-
.withArguments("preparePaparazziDebugResources", "--stacktrace")
95+
.withArguments("verifyPaparazziDebug", "--stacktrace")
9696
.runFixture(fixtureRoot) { build() }
9797

9898
assertThat(result.task(":preparePaparazziDebugResources")).isNotNull()
99+
assertThat(result.task(":testDebugUnitTest")).isNotNull()
99100
}
100101

101102
@Test

paparazzi-gradle-plugin/src/test/projects/multiplatform-plugin-with-android/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ plugins {
66

77
kotlin {
88
androidTarget()
9+
10+
sourceSets {
11+
androidMain {
12+
dependencies {
13+
implementation 'androidx.appcompat:appcompat:1.7.0'
14+
}
15+
}
16+
}
917
}
1018

1119
android {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
kotlin.mpp.androidSourceSetLayoutVersion1.nowarn=true
1+
android.useAndroidX=true
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package app.cash.paparazzi.plugin.test
2+
3+
import android.widget.TextView
4+
import app.cash.paparazzi.Paparazzi
5+
import org.junit.Rule
6+
import org.junit.Test
7+
8+
class SampleTest {
9+
@get:Rule
10+
val paparazzi = Paparazzi()
11+
12+
@Test
13+
fun test() {
14+
paparazzi.snapshot(TextView(paparazzi.context).apply { text = "Hello, Paparazzi!" })
15+
}
16+
}
5.78 KB
Loading

0 commit comments

Comments
 (0)