Skip to content

Commit

Permalink
Merge pull request #389 from takahirom/takahirom/use-empty-files-for-…
Browse files Browse the repository at this point in the history
…record/2024-05-28

Use empty files for record task
  • Loading branch information
takahirom authored May 29, 2024
2 parents 8839dc2 + 455f6e6 commit e2efd5a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class RoborazziGradleProject(val testProjectDir: TemporaryFolder) {
assert(output.contains("testDebugUnitTest UP-TO-DATE"))
}

fun assertFromCache(output: String) {
assert(output.contains("testDebugUnitTest FROM-CACHE"))
}

enum class BuildType {
Build, BuildAndFail
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class RoborazziGradleProjectTest {
record()
removeRoborazziOutputDir()
val output = record().output
assertNotSkipped(output)
assertSkipped(output)

checkResultsSummaryFileExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
Expand All @@ -74,10 +74,8 @@ class RoborazziGradleProjectTest {
removeRoborazziAndIntermediateOutputDir()
record()
removeRoborazziAndIntermediateOutputDir()
// should not be skipped even if tests and sources are not changed
// when output directory is removed
val output = record().output
assertNotSkipped(output)
assertFromCache(output)

checkResultsSummaryFileExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
Expand All @@ -89,7 +87,6 @@ class RoborazziGradleProjectTest {
@Test
fun recordWhenRunTwice() {
RoborazziGradleProject(testProjectDir).apply {
record()
val output1 = record().output
assertNotSkipped(output1)
val output2 = record().output
Expand All @@ -102,12 +99,27 @@ class RoborazziGradleProjectTest {
}
}

@Test
fun recordWithSystemParameterWhenRemovedOutputAndIntermediate() {
RoborazziGradleProject(testProjectDir).apply {
val output1 = recordWithSystemParameter().output
assertNotSkipped(output1)
removeRoborazziAndIntermediateOutputDir()
val output2 = recordWithSystemParameter().output
assertFromCache(output2)

checkResultsSummaryFileExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}

@Test
fun recordWhenRunTwiceWithGradleCustomOutput() {
RoborazziGradleProject(testProjectDir).apply {
val customDirFromGradle = "src/screenshots/roborazzi_customdir_from_gradle"
appBuildFile.customOutputDirPath = customDirFromGradle
record()
val output1 = record().output
assertNotSkipped(output1)
val output2 = record().output
Expand Down Expand Up @@ -460,4 +472,4 @@ class RoborazziGradleProjectTest {
checkResultCount(recorded = 1)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ open class RoborazziExtension @Inject constructor(objects: ObjectFactory) {
@Suppress("unused")
// From Paparazzi: https://github.com/cashapp/paparazzi/blob/a76702744a7f380480f323ffda124e845f2733aa/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt
abstract class RoborazziPlugin : Plugin<Project> {
@Inject abstract fun getEventsListenerRegistry(): BuildEventsListenerRegistry
@Inject
abstract fun getEventsListenerRegistry(): BuildEventsListenerRegistry

val AbstractTestTask.systemProperties: MutableMap<String, Any?>
get() = when (this) {
Expand Down Expand Up @@ -192,6 +193,16 @@ abstract class RoborazziPlugin : Plugin<Project> {
}
}
}
val onlyRecordRunProvider = isRecordRun.flatMap { isRecordTaskRun ->
isVerifyRun.map { isVerifyTaskRun ->
isRecordTaskRun && !isVerifyTaskRun
}
}.map { isRecordingTaskOnly ->
isRecordingTaskOnly ||
(roborazziProperties["roborazzi.test.record"] == "true" &&
roborazziProperties["roborazzi.test.verify"] != "true")
}

val projectAbsolutePathProvider = project.providers.provider {
project.projectDir.absolutePath
}
Expand Down Expand Up @@ -261,27 +272,35 @@ abstract class RoborazziPlugin : Plugin<Project> {
testTaskProvider
.configureEach { test: AbstractTestTask ->
val resultsDir = resultDirFileProperty.get().asFile
if (restoreOutputDirRoborazziTaskProvider.isPresent) {
// Previous outputs are an input when running in compare or verify mode.
// However, during record runs the output dir might not exist yet, so we use
// files() to express that it is optional.
// See also: https://github.com/gradle/gradle/issues/2016
test.inputs.files(restoreOutputDirRoborazziTaskProvider.map {
if (!it.outputDir.get().asFile.exists()) {
it.outputDir.get().asFile.mkdirs()
}
test.infoln("Roborazzi: Set input dir ${it.outputDir.get()} to test task")
it.outputDir.files(".")
})
} else {
test.inputs.files(outputDir.map {
if (!it.asFile.exists()) {
it.asFile.mkdirs()
test.inputs.files(
onlyRecordRunProvider.map { onlyRecordRun ->
if (onlyRecordRun) {
// Note: this is not files in outputDir,
// but empty input when running in record mode.
outputDir.get().files()
} else if (restoreOutputDirRoborazziTaskProvider.isPresent) {
// Previous outputs are an input when running in compare or verify mode.
// However, during record runs the output dir might not exist yet, so we use
// files() to express that it is optional.
// See also: https://github.com/gradle/gradle/issues/2016
restoreOutputDirRoborazziTaskProvider.map {
if (!it.outputDir.get().asFile.exists()) {
it.outputDir.get().asFile.mkdirs()
}
test.infoln("Roborazzi: Set input dir ${it.outputDir.get()} to test task")
it.outputDir.files(".")
}
} else {
outputDir.map {
if (!it.asFile.exists()) {
it.asFile.mkdirs()
}
test.infoln("Roborazzi: Set input dir $it to test task")
it.files(".")
}
}
test.infoln("Roborazzi: Set input dir $it to test task")
it.files(".")
})
}
}
)
test.outputs.dir(intermediateDirForEachVariant.map {
test.infoln("Roborazzi: Set output dir $it to test task")
it
Expand Down

0 comments on commit e2efd5a

Please sign in to comment.