Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding cleanRecordPaparazzi... task to help tidy up orphaned snapshots #1316

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.gradle.api.internal.artifacts.transform.UnzipTransform
import org.gradle.api.logging.LogLevel.LIFECYCLE
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.reporting.ReportingExtension
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.options.Option
import org.gradle.api.tasks.testing.Test
Expand Down Expand Up @@ -77,6 +78,7 @@ public class PaparazziPlugin : Plugin<Project> {
private fun <T> setupPaparazzi(project: Project, variants: DomainObjectSet<T>) where T : BaseVariant, T : TestedVariant {
project.addTestDependency()
val nativePlatformFileCollection = project.setupNativePlatformDependency()
val snapshotOutputDir = project.layout.projectDirectory.dir("src/test/snapshots")
jrodbx marked this conversation as resolved.
Show resolved Hide resolved

// Create anchor tasks for all variants.
val verifyVariants = project.tasks.register("verifyPaparazzi") {
Expand All @@ -87,6 +89,19 @@ public class PaparazziPlugin : Plugin<Project> {
it.group = VERIFICATION_GROUP
it.description = "Record golden images for all variants"
}
val cleanRecordVariants = project.tasks.register("cleanRecordPaparazzi") {
it.group = VERIFICATION_GROUP
it.description = "Clean and record golden images for all variants"
}
val deleteSnapshots = project.tasks.register("deletePaparazziSnapshots", Delete::class.java) {
it.group = VERIFICATION_GROUP
it.description = "Delete all golden images"
val files = project.fileTree(snapshotOutputDir) { tree ->
tree.include("**/*.png")
tree.include("**/*.mov")
}
it.delete(files)
}

variants.all { variant ->
val variantSlug = variant.name.capitalize(Locale.US)
Expand All @@ -96,7 +111,6 @@ public class PaparazziPlugin : Plugin<Project> {
val buildDirectory = project.layout.buildDirectory
val gradleUserHomeDir = project.gradle.gradleUserHomeDir
val reportOutputDir = project.extensions.getByType(ReportingExtension::class.java).baseDirectory.dir("paparazzi/${variant.name}")
val snapshotOutputDir = project.layout.projectDirectory.dir("src/test/snapshots")

val localResourceDirs = project
.files(variant.sourceSets.flatMap { it.resDirectories })
Expand Down Expand Up @@ -179,8 +193,15 @@ public class PaparazziPlugin : Plugin<Project> {
val recordTaskProvider = project.tasks.register("recordPaparazzi$variantSlug", PaparazziTask::class.java) {
it.group = VERIFICATION_GROUP
it.description = "Record golden images for variant '${variant.name}'"
it.mustRunAfter(deleteSnapshots)
jrodbx marked this conversation as resolved.
Show resolved Hide resolved
}
recordVariants.configure { it.dependsOn(recordTaskProvider) }
val cleanRecordTaskProvider = project.tasks.register("cleanRecordPaparazzi$variantSlug") {
it.group = VERIFICATION_GROUP
it.description = "Clean and record golden images for variant '${variant.name}'"
it.dependsOn(deleteSnapshots, recordTaskProvider)
}
cleanRecordVariants.configure { it.dependsOn(cleanRecordTaskProvider) }
val verifyTaskProvider = project.tasks.register("verifyPaparazzi$variantSlug", PaparazziTask::class.java) {
it.group = VERIFICATION_GROUP
it.description = "Run screenshot tests for variant '${variant.name}'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,45 @@ class PaparazziPluginTest {
assertThat(snapshots[2]).isSimilarTo(verticalScroll).withDefaultThreshold()
}

@Test
fun deleteSnapshots() {
val fixtureRoot = File("src/test/projects/delete-snapshots")
val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
val snapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_DeleteTest_delete.png")
val snapshotWithLabel = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_DeleteTest_delete_label.png")

assertThat(snapshot.exists()).isTrue()
assertThat(snapshotWithLabel.exists()).isTrue()

gradleRunner
.withArguments("deletePaparazziSnapshots", "--stacktrace")
.runFixture(fixtureRoot) { build() }

assertThat(snapshot.exists()).isFalse()
assertThat(snapshotWithLabel.exists()).isFalse()
}

@Test
fun cleanRecord() {
val fixtureRoot = File("src/test/projects/clean-record")
val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
val snapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_CleanRecordTest_clean.png")
val snapshotWithKeep = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_CleanRecordTest_clean_keep.png")

assertThat(snapshot.exists()).isTrue()
assertThat(snapshotWithKeep.exists()).isTrue()

val result = gradleRunner
.withArguments("cleanRecordPaparazziDebug", "--stacktrace")
.runFixture(fixtureRoot) { build() }

assertThat(result.task(":deletePaparazziSnapshots")).isNotNull()
assertThat(result.task(":recordPaparazziDebug")).isNotNull()
jrodbx marked this conversation as resolved.
Show resolved Hide resolved

assertThat(snapshot.exists()).isFalse()
assertThat(snapshotWithKeep.exists()).isTrue()
jrodbx marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
fun widgets() {
val fixtureRoot = File("src/test/projects/widgets")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'app.cash.paparazzi'
}

android {
namespace 'app.cash.paparazzi.plugin.test'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
}
compileOptions {
sourceCompatibility = libs.versions.javaTarget.get()
targetCompatibility = libs.versions.javaTarget.get()
}
kotlinOptions {
jvmTarget = libs.versions.javaTarget.get()
}
}

apply from: '../guava-fix.gradle'
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.paparazzi.plugin.test

import android.widget.LinearLayout
import app.cash.paparazzi.Paparazzi
import org.junit.Rule
import org.junit.Test

class CleanRecordTest {
@get:Rule
val paparazzi = Paparazzi()

@Test
fun clean() {
paparazzi.snapshot(LinearLayout(paparazzi.context), "keep")
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'app.cash.paparazzi'
}

android {
namespace 'app.cash.paparazzi.plugin.test'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
}
compileOptions {
sourceCompatibility = libs.versions.javaTarget.get()
targetCompatibility = libs.versions.javaTarget.get()
}
kotlinOptions {
jvmTarget = libs.versions.javaTarget.get()
}
}

apply from: '../guava-fix.gradle'
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.paparazzi.plugin.test

import android.widget.LinearLayout
import app.cash.paparazzi.Paparazzi
import org.junit.Rule
import org.junit.Test

class DeleteTest {
@get:Rule
val paparazzi = Paparazzi()

@Test
fun delete() {
paparazzi.snapshot(LinearLayout(paparazzi.context))
paparazzi.snapshot(LinearLayout(paparazzi.context), "label")
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.