Skip to content

Commit

Permalink
Merge pull request #539 from takahirom/takahirom/delete-old-screensho…
Browse files Browse the repository at this point in the history
…t-option/2024-11-04

Add cleanupOldScreenshots option
  • Loading branch information
takahirom authored Nov 4, 2024
2 parents a278527 + bdc2ca0 commit 47f1a7d
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 41 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,8 @@ The sample image
<!-- Generated by docs/topics/gradle_properties_options.md. Do not edit this file. -->
# Roborazzi gradle.properties Options and Recommendations
You can configure the following options in your `gradle.properties` file:
You can configure the following options in your `gradle.properties` file.
You can also use `-P` to set the options in the command line. For example, `./gradlew test -Proborazzi.test.record=true`.
## roborazzi.test
Expand Down Expand Up @@ -1434,6 +1435,15 @@ This option enables you to define the naming strategy for the recorded image. Th
roborazzi.record.namingStrategy=testClassAndMethod
```
## roborazzi.cleanupOldScreenshots
This option allows you to clean up old screenshots. By default, this option is set to false.
The reason why Roborazzi does not delete old screenshots by default is that Roborazzi doesn't know if you are running filtered tests or not. If you are running filtered tests, Roborazzi will delete the screenshots that are not related to the current test run.
```
roborazzi.cleanupOldScreenshots=true
```
## Robolectric Options
### robolectric.pixelCopyRenderMode
Expand Down
2 changes: 2 additions & 0 deletions docs/topics/ai_powered_image_assertion.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ val roborazziRule = RoborazziRule(
compareOptions = RoborazziOptions.CompareOptions(
aiAssertionOptions = AiAssertionOptions(
aiAssertionModel = GeminiAiAssertionModel(
// DO NOT HARDCODE your API key in your code.
// This is an example passing API Key through unitTests.all{ environment(key, value) }
apiKey = System.getenv("gemini_api_key") ?: ""
),
)
Expand Down
12 changes: 11 additions & 1 deletion docs/topics/gradle_properties_options.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Roborazzi gradle.properties Options and Recommendations

You can configure the following options in your `gradle.properties` file:
You can configure the following options in your `gradle.properties` file.
You can also use `-P` to set the options in the command line. For example, `./gradlew test -Proborazzi.test.record=true`.

## roborazzi.test

Expand Down Expand Up @@ -43,6 +44,15 @@ This option enables you to define the naming strategy for the recorded image. Th
roborazzi.record.namingStrategy=testClassAndMethod
```

## roborazzi.cleanupOldScreenshots

This option allows you to clean up old screenshots. By default, this option is set to false.
The reason why Roborazzi does not delete old screenshots by default is that Roborazzi doesn't know if you are running filtered tests or not. If you are running filtered tests, Roborazzi will delete the screenshots that are not related to the current test run.

```
roborazzi.cleanupOldScreenshots=true
```

## Robolectric Options

### robolectric.pixelCopyRenderMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class RoborazziGradleRootProject(val testProjectDir: TemporaryFolder) {
val appModule = AppModule(this, testProjectDir)
val previewModule = PreviewModule(this, testProjectDir)

fun runTask(task: String, buildType: BuildType, additionalParameters: Array<String>): BuildResult {
fun runTask(
task: String,
buildType: BuildType,
additionalParameters: Array<String>
): BuildResult {
val buildResult = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments(
Expand Down Expand Up @@ -60,6 +64,11 @@ class AppModule(val rootProject: RoborazziGradleRootProject, val testProjectDir:
return runTask(task)
}

fun recordWithCleanupOldScreenshots(): BuildResult {
val task = "recordRoborazziDebug"
return runTask(task, additionalParameters = arrayOf("-Proborazzi.cleanupOldScreenshots=true"))
}

fun recordWithFilter1(): BuildResult {
val task = "recordRoborazziDebug"
return runTask(
Expand Down Expand Up @@ -136,6 +145,11 @@ class AppModule(val rootProject: RoborazziGradleRootProject, val testProjectDir:
return runTask(task)
}

fun compareWithCleanupOldScreenshots(): BuildResult {
val task = "compareRoborazziDebug"
return runTask(task, additionalParameters = arrayOf("-Proborazzi.cleanupOldScreenshots=true"))
}

fun clear(): BuildResult {
val task = "clearRoborazziDebug"
return runTask(task)
Expand Down Expand Up @@ -181,7 +195,7 @@ class AppModule(val rootProject: RoborazziGradleRootProject, val testProjectDir:
buildGradle.addIncludeBuild()

val buildResult = rootProject.runTask(
"app:"+task,
"app:" + task,
buildType,
additionalParameters
)
Expand All @@ -192,8 +206,9 @@ class AppModule(val rootProject: RoborazziGradleRootProject, val testProjectDir:
private val PATH = "app/build.gradle.kts"
var removeOutputDirBeforeTestTypeTask = false
var customOutputDirPath: String? = null

init {
addIncludeBuild()
addIncludeBuild()
}

fun addIncludeBuild() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ package io.github.takahirom.roborazzi
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.rules.TestWatcher

/**
* Run this test with `cd include-build` and `./gradlew roborazzi-gradle-plugin:check`
* You can also run this test with the following command:
* ./gradlew roborazzi-gradle-plugin:integrationTest --tests "*RoborazziGradleProjectTest.record"
*/
class RoborazziGradleProjectTest {

@get:Rule
val testProjectDir = TemporaryFolder()

@get:Rule
val testNameOutputRule = object : TestWatcher() {
override fun starting(description: org.junit.runner.Description?) {
println("RoborazziGradleProjectTest.${description?.methodName} started")
}
}

private val className = "com.github.takahirom.integration_test_project.RoborazziTest"

private var defaultBuildDir = "build"
Expand Down Expand Up @@ -308,8 +318,7 @@ class RoborazziGradleProjectTest {
removeTests()
record()

// Summary file will be generated even if no test files
checkResultsSummaryFileExists()
checkResultsSummaryFileNotExists()
// Test will be skipped when no source so no output
checkResultFileNotExists(resultFileSuffix)
}
Expand Down Expand Up @@ -392,6 +401,21 @@ class RoborazziGradleProjectTest {
}
}

@Test
fun compareWithDeleteOldScreenshot() {
RoborazziGradleRootProject(testProjectDir).appModule.apply {
recordWithCleanupOldScreenshots()
changeScreen()
compareWithCleanupOldScreenshots()

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

@Test
fun compareWithSystemParameter() {
println("start compareWithSystemParameter")
Expand Down Expand Up @@ -420,6 +444,21 @@ class RoborazziGradleProjectTest {
}
}

@Test
fun ifWeUseCleanupOldScreenshotsTheOldScreenshotsShouldNotExit() {
RoborazziGradleRootProject(testProjectDir).appModule.apply {
recordWithCleanupOldScreenshots()
removeTests()
addTestCaptureWithCustomPathTest()
recordWithCleanupOldScreenshots()

checkResultsSummaryFileExists()
checkRecordedFileNotExists("$screenshotAndName.testCapture.png")
checkRecordedFileExists("$customReferenceScreenshotAndName.png")
checkRecordedFileExists("app/build/outputs/roborazzi/custom_outputDirectoryPath_from_rule/custom_outputFileProvider-com.github.takahirom.integration_test_project.RoborazziTest.testCaptureWithCustomPath.png")
}
}

@Test
fun compareWithCustomPath() {
RoborazziGradleRootProject(testProjectDir).appModule.apply {
Expand Down
Loading

0 comments on commit 47f1a7d

Please sign in to comment.