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..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 @@ -607,4 +607,39 @@ class RoborazziGradleProjectTest { checkResultCount(recorded = 1) } } -} \ No newline at end of file + + @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") + } + } +} 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..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,14 +615,19 @@ abstract class RoborazziPlugin : Plugin { intermediateDir: DirectoryProperty, roborazziResults: CaptureResults, ) { - if (roborazziProperties["roborazzi.cleanupOldScreenshots"] == "true") { + val isCleanupRun = roborazziProperties["roborazzi.cleanupOldScreenshots"] == "true" + val isRecordRun = test.systemProperties["roborazzi.test.record"] == true + + if (isCleanupRun || isRecordRun) { // Delete all images from the intermediateDir intermediateDir.get().asFile.walkTopDown().forEach { file -> if (KnownImageFileExtensions.contains(file.extension)) { file.delete() } } + } + if (isCleanupRun) { // Remove all files not in the results from the outputDir val removingFiles: MutableSet = outputDir.get().asFile .listFiles()