Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable screenshot recording by means of an extension property #293

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/topics/build_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ apply plugin: "io.github.takahirom.roborazzi"
</table>
</details>

Roborazzi can then be activated in multiple ways:
1. By adding a specific property into the module's `gradle.properties` file, to enable the default mode Roborazzi operates in, e.g. verification.
2. By configuring the `roborazzi.taskType` property in the module's build file or an build convention plugin, again, to enable the default mode Roborazzi operates in.
3. By calling one of the specific tasks created by the Roborazzi plugin; this overrides any previously configured defaults.

The following table lists the specific configuration options in detail:

<table>
<tr>
<td> Use Roborazzi task </td> <td> Use default unit test task </td> <td> Description </td>
Expand All @@ -120,6 +127,9 @@ or

`./gradlew testDebugUnitTest -Proborazzi.test.record=true`

or

`./gradlew testDebugUnitTest` after adding `roborazzi { taskType.set(RoborazziTaskType.Record) }` to your module's Gradle build file or build convention plugin.

</td><td>

Expand All @@ -143,6 +153,10 @@ or

`./gradlew testDebugUnitTest -Proborazzi.test.compare=true`

or

`./gradlew testDebugUnitTest` after adding `roborazzi { taskType.set(RoborazziTaskType.Compare) }` to your module's Gradle build file or build convention plugin.

</td><td>

Review changes made to an image. This action will
Expand All @@ -166,6 +180,10 @@ or

`./gradlew testDebugUnitTest -Proborazzi.test.verify=true`

or

`./gradlew testDebugUnitTest` after adding `roborazzi { taskType.set(RoborazziTaskType.Verify) }` to your module's Gradle build file or build convention plugin.

</td><td>

Validate changes made to an image. If there is any difference between the current image and the
Expand All @@ -187,6 +205,10 @@ or

`./gradlew testDebugUnitTest -Proborazzi.test.verify=true -Proborazzi.test.record=true`

or

`./gradlew testDebugUnitTest` after adding `roborazzi { taskType.set(RoborazziTaskType.VerifyAndRecord) }` to your module's Gradle build file or build convention plugin.

</td><td>

This task will first verify the images and, if differences are detected, it will record a new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.takahirom.roborazzi

import com.github.takahirom.roborazzi.CaptureResults
import com.github.takahirom.roborazzi.RoborazziTaskType
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.junit.rules.TemporaryFolder
Expand Down Expand Up @@ -174,6 +175,7 @@ class RoborazziGradleProject(val testProjectDir: TemporaryFolder) {
private val PATH = "app/build.gradle.kts"
var removeOutputDirBeforeTestTypeTask = false
var customOutputDirPath: String? = null
var taskType: RoborazziTaskType? = null

fun addIncludeBuild() {
folder.root.resolve(PATH).delete()
Expand Down Expand Up @@ -270,15 +272,22 @@ dependencies {
""".trimIndent()
)
}
buildFile.appendText("""
roborazzi {
""")
if (customOutputDirPath != null) {
buildFile.appendText(
"""
roborazzi {
outputDir.set(file("$customOutputDirPath"))
}
""".trimIndent()
)
buildFile.appendText("""
outputDir.set(file("$customOutputDirPath"))
""".trimIndent())
}
if (taskType != null) {
buildFile.appendText("""
taskType.set(com.github.takahirom.roborazzi.RoborazziTaskType.${taskType.toString()})
""".trimIndent())
}
buildFile.appendText("""
}
""".trimIndent())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.takahirom.roborazzi

import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
import com.github.takahirom.roborazzi.RoborazziTaskType
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
Expand Down Expand Up @@ -472,4 +474,22 @@ class RoborazziGradleProjectTest {
checkResultCount(recorded = 1)
}
}
}

@Test
fun shouldNotRecordResultsByDefault() {
RoborazziGradleProject(testProjectDir).apply {
unitTest()
checkResultsSummaryFileNotExists()
}
}

@OptIn(ExperimentalRoborazziApi::class)
@Test
fun shouldRecordResultsByDefaultIfExtensionIsConfigured() {
RoborazziGradleProject(testProjectDir).apply {
appBuildFile.taskType = RoborazziTaskType.Record
unitTest()
checkResultsSummaryFileExists()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.github.takahirom.roborazzi.CaptureResult
import com.github.takahirom.roborazzi.CaptureResults
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
import com.github.takahirom.roborazzi.InternalRoborazziApi
import com.github.takahirom.roborazzi.RoborazziReportConst
import com.github.takahirom.roborazzi.RoborazziTaskType
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
Expand Down Expand Up @@ -46,8 +48,10 @@ private const val DEFAULT_TEMP_DIR = "intermediates/roborazzi"
*/
open class RoborazziExtension @Inject constructor(objects: ObjectFactory) {
val outputDir: DirectoryProperty = objects.directoryProperty()
val taskType: Property<RoborazziTaskType> = objects.property(RoborazziTaskType::class.java).convention(RoborazziTaskType.None)
}

@OptIn(ExperimentalRoborazziApi::class)
@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> {
Expand Down Expand Up @@ -187,6 +191,7 @@ abstract class RoborazziPlugin : Plugin<Project> {
isCompareRun.map { compareRunValue ->
isRecordRunValue || isVerifyRunValue || isVerifyAndRecordRunValue || compareRunValue
|| hasRoborazziTaskProperty(roborazziProperties)
|| extension.taskType.orNull != RoborazziTaskType.None
}
}
}
Expand Down Expand Up @@ -338,9 +343,10 @@ abstract class RoborazziPlugin : Plugin<Project> {
val isTaskPresent =
isAnyTaskRun(isRecordRun, isVerifyRun, isVerifyAndRecordRun, isCompareRun)
// Task properties
if (!isTaskPresent) {
if (!isTaskPresent && extension.taskType.orNull == RoborazziTaskType.None) {
test.systemProperties.putAll(roborazziProperties)
} else {
val extensionTaskType = extension.taskType.orNull
// Apply other roborazzi properties except for the ones that
// start with "roborazzi.test"
test.systemProperties.putAll(
Expand All @@ -349,10 +355,13 @@ abstract class RoborazziPlugin : Plugin<Project> {
}
)
test.systemProperties["roborazzi.test.record"] =
isRecordRun.get() || isVerifyAndRecordRun.get()
test.systemProperties["roborazzi.test.compare"] = isCompareRun.get()
isRecordRun.get() || isVerifyAndRecordRun.get() ||
extensionTaskType == RoborazziTaskType.Record || extensionTaskType == RoborazziTaskType.VerifyAndRecord
test.systemProperties["roborazzi.test.compare"] =
isCompareRun.get() || extensionTaskType == RoborazziTaskType.Compare
test.systemProperties["roborazzi.test.verify"] =
isVerifyRun.get() || isVerifyAndRecordRun.get()
isVerifyRun.get() || isVerifyAndRecordRun.get() ||
extensionTaskType == RoborazziTaskType.Verify || extensionTaskType == RoborazziTaskType.VerifyAndRecord
}

// Other properties
Expand Down
Loading