Skip to content

Commit

Permalink
Couple of small refinements to prefix / suffixes
Browse files Browse the repository at this point in the history
Spotted when reviewing code; this uses native rust methods rather than specific slicing
  • Loading branch information
max-sixty committed Sep 24, 2024
1 parent caa1e8f commit 155f9f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
28 changes: 15 additions & 13 deletions cargo-insta/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ pub(crate) fn find_pending_snapshots<'a>(
flags: FindFlags,
) -> impl Iterator<Item = Result<SnapshotContainer, Box<dyn Error>>> + 'a {
make_snapshot_walker(package_root, extensions, flags)
.filter_map(|e| e.ok())
.filter_map(move |e| {
let fname = e.file_name().to_string_lossy();
if fname.ends_with(".new") {
let new_path = e.into_path();
let old_path = new_path.clone().with_extension("");
.filter_map(Result::ok)
.filter_map(|entry| {
let fname = entry.file_name().to_string_lossy();
let path = entry.clone().into_path();

#[allow(clippy::manual_map)]
if let Some(new_fname) = fname.strip_suffix(".new") {
Some(SnapshotContainer::load(
new_path,
old_path,
path.clone(),
path.with_file_name(new_fname),
SnapshotKind::File,
))
} else if fname.starts_with('.') && fname.ends_with(".pending-snap") {
let mut target_path = e.path().to_path_buf();
target_path.set_file_name(&fname[1..fname.len() - 13]);
} else if let Some(new_fname) = fname
.strip_prefix('.')
.and_then(|f| f.strip_suffix(".pending-snap"))
{
Some(SnapshotContainer::load(
e.path().to_path_buf(),
target_path,
path.clone(),
path.with_file_name(new_fname),
SnapshotKind::Inline,
))
} else {
Expand Down
15 changes: 7 additions & 8 deletions insta/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,14 @@ fn is_doctest(function_name: &str) -> bool {
}

fn detect_snapshot_name(function_name: &str, module_path: &str) -> Result<String, &'static str> {
let mut name = function_name;

// clean test name first
name = name.rsplit("::").next().unwrap();
let mut test_prefixed = false;
if name.starts_with("test_") {
name = &name[5..];
test_prefixed = true;
}
let name = function_name.rsplit("::").next().unwrap();

let (name, test_prefixed) = if let Some(stripped) = name.strip_prefix("test_") {
(stripped, true)
} else {
(name, false)
};

// next check if we need to add a suffix
let name = add_suffix_to_snapshot_name(Cow::Borrowed(name));
Expand Down

0 comments on commit 155f9f2

Please sign in to comment.