Skip to content

Commit bd1dd1a

Browse files
TWiStErRobjrodbx
andauthored
Make sure record/verify/properties actually affect test tasks (#690)
* Add failing tests that reproduce the problem where the record and verify parameters are not playing in up to date checks and fix by registering inputs explicitly. Co-authored-by: John Rodriguez <john.rodriguez@gmail.com> * Move paparazzi properties earlier, so they are playing in incremental builds. It's a standard HashMap not a DomainObjectCollection, so there won't be any further items inserted between filterKeys and doFirst. * Move and remove comments * Document that we're doing something funky * Merge two tests * Move systemProperties together --------- Co-authored-by: John Rodriguez <john.rodriguez@gmail.com>
1 parent 9e4684a commit bd1dd1a

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ class PaparazziPlugin : Plugin<Project> {
158158
writeResourcesTask.flatMap { it.paparazziResources.asFile }.get().path
159159
test.systemProperties["paparazzi.build.dir"] =
160160
buildDirectory.get().toString()
161+
test.systemProperties["kotlinx.coroutines.main.delay"] = true
162+
test.systemProperties.putAll(project.properties.filterKeys { it.startsWith("app.cash.paparazzi") })
163+
164+
test.inputs.property("paparazzi.test.record", isRecordRun)
165+
test.inputs.property("paparazzi.test.verify", isVerifyRun)
161166

162167
test.inputs.dir(mergeResourcesOutputDir)
163168
test.inputs.dir(mergeAssetsOutputDir)
@@ -168,18 +173,16 @@ class PaparazziPlugin : Plugin<Project> {
168173
test.outputs.dir(reportOutputDir)
169174
test.outputs.dir(snapshotOutputDir)
170175

171-
val paparazziProperties = project.properties.filterKeys { it.startsWith("app.cash.paparazzi") }
172-
173176
@Suppress("ObjectLiteralToLambda")
174177
// why not a lambda? See: https://docs.gradle.org/7.2/userguide/validation_problems.html#implementation_unknown
175178
test.doFirst(object : Action<Task> {
176179
override fun execute(t: Task) {
180+
// Note: these are lazy properties that are not resolvable in the Gradle configuration phase.
181+
// They need special handling, so they're added as inputs.property above, and systemProperty here.
177182
test.systemProperties["paparazzi.platform.data.root"] =
178183
nativePlatformFileCollection.singleFile.absolutePath
179184
test.systemProperties["paparazzi.test.record"] = isRecordRun.get()
180185
test.systemProperties["paparazzi.test.verify"] = isVerifyRun.get()
181-
test.systemProperties["kotlinx.coroutines.main.delay"] = true
182-
test.systemProperties.putAll(paparazziProperties)
183186
}
184187
})
185188
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,44 @@ class PaparazziPluginTest {
521521
snapshotsDir.deleteRecursively()
522522
}
523523

524+
@Test
525+
fun rerunTestsOnPropertyChange() {
526+
val fixtureRoot = File("src/test/projects/rerun-property-change")
527+
528+
// Take 1
529+
val firstRunResult = gradleRunner
530+
.withArguments("testDebugUnitTest", "--stacktrace")
531+
.forwardOutput()
532+
.runFixture(fixtureRoot) { build() }
533+
534+
with(firstRunResult.task(":testDebugUnitTest")) {
535+
assertThat(this).isNotNull()
536+
assertThat(this!!.outcome).isEqualTo(SUCCESS)
537+
}
538+
539+
// Take 2
540+
val secondRunResult = gradleRunner
541+
.withArguments("recordPaparazziDebug", "--stacktrace")
542+
.forwardOutput()
543+
.runFixture(fixtureRoot) { build() }
544+
545+
with(secondRunResult.task(":testDebugUnitTest")) {
546+
assertThat(this).isNotNull()
547+
assertThat(this!!.outcome).isEqualTo(SUCCESS) // not UP-TO-DATE
548+
}
549+
550+
// Take 3
551+
val thirdRunResult = gradleRunner
552+
.withArguments("verifyPaparazziDebug", "--stacktrace")
553+
.forwardOutput()
554+
.runFixture(fixtureRoot) { build() }
555+
556+
with(thirdRunResult.task(":testDebugUnitTest")) {
557+
assertThat(this).isNotNull()
558+
assertThat(this!!.outcome).isEqualTo(SUCCESS) // not UP-TO-DATE
559+
}
560+
}
561+
524562
@Test
525563
fun verifySuccess() {
526564
val fixtureRoot = File("src/test/projects/verify-mode-success")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'kotlin-android'
4+
id 'app.cash.paparazzi'
5+
}
6+
7+
repositories {
8+
maven {
9+
url "file://${projectDir.absolutePath}/../../../../../build/localMaven"
10+
}
11+
mavenCentral()
12+
//mavenLocal()
13+
google()
14+
}
15+
16+
android {
17+
namespace 'app.cash.paparazzi.plugin.test'
18+
compileSdk libs.versions.compileSdk.get() as int
19+
defaultConfig {
20+
minSdk libs.versions.minSdk.get() as int
21+
}
22+
}
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.view.View
4+
import app.cash.paparazzi.Paparazzi
5+
import org.junit.Rule
6+
import org.junit.Test
7+
8+
class RecordTest {
9+
@get:Rule
10+
val paparazzi = Paparazzi()
11+
12+
@Test
13+
fun record() {
14+
paparazzi.snapshot(View(paparazzi.context))
15+
}
16+
}

0 commit comments

Comments
 (0)