Skip to content

Conversation

@robbiemcmichael
Copy link

@robbiemcmichael robbiemcmichael commented Nov 15, 2025

Summary

Ensure deterministic order when diffing maps to make the diffs readable.

Changes

Enable the SpewKeys setting in spew.ConfigState.

Motivation

When generating the diff for two maps where the map key is not a basic type, the order is non-deterministic which makes the diffs unreadable.

By enabling the SpewKeys setting, the map keys will be spewed to strings for the purpose of sorting them. This results in a deterministic order and makes the diffs readable.

For the test case added in this PR, the old config that has SpewKeys disabled would look like this:

--- Expected
+++ Actual
@@ -1,5 +1,2 @@
 (map[assert.Key]int) (len=4) {
- (assert.Key) {
-  x: (int) 4
- }: (int) 4,
  (assert.Key) {
@@ -12,3 +9,6 @@
   x: (int) 3
- }: (int) 3
+ }: (int) 3,
+ (assert.Key) {
+  x: (int) 4
+ }: (int) 999
 }

After enabling SpewKeys, we get the simpler diff that we would expect:

--- Expected
+++ Actual
@@ -12,3 +12,3 @@
   x: (int) 4
- }: (int) 4
+ }: (int) 999
 }

When generating the diff for two maps where the map key is not a basic
type, the order is non-deterministic which makes the diffs unreadable.

By enabling the SpewKeys setting, the map keys will be spewed to strings
for the purpose of sorting them. This results in a deterministic order
and makes the diffs readable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants