From 247650940011f676d0a3c094eb960371bed3b109 Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Mon, 29 Jul 2024 12:44:12 -0700 Subject: [PATCH] Add support for reporting snapshot error types (#207) --- .../com/emergetools/snapshots/EmergeSnapshots.kt | 2 ++ .../snapshots/SnapshotImageMetadata.kt | 6 ++++++ .../com/emergetools/snapshots/SnapshotSaver.kt | 4 ++++ .../snapshots/compose/ComposeSnapshotter.kt | 16 ++++++++++++---- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/EmergeSnapshots.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/EmergeSnapshots.kt index 11fab5ff..a63e1ea3 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/EmergeSnapshots.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/EmergeSnapshots.kt @@ -57,12 +57,14 @@ class EmergeSnapshots : TestRule { internal fun saveError( type: SnapshotType, + errorType: SnapshotErrorType, composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, ) { SnapshotSaver.saveError( displayName = null, fqn = fqn, type = type, + errorType = errorType, composePreviewSnapshotConfig = composePreviewSnapshotConfig, ) } diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt index 06c84523..7e17f269 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt @@ -11,6 +11,11 @@ enum class SnapshotType { ACTIVITY, } +enum class SnapshotErrorType { + EMPTY_SNAPSHOT, + GENERAL, +} + @OptIn(ExperimentalSerializationApi::class) @Serializable @JsonClassDiscriminator("metadataType") @@ -45,6 +50,7 @@ sealed class SnapshotMetadata { // FQN of the test class override val fqn: String, override val type: SnapshotType, + val errorType: SnapshotErrorType, // Compose-specific metadata, only set if type == COMPOSABLE override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, ) : SnapshotMetadata() diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt index dd8fe88d..4e893e7f 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt @@ -76,6 +76,7 @@ internal object SnapshotSaver { displayName: String?, fqn: String, type: SnapshotType, + errorType: SnapshotErrorType, composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, ) { val snapshotsDir = File(filesDir, SNAPSHOTS_DIR_NAME) @@ -98,6 +99,7 @@ internal object SnapshotSaver { keyName = keyName, type = type, fqn = fqn, + errorType = errorType, composePreviewSnapshotConfig = composePreviewSnapshotConfig, ) } @@ -144,6 +146,7 @@ internal object SnapshotSaver { displayName: String?, fqn: String, type: SnapshotType, + errorType: SnapshotErrorType, composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, ) { val metadata: SnapshotMetadata = SnapshotMetadata.ErrorMetadata( @@ -151,6 +154,7 @@ internal object SnapshotSaver { displayName = displayName, fqn = fqn, type = type, + errorType = errorType, composePreviewSnapshotConfig = composePreviewSnapshotConfig, ) diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/ComposeSnapshotter.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/ComposeSnapshotter.kt index a29ea6fe..6ce3287a 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/ComposeSnapshotter.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/ComposeSnapshotter.kt @@ -11,7 +11,8 @@ import androidx.compose.runtime.currentComposer import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.unit.IntSize import com.emergetools.snapshots.EmergeSnapshots -import com.emergetools.snapshots.SnapshotType.COMPOSABLE +import com.emergetools.snapshots.SnapshotErrorType +import com.emergetools.snapshots.SnapshotType import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig import kotlin.math.max @@ -83,7 +84,13 @@ fun snapshotComposable( } bitmap?.let { snapshotRule.take(bitmap, previewConfig) - } ?: snapshotRule.saveError(COMPOSABLE, previewConfig) + } ?: run { + snapshotRule.saveError( + type = SnapshotType.COMPOSABLE, + errorType = SnapshotErrorType.EMPTY_SNAPSHOT, + composePreviewSnapshotConfig = previewConfig, + ) + } } } catch (e: Exception) { Log.e( @@ -92,8 +99,9 @@ fun snapshotComposable( e, ) snapshotRule.saveError( - COMPOSABLE, - previewConfig + type = SnapshotType.COMPOSABLE, + errorType = SnapshotErrorType.GENERAL, + composePreviewSnapshotConfig = previewConfig, ) // Re-throw to fail the test throw e