From c0dc69357b9735b73a46b2423f0bc679d192adcb Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 9 Oct 2023 23:27:25 +0200 Subject: [PATCH] Sort snapshot containers (#413) --- CHANGELOG.md | 3 ++- cargo-insta/src/cli.rs | 1 + cargo-insta/src/container.rs | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 085cce10..588afdd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,9 @@ All notable changes to insta and cargo-insta are documented here. ## 1.34.0 -- Fixed handling of `--manifest-path` with regards to virtual workspaces. (#409) +- Snapshots are now sorted in the UI on review. (#413) - Re-organized repository to move `cargo-insta` into a workspace. (#410) +- Fixed handling of `--manifest-path` with regards to virtual workspaces. (#409) ## 1.33.0 diff --git a/cargo-insta/src/cli.rs b/cargo-insta/src/cli.rs index 936fd546..f47e404c 100644 --- a/cargo-insta/src/cli.rs +++ b/cargo-insta/src/cli.rs @@ -443,6 +443,7 @@ fn load_snapshot_containers<'a>( snapshot_containers.push((snapshot_container?, None)); } } + snapshot_containers.sort_by(|a, b| a.0.snapshot_sort_key().cmp(&b.0.snapshot_sort_key())); Ok((snapshot_containers, roots)) } diff --git a/cargo-insta/src/container.rs b/cargo-insta/src/container.rs index 325a7c89..83c8fc5c 100644 --- a/cargo-insta/src/container.rs +++ b/cargo-insta/src/container.rs @@ -140,6 +140,20 @@ impl SnapshotContainer { } } + pub(crate) fn snapshot_sort_key(&self) -> impl Ord + '_ { + let path = self + .snapshot_path + .file_name() + .and_then(|x| x.to_str()) + .unwrap_or_default(); + let mut pieces = path.rsplitn(2, '-'); + if let Some(num_suffix) = pieces.next().and_then(|x| x.parse::().ok()) { + (pieces.next().unwrap_or(""), num_suffix) + } else { + (path, 0) + } + } + pub(crate) fn len(&self) -> usize { self.snapshots.len() }