Make stricter assertions about change events in random worktree test

This commit is contained in:
Max Brunsfeld 2023-05-23 14:07:10 -07:00
parent f890eefdef
commit 59bfd40679

View file

@ -4331,48 +4331,47 @@ mod tests {
// all changes to the worktree's snapshot. // all changes to the worktree's snapshot.
worktree.update(cx, |tree, cx| { worktree.update(cx, |tree, cx| {
let mut paths = tree let mut paths = tree
.as_local() .entries(true)
.unwrap() .map(|e| (e.path.clone(), e.mtime))
.paths()
.cloned()
.collect::<Vec<_>>(); .collect::<Vec<_>>();
cx.subscribe(&worktree, move |tree, _, event, _| { cx.subscribe(&worktree, move |tree, _, event, _| {
if let Event::UpdatedEntries(changes) = event { if let Event::UpdatedEntries(changes) = event {
for ((path, _), change_type) in changes.iter() { for ((path, _), change_type) in changes.iter() {
let mtime = tree.entry_for_path(&path).map(|e| e.mtime);
let path = path.clone(); let path = path.clone();
let ix = match paths.binary_search(&path) { let ix = match paths.binary_search_by_key(&&path, |e| &e.0) {
Ok(ix) | Err(ix) => ix, Ok(ix) | Err(ix) => ix,
}; };
match change_type { match change_type {
PathChange::Loaded => { PathChange::Loaded => {
assert_ne!(paths.get(ix), Some(&path)); paths.insert(ix, (path, mtime.unwrap()));
paths.insert(ix, path);
} }
PathChange::Added => { PathChange::Added => {
assert_ne!(paths.get(ix), Some(&path)); paths.insert(ix, (path, mtime.unwrap()));
paths.insert(ix, path);
} }
PathChange::Removed => { PathChange::Removed => {
assert_eq!(paths.get(ix), Some(&path));
paths.remove(ix); paths.remove(ix);
} }
PathChange::Updated => { PathChange::Updated => {
assert_eq!(paths.get(ix), Some(&path)); let entry = paths.get_mut(ix).unwrap();
assert_eq!(entry.0, path);
entry.1 = mtime.unwrap();
} }
PathChange::AddedOrUpdated => { PathChange::AddedOrUpdated => {
if paths[ix] != path { if paths.get(ix).map(|e| &e.0) == Some(&path) {
paths.insert(ix, path); paths.get_mut(ix).unwrap().1 = mtime.unwrap();
} else {
paths.insert(ix, (path, mtime.unwrap()));
} }
} }
} }
} }
let new_paths = tree.paths().cloned().collect::<Vec<_>>(); let new_paths = tree
.entries(true)
.map(|e| (e.path.clone(), e.mtime))
.collect::<Vec<_>>();
assert_eq!(paths, new_paths, "incorrect changes: {:?}", changes); assert_eq!(paths, new_paths, "incorrect changes: {:?}", changes);
} }
}) })