Skip to content

Commit

Permalink
Add support for reporting snapshot error types (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbro112 authored Jul 29, 2024
1 parent d27e62a commit 2476509
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ enum class SnapshotType {
ACTIVITY,
}

enum class SnapshotErrorType {
EMPTY_SNAPSHOT,
GENERAL,
}

@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonClassDiscriminator("metadataType")
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -98,6 +99,7 @@ internal object SnapshotSaver {
keyName = keyName,
type = type,
fqn = fqn,
errorType = errorType,
composePreviewSnapshotConfig = composePreviewSnapshotConfig,
)
}
Expand Down Expand Up @@ -144,13 +146,15 @@ internal object SnapshotSaver {
displayName: String?,
fqn: String,
type: SnapshotType,
errorType: SnapshotErrorType,
composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null,
) {
val metadata: SnapshotMetadata = SnapshotMetadata.ErrorMetadata(
name = keyName,
displayName = displayName,
fqn = fqn,
type = type,
errorType = errorType,
composePreviewSnapshotConfig = composePreviewSnapshotConfig,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 2476509

Please sign in to comment.