Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: use stable key ordering for snapshots with maps #190

Merged
merged 1 commit into from Nov 22, 2022

Conversation

jamestelfer
Copy link
Contributor

@jamestelfer jamestelfer commented Nov 21, 2022

Snapshots containing maps are unstable because iteration order of a map's keys is not guaranteed. This leads to test instability, where instability increases with the size of the map included in the snapshot.

Stability can be introduced by using the option of the go-spew library that sorts map keys before serialization (the SortKeys option). The SortKeys option only deals with keys that are native types, or types that implement Stringer or error, but this covers a significant proportion of the uses of map.

Breaking change: This change turns the SortKeys option on for all snapshots, potentially invalidating snapshots that already exist. In reality though, given the instability of maps in snapshots currently, it's unlikely any user of the library will be affected.

Further development

There is another option: SpewKeys. This extends the function of SortKeys to other types by first using go-spew to serialize the type to a string. In my opinion, this is likely to either be (a) something a library user might want to opt into, and (b) a less-likely scenario. For this reason, I've left that option as a potential future development.

See also

Snapshots containing maps are unstable because iteration order of a map's
keys is not guaranteed.

The spew library can sort map keys that are sortable to provide
stability at the cost of speed. Some types are not sortable, and there
is a spew option to deal with this.

This option first serializes keys to strings using spew, then sorts.
This behaviour should probably be opt-in, so it's not included here.
@github-actions github-actions bot added the fix label Nov 21, 2022
@codecov
Copy link

codecov bot commented Nov 21, 2022

Codecov Report

Base: 87.02% // Head: 87.04% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (7496539) compared to base (0854b36).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #190      +/-   ##
==========================================
+ Coverage   87.02%   87.04%   +0.01%     
==========================================
  Files          10       10              
  Lines         740      741       +1     
==========================================
+ Hits          644      645       +1     
  Misses         58       58              
  Partials       38       38              
Impacted Files Coverage Δ
snapshot.go 84.00% <100.00%> (+0.21%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions github-actions bot added fix and removed fix labels Nov 21, 2022
@jamestelfer jamestelfer marked this pull request as ready for review November 21, 2022 22:09
@github-actions github-actions bot added fix and removed fix labels Nov 21, 2022
@jamestelfer jamestelfer changed the title fix: use stable key ordering for snapshots with maps fix!: use stable key ordering for snapshots with maps Nov 21, 2022
Copy link
Owner

@MarvinJWendt MarvinJWendt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing! 🚀

There is another option: SpewKeys. This extends the function of SortKeys to other types by first using go-spew to serialize the type to a string.

Sounds interesting, but I agree that this should probably be opt-in.

@MarvinJWendt MarvinJWendt merged commit c1dfc1c into MarvinJWendt:main Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants