Skip to content

Commit fdbfc15

Browse files
max-sixtyclaude
andauthored
Revert sort_maps sequence sorting, release 1.47.1 (#891)
Reverts #876 — the `sort_maps` change in 1.47.0 sorted all `Seq` values (including `Vec`), not just non-deterministic collections like `HashSet`. This was a breaking change for anyone relying on ordered sequence output with `sort_maps` enabled. Thanks to @spinda for reporting in #876. > _This was written by Claude Code on behalf of max-sixty_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 46b6f2a commit fdbfc15

7 files changed

Lines changed: 23 additions & 55 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to insta and cargo-insta are documented here.
44

5+
## 1.47.1
6+
7+
- Revert sorting of sequences in `sort_maps`. The change in 1.47.0 sorted all
8+
`Seq` values (including `Vec`), not just non-deterministic collections like
9+
`HashSet`, which was a breaking change. #876
10+
511
## 1.47.0
612

713
- Add `Comparator` trait for customizing how snapshot values are compared. #872 (@dstu)

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-insta/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-insta"
3-
version = "1.47.0"
3+
version = "1.47.1"
44
license = "Apache-2.0"
55
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
66
description = "A review tool for the insta snapshot testing library for Rust"
@@ -14,7 +14,7 @@ readme = "README.md"
1414
rust-version = "1.66.0"
1515

1616
[dependencies]
17-
insta = { version = "=1.47.0", path = "../insta", features = [
17+
insta = { version = "=1.47.1", path = "../insta", features = [
1818
"json",
1919
"yaml",
2020
"redactions",

insta/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "insta"
3-
version = "1.47.0"
3+
version = "1.47.1"
44
license = "Apache-2.0"
55
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
66
description = "A snapshot testing library for Rust"

insta/src/content/serialization.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,18 @@ impl Content {
9494
}
9595
}
9696

97-
fn cmp_as_key(&self, other: &Content) -> Ordering {
98-
match (self.as_key(), other.as_key()) {
99-
(Key::Other, _) | (_, Key::Other) => self.partial_cmp(other).unwrap_or(Ordering::Equal),
100-
(ref a, ref b) => a.cmp(b),
101-
}
102-
}
103-
10497
pub(crate) fn sort_maps(&mut self) {
10598
self.walk(&mut |content| {
106-
match content {
107-
Content::Map(ref mut items) => {
108-
items.sort_by(|a, b| a.0.cmp_as_key(&b.0));
109-
}
110-
Content::Seq(ref mut items) => {
111-
items.sort_by(|a, b| a.cmp_as_key(b));
112-
}
113-
_ => {}
99+
if let Content::Map(ref mut items) = content {
100+
// try to compare by key first, if that fails compare by the
101+
// object value. That way some values normalize, and if we
102+
// can't normalize we still have a stable order.
103+
items.sort_by(|a, b| match (a.0.as_key(), b.0.as_key()) {
104+
(Key::Other, _) | (_, Key::Other) => {
105+
a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal)
106+
}
107+
(ref a, ref b) => a.cmp(b),
108+
})
114109
}
115110
true
116111
})

insta/src/settings.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,7 @@ impl Settings {
235235
Rc::make_mut(&mut self.inner)
236236
}
237237

238-
/// Enables forceful sorting of maps and sequences before serialization.
239-
///
240-
/// This is particularly useful for ensuring deterministic snapshots of
241-
/// non-ordered collections like [`HashMap`](std::collections::HashMap) and
242-
/// [`HashSet`](std::collections::HashSet).
238+
/// Enables forceful sorting of maps before serialization.
243239
///
244240
/// Note that this only applies to snapshots that undergo serialization
245241
/// (eg: does not work for [`assert_debug_snapshot!`](crate::assert_debug_snapshot!)).
@@ -249,7 +245,7 @@ impl Settings {
249245
self._private_inner_mut().sort_maps = value;
250246
}
251247

252-
/// Returns the current value for map and sequence sorting.
248+
/// Returns the current value for map sorting.
253249
pub fn sort_maps(&self) -> bool {
254250
self.inner.sort_maps
255251
}

insta/tests/test_settings.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[cfg(feature = "json")]
2-
use insta::assert_json_snapshot;
31
#[cfg(feature = "yaml")]
42
use insta::assert_yaml_snapshot;
53
use similar_asserts::assert_eq;
@@ -130,33 +128,6 @@ fn test_snapshot_with_description_and_info() {
130128
});
131129
}
132130

133-
#[cfg(feature = "json")]
134-
#[test]
135-
fn test_sort_maps_sorts_hashset() {
136-
with_settings!({sort_maps => true}, {
137-
assert_json_snapshot!(
138-
(
139-
std::collections::HashSet::from([2, 1, 3]),
140-
std::collections::HashMap::from([("one", 1), ("two", 2), ("three", 3)]),
141-
),
142-
@r###"
143-
[
144-
[
145-
1,
146-
2,
147-
3
148-
],
149-
{
150-
"one": 1,
151-
"three": 3,
152-
"two": 2
153-
}
154-
]
155-
"###
156-
);
157-
});
158-
}
159-
160131
#[test]
161132
fn test_with_settings_inherit() {
162133
with_settings!({sort_maps => true}, {

0 commit comments

Comments
 (0)