Skip to content

Commit

Permalink
Improve the error message for duplicate writes to help the user fix t…
Browse files Browse the repository at this point in the history
…heir problem (#176 fixes #140)
  • Loading branch information
nedtwigg committed Jan 30, 2024
2 parents 50d8286 + aa2a31d commit 4f249ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions jvm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Fixed
- `@ParameterizedTest` no longer has. ([#140](https://github.com/diffplug/selfie/issues/140))
- If a test class contained package-private methods and a single test method was run without the others, selfie would erroneously garbage collect disk snapshots for the other methods, now fixed. ([#175](https://github.com/diffplug/selfie/pull/175) fixes [#174](https://github.com/diffplug/selfie/issues/174))

## [1.1.1] - 2024-01-25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,36 @@ class CallStack(val location: CallLocation, val restOfStack: List<CallLocation>)
internal class FirstWrite<T>(val snapshot: T, val callStack: CallStack)

/** For tracking the writes of disk snapshots literals. */
open class WriteTracker<K : Comparable<K>, V> {
sealed class WriteTracker<K : Comparable<K>, V> {
internal val writes = mutableMapOf<K, FirstWrite<V>>()
protected fun recordInternal(key: K, snapshot: V, call: CallStack, layout: SnapshotFileLayout) {
val existing = writes[key]
if (existing == null) {
writes[key] = FirstWrite(snapshot, call)
} else {
val howToFix =
when (this) {
is DiskWriteTracker ->
"You can fix this with `.toMatchDisk(String sub)` and pass a unique value for sub."
is InlineWriteTracker ->
"""
You can fix this by doing an `if` before the assertion to separate the cases, e.g.
if (isWindows) {
expectSelfie(underTest).toBe("C:\\")
} else {
expectSelfie(underTest).toBe("bash$")
}
"""
.trimIndent()
}
if (existing.snapshot != snapshot) {
throw layout.fs.assertFailed(
"Snapshot was set to multiple values!\n first time: ${existing.callStack.location.ideLink(layout)}\n this time: ${call.location.ideLink(layout)}",
"Snapshot was set to multiple values!\n first time: ${existing.callStack.location.ideLink(layout)}\n this time: ${call.location.ideLink(layout)}\n$howToFix",
existing.snapshot,
snapshot)
} else if (!layout.allowMultipleEquivalentWritesToOneLocation) {
throw layout.fs.assertFailed(
"Snapshot was set to the same value multiple times.",
"Snapshot was set to the same value multiple times.\n$howToFix",
existing.callStack.ideLink(layout),
call.ideLink(layout))
}
Expand Down

0 comments on commit 4f249ec

Please sign in to comment.