showkaseName for composable is unstable when referencing custom multipreview annotation from another module
When I was looking into why the paparazzi snapshot keep generating a new golden images for existing components whenever I add a new component, I found out that it may be because of the name generation although I'm not really quite sure exactly if it's related yet. When a composable has a custom multipreview annotation from another module ,every time you add a composable to that module, the name will be generated with different integer in it.
To demonstrate, follow these steps:
- Create a module, we call this
rootModule, since this is where ShowKaseRoot will live. Setup ShowKase here - In this Module, create a composable,
ComposableB
@Composable
fun ComposableB() { /*...*/ }
- Create an Android library module, for future reference, we call this
liband setup ShowKase there - Create a custom multipreview annotation in the
libmodule
@Preview(group = "Large", fontScale = 2.0f, showBackground = true)
@Preview(group = "XLarge", fontScale = 3.0f, showBackground = true)
@Preview(group = "Small", fontScale = 0.1f, showBackground = true)
annotation class PreviewGroupFromAnotherModule
- Add a preview for
ComposableBback inrootModulewith this annotation
@PreviewGroupFromAnotherModule
@Composable
fun ComposableBPreview() {
ComposableB()
}
- Rebuild project and check the code generated and we see this for showkase metadata
@ShowkaseCodegenMetadata(
showkaseName = "ComposableBPreview - - 0",
...
)
- Create another Composable in
rootModuleand preview with same preview group
@Composable
fun ComposableA() { /*...*/ }
@PreviewGroupFromAnotherModule
@Composable
fun ComposableAPreview() {
ComposableA()
}
- Rebuild the project and observe that the name for
ComposableBPreviewhas now changed from previously 0 to now 1
@ShowkaseCodegenMetadata(
showkaseName = "ComposableAPreview - - 0",
//...
)
//...
@ShowkaseCodegenMetadata(
showkaseName = "ComposableBPreview - - 1",
//...
)
I don't observe this problem when using Preview or when the custom multipreview annotation is from same module. This only happens when referencing the one from another module. And this integer shift only affects previews that are lower in term of alphabetical order than the new one. In above example, B used to be 1. Then adding A shift B to 1. Adding ComposableC would not affect ComposableB
This impact us greatly because we have a common multipreview annotations in our core module referenced by different feature module. Every time we add a new composable, all of our snapshots are regenerated and we can't rely anything from snapshot test because of this bug. It also would not make sense to have the same preview groups across all features for us that would be a lot harder to maintain.
I have a made a repo here to test out easily
I believe this number is the elementIndex when generating ShowkaseMetadata.Component as:
showkaseName = "${xElement.name} - ${customPreviewMetadata.previewName} - $elementIndex",
Since in this example the customPreviewMetadata.previewName is null we get the - - #
You can just filter this out using regex if it is used as part of a snapshot filename (e.g. when using Paparazzi).
For example:
showkaseBrowserComponent.componentName.replace("""- - \d$""".toRegex(), "")
we used this and it is working
showkaseBrowserComponent.componentName.replace(Regex(" - \\d*"), "")