takahirom / roborazzi

Make JVM Android integration test visible 🤖📸
https://takahirom.github.io/roborazzi/
Apache License 2.0
650 stars 24 forks source link

[Feature Request] Bazel Support #63

Open bohregard opened 1 year ago

bohregard commented 1 year ago

The company that I work for is using Bazel as the build tool for our application and we are considering using Roborazzi for snapshot testing in the future. We've done some internal testing and it looks like the tool doesn't need the Gradle plugin to successfully run since you can pass in the environment flag. For instance in Gradle we could do:

tasks.withType<Test> {
  doFirst {
    systemProperties["roborazzi.test.record"] = "true"
  }
}

All tests that incorporate Roborazzi will then output the snapshots. (Obviously they will always do that but that's ok for us since that's our desired output).

Ideally we would build a custom rule on android_local_test (something like snapshot_local_test) where we pass in the custom environment variable for Roborazzi.

The challenge is then Bazel outputs everything into a build output directory (https://bazel.build/remote/output-directories). Is it possible to bake support for moving the snapshot tests to the root src directory after a successful rule has executed?

Bencodes commented 1 year ago

Apart from telling Roborazzi where to write the snapshots to and where to look for the previous ones, this pretty much works out of the box with Bazel:

android_local_test(
    name = "test",
    jvm_flags = [
        # Pass the Roborazzi flags that you need
        "-Droborazzi.test.record=true",
        # Pass in the runfiles path to the baselines
        "-Dsnapshot.baseline='$(rlocationpath baseline.zip)'",
    ],
    data = [
        # Feed the baselines back in
        "baseline.zip",
    ],
)
@RunWith(AndroidJUnit4::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class ExampleSnapshotTest {

    private val workingDirectory = File(System.getenv("TEST_UNDECLARED_OUTPUTS_DIR")!!)

    @get:Rule
    val testRule = createSnapshotTestRule()

    @get:Rule
    val snapshotRule = SnapshotRule(testRule = testRule, outputDirectoryPath = workingDirectory.path)

And you can place the snapshots back by doing:

    // Can be wrapped up into a custom JunitRule
    val runfiles = Runfiles.preload()
    val baselinesZipArchive = File(runfiles.unmapped().rlocation(System.getProperty("snapshot.baseline")!!))
    unzip(baselinesZipArchive, workingDirectory)
bohregard commented 1 year ago

Excellent. Thanks for the insight @Bencodes . It sounds like there isn't anything more to want then. I'll close this for now and reopen if something comes up. :)

Bencodes commented 1 year ago

If there is interest in supporting an open source Bazel specific wrapper around SnapshotRule we could drop it into https://github.com/robolectric/robolectric-bazel so that Bazel users have access to that when setting up Robolectric for Bazel.

takahirom commented 1 year ago

Thank you @bohregard and @Bencodes for discussing Bazel support for Roborazzi. I appreciate your insights and sample code.

I'm considering creating a roborazzi-bazel module with Bazel-specific rules for easier integration. I'll also look into updating documentation, and providing sample projects showcasing the Bazel integration.

Feel free to reach out with any questions or concerns about Bazel integration. I'll monitor the issue tracker and provide support for any Bazel-related issues.

Thanks for your input, and I look forward to improving Roborazzi for the Bazel community.

sanchezz93 commented 1 year ago

Hey everyone, I've synced with @takahirom and I'll be happy to contribute the roborazzi-bazel module.

takahirom commented 1 year ago

@sanchezz93, great to have you on board! Thanks for joining us on the roborazzi-bazel module. Looking forward to it!

utzcoz commented 6 months ago

If there is interest in supporting an open source Bazel specific wrapper around SnapshotRule we could drop it into https://github.com/robolectric/robolectric-bazel so that Bazel users have access to that when setting up Robolectric for Bazel.

@Bencodes is it okay to support Roborazzi in robolectric-bazel? Ideally, Roborazzi might need to create a custom wrapper for itself but depends on robolectric-bazel? Or do you want to support common SnapshotRule in robolectric-bazel?