Fix common_path stemming and start testing summarise_events_to_env
This commit is contained in:
parent
f304774a4f
commit
e9275702d1
|
@ -6,7 +6,7 @@ use std::{
|
|||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::event::{Event, Tag};
|
||||
use crate::event::{Event, FileType, Tag};
|
||||
|
||||
/// Returns the longest common prefix of all given paths.
|
||||
///
|
||||
|
@ -72,15 +72,30 @@ pub fn summarise_events_to_env<'events>(
|
|||
#[cfg(not(unix))]
|
||||
const ENV_SEP: &str = ";";
|
||||
|
||||
let mut all_paths = Vec::new();
|
||||
let mut all_trunks = Vec::new();
|
||||
let mut kind_buckets = HashMap::new();
|
||||
for event in events {
|
||||
let paths = event.paths().map(|(p, _)| p.to_owned()).collect::<Vec<_>>();
|
||||
let (paths, trunks): (Vec<_>, Vec<_>) = event
|
||||
.paths()
|
||||
.map(|(p, ft)| {
|
||||
(
|
||||
p.to_owned(),
|
||||
match ft {
|
||||
Some(FileType::Dir) => None,
|
||||
_ => p.parent(),
|
||||
}
|
||||
.unwrap_or(p)
|
||||
.to_owned(),
|
||||
)
|
||||
})
|
||||
.unzip();
|
||||
tracing::trace!(?paths, ?trunks, "event paths");
|
||||
|
||||
if paths.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
all_paths.extend(paths.clone());
|
||||
all_trunks.extend(trunks.clone());
|
||||
|
||||
// usually there's only one but just in case
|
||||
for kind in event.tags.iter().filter_map(|t| {
|
||||
|
@ -97,7 +112,7 @@ pub fn summarise_events_to_env<'events>(
|
|||
}
|
||||
}
|
||||
|
||||
let common_path = common_prefix(all_paths);
|
||||
let common_path = common_prefix(all_trunks);
|
||||
|
||||
let mut grouped_buckets = HashMap::new();
|
||||
for (kind, paths) in kind_buckets {
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
ffi::{OsStr, OsString},
|
||||
};
|
||||
|
||||
use notify::event::CreateKind;
|
||||
use watchexec::{
|
||||
event::{filekind::*, Event, Tag},
|
||||
paths::summarise_events_to_env,
|
||||
};
|
||||
|
||||
fn ospath(path: &str) -> OsString {
|
||||
let root = dunce::canonicalize(".").unwrap();
|
||||
if path.is_empty() {
|
||||
root
|
||||
} else {
|
||||
root.join(path)
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn event(path: &str, kind: FileEventKind) -> Event {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let path = dunce::canonicalize(".").unwrap().join(path);
|
||||
Event {
|
||||
tags: vec![
|
||||
Tag::Path {
|
||||
path,
|
||||
file_type: None,
|
||||
},
|
||||
Tag::FileEventKind(kind),
|
||||
],
|
||||
metadata: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_events_no_env() {
|
||||
let events = Vec::<Event>::new();
|
||||
assert_eq!(summarise_events_to_env(&events), HashMap::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_created() {
|
||||
let events = vec![event("file.txt", FileEventKind::Create(CreateKind::File))];
|
||||
assert_eq!(
|
||||
summarise_events_to_env(&events),
|
||||
HashMap::from([
|
||||
(OsStr::new("CREATED"), OsString::from("file.txt")),
|
||||
(OsStr::new("COMMON_PATH"), ospath("")),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_meta() {
|
||||
let events = vec![event(
|
||||
"file.txt",
|
||||
FileEventKind::Modify(ModifyKind::Metadata(MetadataKind::Any)),
|
||||
)];
|
||||
assert_eq!(
|
||||
summarise_events_to_env(&events),
|
||||
HashMap::from([
|
||||
(OsStr::new("META_CHANGED"), OsString::from("file.txt")),
|
||||
(OsStr::new("COMMON_PATH"), ospath("")),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_removed() {
|
||||
let events = vec![event("file.txt", FileEventKind::Remove(RemoveKind::File))];
|
||||
assert_eq!(
|
||||
summarise_events_to_env(&events),
|
||||
HashMap::from([
|
||||
(OsStr::new("REMOVED"), OsString::from("file.txt")),
|
||||
(OsStr::new("COMMON_PATH"), ospath("")),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_renamed() {
|
||||
let events = vec![event(
|
||||
"file.txt",
|
||||
FileEventKind::Modify(ModifyKind::Name(RenameMode::Any)),
|
||||
)];
|
||||
assert_eq!(
|
||||
summarise_events_to_env(&events),
|
||||
HashMap::from([
|
||||
(OsStr::new("RENAMED"), OsString::from("file.txt")),
|
||||
(OsStr::new("COMMON_PATH"), ospath("")),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_written() {
|
||||
let events = vec![event(
|
||||
"file.txt",
|
||||
FileEventKind::Modify(ModifyKind::Data(DataChange::Any)),
|
||||
)];
|
||||
assert_eq!(
|
||||
summarise_events_to_env(&events),
|
||||
HashMap::from([
|
||||
(OsStr::new("WRITTEN"), OsString::from("file.txt")),
|
||||
(OsStr::new("COMMON_PATH"), ospath("")),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_otherwise() {
|
||||
let events = vec![event("file.txt", FileEventKind::Any)];
|
||||
assert_eq!(
|
||||
summarise_events_to_env(&events),
|
||||
HashMap::from([
|
||||
(OsStr::new("OTHERWISE_CHANGED"), OsString::from("file.txt")),
|
||||
(OsStr::new("COMMON_PATH"), ospath("")),
|
||||
])
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue