From a890b0c916da72661de510e042a60afe12cf7fc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Mon, 31 Jan 2022 02:14:18 +1300 Subject: [PATCH] Deduplicate paths in summarise_events_to_env Fixes #253 --- lib/src/paths.rs | 9 +++++---- lib/tests/env_reporting.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/src/paths.rs b/lib/src/paths.rs index 13c3a01..056fc78 100644 --- a/lib/src/paths.rs +++ b/lib/src/paths.rs @@ -1,7 +1,7 @@ //! Utilities for paths and sets of paths. use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, ffi::OsString, path::{Path, PathBuf}, }; @@ -109,7 +109,7 @@ pub fn summarise_events_to_env<'events>( }) { kind_buckets .entry(kind) - .or_insert_with(Vec::new) + .or_insert_with(HashSet::new) .extend(paths.clone()); } } @@ -128,7 +128,7 @@ pub fn summarise_events_to_env<'events>( Modify(Name(_)) => "RENAMED", _ => "OTHERWISE_CHANGED", }) - .or_insert_with(Vec::new) + .or_insert_with(HashSet::new) .extend(paths.into_iter().map(|p| { if let Some(suffix) = common_path .as_ref() @@ -143,10 +143,11 @@ pub fn summarise_events_to_env<'events>( let mut res: HashMap<&'static str, OsString> = grouped_buckets .into_iter() - .map(|(kind, mut paths)| { + .map(|(kind, paths)| { let mut joined = OsString::with_capacity(paths.iter().map(|p| p.len()).sum::() + paths.len()); + let mut paths = paths.into_iter().collect::>(); paths.sort(); paths.into_iter().enumerate().for_each(|(i, path)| { if i > 0 { diff --git a/lib/tests/env_reporting.rs b/lib/tests/env_reporting.rs index 849c302..76e413e 100644 --- a/lib/tests/env_reporting.rs +++ b/lib/tests/env_reporting.rs @@ -322,3 +322,33 @@ fn multipath_is_sorted() { ]) ); } + +#[test] +fn multipath_is_deduped() { + let events = vec![ + event("0123.txt", FileEventKind::Any), + event("0123.txt", FileEventKind::Any), + event("a.txt", FileEventKind::Any), + event("a.txt", FileEventKind::Any), + event("b.txt", FileEventKind::Any), + event("b.txt", FileEventKind::Any), + event("c.txt", FileEventKind::Any), + event("ᄁ.txt", FileEventKind::Any), + event("ᄁ.txt", FileEventKind::Any), + ]; + assert_eq!( + summarise_events_to_env(&events), + HashMap::from([ + ( + "OTHERWISE_CHANGED", + OsString::from( + "".to_string() + + "0123.txt" + ENV_SEP + "a.txt" + + ENV_SEP + "b.txt" + ENV_SEP + + "c.txt" + ENV_SEP + "ᄁ.txt" + ) + ), + ("COMMON", ospath("")), + ]) + ); +}