Skip to content

Commit

Permalink
Vary snapshot output dir based on applied kotlin plugin
Browse files Browse the repository at this point in the history
Closes #595
  • Loading branch information
John Rodriguez committed Sep 28, 2023
1 parent b7b3f88 commit cf453a0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ 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 @@ -186,22 +185,31 @@ class PaparazziPlugin : Plugin<Project> {
testVariant.javaCompileProvider.configure { it.dependsOn(writeResourcesTask) }
}

lateinit var snapshotSourceSet: String

if (project.plugins.hasPlugin(KotlinMultiplatformPluginWrapper::class.java)) {
val kotlin = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
val target = checkNotNull(kotlin.targets.find { it is KotlinAndroidTarget }) {
"There must be an Android target configured when using Paparazzi with the Kotlin Multiplatform Plugin"
}
val compilation = target.compilations.getByName(testVariant.name)
compilation.compileTaskProvider.configure { it.dependsOn(writeResourcesTask) }

snapshotSourceSet = compilation.defaultSourceSet.name
} else if (project.plugins.hasPlugin(KotlinAndroidPluginWrapper::class.java)) {
val kotlin = project.extensions.getByType(KotlinAndroidProjectExtension::class.java)
val target = kotlin.target
val compilation = target.compilations.getByName(testVariant.name)
compilation.compileTaskProvider.configure { it.dependsOn(writeResourcesTask) }

snapshotSourceSet = compilation.kotlinSourceSets.last().name
} else {
project.logger.log(INFO, "No kotlin plugin applied!")
snapshotSourceSet = testVariant.name
}

val snapshotOutputDir = project.layout.projectDirectory.dir("src/$snapshotSourceSet/snapshots")

val recordTaskProvider = project.tasks.register("recordPaparazzi$variantSlug", PaparazziTask::class.java) {
it.group = VERIFICATION_GROUP
it.description = "Record golden images for variant '${variant.name}'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class PaparazziPluginTest {

assertThat(result.task(":testDebugUnitTest")).isNotNull()

val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
val snapshotsDir = File(fixtureRoot, "src/testDebug/snapshots")

val snapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_RecordTest_record.png")
assertThat(snapshot.exists()).isTrue()
Expand All @@ -419,8 +419,8 @@ class PaparazziPluginTest {
assertThat(result.task(":recordPaparazziDebug")).isNotNull()
assertThat(result.task(":recordPaparazziRelease")).isNotNull()

val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
snapshotsDir.deleteRecursively()
File(fixtureRoot, "src/testDebug/snapshots").deleteRecursively()
File(fixtureRoot, "src/testRelease/snapshots").deleteRecursively()
}

@Test
Expand All @@ -434,7 +434,7 @@ class PaparazziPluginTest {

assertThat(result.task(":module:testDebugUnitTest")).isNotNull()

val snapshotsDir = File(moduleRoot, "src/test/snapshots")
val snapshotsDir = File(moduleRoot, "src/testDebug/snapshots")

val snapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_RecordTest_record.png")
assertThat(snapshot.exists()).isTrue()
Expand All @@ -456,7 +456,7 @@ class PaparazziPluginTest {

assertThat(result.task(":module:testDebugUnitTest")).isNotNull()

val snapshotsDir = File(moduleRoot, "src/test/snapshots")
val snapshotsDir = File(moduleRoot, "src/testDebug/snapshots")

val firstSnapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_RecordTest_recordFirst.png")
assertThat(firstSnapshot.exists()).isFalse()
Expand All @@ -471,7 +471,7 @@ class PaparazziPluginTest {
fun rerunOnResourceChange() {
val fixtureRoot = File("src/test/projects/rerun-resource-change")

val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
val snapshotsDir = File(fixtureRoot, "src/testDebug/snapshots")
val snapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_RecordTest_record.png")

val valuesDir = File(fixtureRoot, "src/main/res/values/")
Expand Down Expand Up @@ -522,7 +522,7 @@ class PaparazziPluginTest {
fun rerunOnAssetChange() {
val fixtureRoot = File("src/test/projects/rerun-asset-change")

val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
val snapshotsDir = File(fixtureRoot, "src/testDebug/snapshots")
val snapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_RecordTest_record.png")

val assetsDir = File(fixtureRoot, "src/main/assets/")
Expand Down Expand Up @@ -605,15 +605,15 @@ class PaparazziPluginTest {

reportDir.deleteRecursively()

val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
val snapshotsDir = File(fixtureRoot, "src/testDebug/snapshots")
snapshotsDir.deleteRecursively()
}

@Test
fun rerunOnSnapshotDeletion() {
val fixtureRoot = File("src/test/projects/rerun-snapshots")

val snapshotsDir = File(fixtureRoot, "src/test/snapshots")
val snapshotsDir = File(fixtureRoot, "src/testDebug/snapshots")
val snapshot = File(snapshotsDir, "images/app.cash.paparazzi.plugin.test_RecordTest_record.png")
assertThat(snapshot.exists()).isFalse()

Expand Down Expand Up @@ -721,7 +721,7 @@ class PaparazziPluginTest {
val delta = File(failureDir, "delta-app.cash.paparazzi.plugin.test_VerifyTest_verify.png")
assertThat(delta.exists()).isTrue()

val goldenImage = File(fixtureRoot, "src/test/resources/expected_delta.png")
val goldenImage = File(fixtureRoot, "src/testDebug/resources/expected_delta.png")
assertThat(delta).isSimilarTo(goldenImage).withDefaultThreshold()

failureDir.deleteRecursively()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cf453a0

Please sign in to comment.