From 4bd220f3d2ac2bdf8ee4657916ef1002858efdc7 Mon Sep 17 00:00:00 2001 From: Pedro Machado Date: Fri, 20 Dec 2024 16:02:28 +0000 Subject: [PATCH 1/4] Removing files from intermediate dir before copying --- .../github/takahirom/roborazzi/RoborazziPlugin.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt index 4ba4f9ed..595cf6f6 100644 --- a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt +++ b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt @@ -482,6 +482,14 @@ abstract class RoborazziPlugin : Plugin { // outputDir.get().asFileTree.forEach { // println("Copy file ${finalizeTask.absolutePath} to ${intermediateDir.get()}") // } + + // Delete all images from the intermediateDir + intermediateDir.get().asFile.walkTopDown().forEach { file -> + if (KnownImageFileExtensions.contains(file.extension)) { + file.delete() + } + } + outputDir.get().asFile.mkdirs() outputDir.get().asFile.copyRecursively( target = intermediateDir.get().asFile, @@ -616,13 +624,6 @@ abstract class RoborazziPlugin : Plugin { roborazziResults: CaptureResults, ) { if (roborazziProperties["roborazzi.cleanupOldScreenshots"] == "true") { - // Delete all images from the intermediateDir - intermediateDir.get().asFile.walkTopDown().forEach { file -> - if (KnownImageFileExtensions.contains(file.extension)) { - file.delete() - } - } - // Remove all files not in the results from the outputDir val removingFiles: MutableSet = outputDir.get().asFile .listFiles() From 5e5d3db1fc27e2562a3a6106d90e20d293d2b82a Mon Sep 17 00:00:00 2001 From: Pedro Machado Date: Fri, 20 Dec 2024 23:39:39 +0000 Subject: [PATCH 2/4] Deleting intermediates only when recording and cleaning up --- .../takahirom/roborazzi/RoborazziPlugin.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt index 595cf6f6..29eb1b45 100644 --- a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt +++ b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt @@ -482,14 +482,6 @@ abstract class RoborazziPlugin : Plugin { // outputDir.get().asFileTree.forEach { // println("Copy file ${finalizeTask.absolutePath} to ${intermediateDir.get()}") // } - - // Delete all images from the intermediateDir - intermediateDir.get().asFile.walkTopDown().forEach { file -> - if (KnownImageFileExtensions.contains(file.extension)) { - file.delete() - } - } - outputDir.get().asFile.mkdirs() outputDir.get().asFile.copyRecursively( target = intermediateDir.get().asFile, @@ -623,7 +615,19 @@ abstract class RoborazziPlugin : Plugin { intermediateDir: DirectoryProperty, roborazziResults: CaptureResults, ) { - if (roborazziProperties["roborazzi.cleanupOldScreenshots"] == "true") { + val isCleanupTask = roborazziProperties["roborazzi.cleanupOldScreenshots"] == "true" + val isRecordTask = roborazziProperties["roborazzi.test.record"] == "true" + + if (isCleanupTask || isRecordTask) { + // Delete all images from the intermediateDir + intermediateDir.get().asFile.walkTopDown().forEach { file -> + if (KnownImageFileExtensions.contains(file.extension)) { + file.delete() + } + } + } + + if (isCleanupTask) { // Remove all files not in the results from the outputDir val removingFiles: MutableSet = outputDir.get().asFile .listFiles() From 879a8a65dfcce59b6b88be5232bf069fcfb2e3a8 Mon Sep 17 00:00:00 2001 From: Pedro Machado Date: Sat, 21 Dec 2024 10:00:25 +0000 Subject: [PATCH 3/4] Added test and fixed record run condition --- .../roborazzi/RoborazziGradleProjectTest.kt | 21 ++++++++++++++++++- .../takahirom/roborazzi/RoborazziPlugin.kt | 8 +++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt b/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt index a81f7868..29511351 100644 --- a/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt +++ b/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt @@ -131,6 +131,25 @@ class RoborazziGradleProjectTest { } } + @Test + fun recordWhenRunTwiceAfterTestChanges() { + RoborazziGradleRootProject(testProjectDir).appModule.apply { + record().output.run(::assertNotSkipped) + checkRecordedFileExists("$screenshotAndName.testCapture.png") + checkRecordedFileNotExists("$screenshotAndName.testCapture1.png") + checkRecordedFileNotExists("$screenshotAndName.testCapture2.png") + + removeTests() + addMultipleTest() + removeRoborazziOutputDir() + + record().output.run(::assertNotSkipped) + checkRecordedFileNotExists("$screenshotAndName.testCapture.png") + checkRecordedFileExists("$screenshotAndName.testCapture1.png") + checkRecordedFileExists("$screenshotAndName.testCapture2.png") + } + } + @Test fun recordWithSystemParameterWhenRemovedOutputAndIntermediate() { RoborazziGradleRootProject(testProjectDir).appModule.apply { @@ -607,4 +626,4 @@ class RoborazziGradleProjectTest { checkResultCount(recorded = 1) } } -} \ No newline at end of file +} diff --git a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt index 29eb1b45..f5dbc1c9 100644 --- a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt +++ b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/RoborazziPlugin.kt @@ -615,10 +615,10 @@ abstract class RoborazziPlugin : Plugin { intermediateDir: DirectoryProperty, roborazziResults: CaptureResults, ) { - val isCleanupTask = roborazziProperties["roborazzi.cleanupOldScreenshots"] == "true" - val isRecordTask = roborazziProperties["roborazzi.test.record"] == "true" + val isCleanupRun = roborazziProperties["roborazzi.cleanupOldScreenshots"] == "true" + val isRecordRun = test.systemProperties["roborazzi.test.record"] == true - if (isCleanupTask || isRecordTask) { + if (isCleanupRun || isRecordRun) { // Delete all images from the intermediateDir intermediateDir.get().asFile.walkTopDown().forEach { file -> if (KnownImageFileExtensions.contains(file.extension)) { @@ -627,7 +627,7 @@ abstract class RoborazziPlugin : Plugin { } } - if (isCleanupTask) { + if (isCleanupRun) { // Remove all files not in the results from the outputDir val removingFiles: MutableSet = outputDir.get().asFile .listFiles() From c9c702b763e7b819abd6038466dcb75ed2ab548b Mon Sep 17 00:00:00 2001 From: Pedro Machado Date: Sat, 21 Dec 2024 15:31:35 +0000 Subject: [PATCH 4/4] Added filtering test --- .../roborazzi/RoborazziGradleProjectTest.kt | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt b/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt index 29511351..7310a882 100644 --- a/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt +++ b/include-build/roborazzi-gradle-plugin/src/integrationTest/java/io/github/takahirom/roborazzi/RoborazziGradleProjectTest.kt @@ -131,25 +131,6 @@ class RoborazziGradleProjectTest { } } - @Test - fun recordWhenRunTwiceAfterTestChanges() { - RoborazziGradleRootProject(testProjectDir).appModule.apply { - record().output.run(::assertNotSkipped) - checkRecordedFileExists("$screenshotAndName.testCapture.png") - checkRecordedFileNotExists("$screenshotAndName.testCapture1.png") - checkRecordedFileNotExists("$screenshotAndName.testCapture2.png") - - removeTests() - addMultipleTest() - removeRoborazziOutputDir() - - record().output.run(::assertNotSkipped) - checkRecordedFileNotExists("$screenshotAndName.testCapture.png") - checkRecordedFileExists("$screenshotAndName.testCapture1.png") - checkRecordedFileExists("$screenshotAndName.testCapture2.png") - } - } - @Test fun recordWithSystemParameterWhenRemovedOutputAndIntermediate() { RoborazziGradleRootProject(testProjectDir).appModule.apply { @@ -626,4 +607,39 @@ class RoborazziGradleProjectTest { checkResultCount(recorded = 1) } } + + @Test + fun shouldNotRetainOutdatedImagesWhenRecording() { + RoborazziGradleRootProject(testProjectDir).appModule.apply { + record().output.run(::assertNotSkipped) + checkRecordedFileExists("$screenshotAndName.testCapture.png") + checkRecordedFileNotExists("$screenshotAndName.testCapture1.png") + checkRecordedFileNotExists("$screenshotAndName.testCapture2.png") + + removeTests() + addMultipleTest() + removeRoborazziOutputDir() + + record().output.run(::assertNotSkipped) + checkRecordedFileNotExists("$screenshotAndName.testCapture.png") + checkRecordedFileExists("$screenshotAndName.testCapture1.png") + checkRecordedFileExists("$screenshotAndName.testCapture2.png") + } + } + + @Test + fun shouldNotDeletePreviousImagesWhenFiltering() { + RoborazziGradleRootProject(testProjectDir).appModule.apply { + removeTests() + addMultipleTest() + + recordWithFilter1().output.run(::assertNotSkipped) + checkRecordedFileExists("$screenshotAndName.testCapture1.png") + checkRecordedFileNotExists("$screenshotAndName.testCapture2.png") + + recordWithFilter2().output.run(::assertNotSkipped) + checkRecordedFileExists("$screenshotAndName.testCapture1.png") + checkRecordedFileExists("$screenshotAndName.testCapture2.png") + } + } }