matchers: impl custom Debug for RepoPathTree to get stable and concise output

The default Debug output entries aren't sorted by name, which was inconvenient
while writing snapshot tests.
This commit is contained in:
Yuya Nishihara 2024-04-06 13:21:16 +09:00
parent c9b21a16be
commit 7acfab695a

View file

@ -16,8 +16,9 @@
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::iter;
use std::{fmt, iter};
use itertools::Itertools as _;
use tracing::instrument;
use crate::repo_path::{RepoPath, RepoPathComponentBuf};
@ -339,7 +340,7 @@ impl<M1: Matcher, M2: Matcher> Matcher for IntersectionMatcher<M1, M2> {
/// Keeps track of which subdirectories and files of each directory need to be
/// visited.
#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq)]
struct RepoPathTree {
entries: HashMap<RepoPathComponentBuf, RepoPathTree>,
// is_dir/is_file aren't exclusive, both can be set to true. If entries is not empty,
@ -415,6 +416,25 @@ impl RepoPathTree {
}
}
impl Debug for RepoPathTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let type_name = match (self.is_dir, self.is_file) {
(true, true) => "Dir|File",
(true, false) => "Dir",
(false, true) => "File",
(false, false) => "_",
};
write!(f, "{type_name} ")?;
f.debug_map()
.entries(
self.entries
.iter()
.sorted_unstable_by_key(|&(name, _)| name),
)
.finish()
}
}
#[cfg(test)]
mod tests {
use maplit::hashset;