log2src 0.1.0

log2src maps logs back to source code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
use crate::{LogError, SourceLanguage};
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::ffi::{OsStr, OsString};
use std::path::{Component, Path, PathBuf};
use std::time::SystemTime;
use std::{fs, io};

fn is_ignored_dir(name: &OsStr) -> bool {
    name == ".git" || name == ".hg" || name == ".svn" || name == ".vscode"
}

fn build_gitignore(root: &Path) -> Gitignore {
    let mut builder = GitignoreBuilder::new(root);
    builder.add(root.join(".gitignore"));
    builder.build().unwrap_or_else(|_| Gitignore::empty())
}

fn is_gitignored(gi: &Gitignore, path: &Path, is_dir: bool) -> bool {
    gi.matched_path_or_any_parents(path, is_dir).is_ignore()
}

/// Result of a shallow check of a file system path.  Mainly interested in getting a directory
/// listing without descending into the child trees.
enum ShallowCheckResult {
    File {
        latest_modified_time: SystemTime,
    },
    Directory {
        latest_entries: BTreeMap<OsString, Result<fs::Metadata, io::Error>>,
    },
    Error,
}

/// A unique identifier for a file that can be used instead of retaining the full path.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct SourceFileID(usize);

/// A summary of a source code file
#[derive(Copy, Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct SourceFileInfo {
    pub language: SourceLanguage,
    pub id: SourceFileID,
}

impl SourceFileInfo {
    // Allow the ID counter to be set for a thread from the SourceHierTree.
    thread_local! {
        static NEXT_ID: RefCell<usize> = const { RefCell::new(0) };
    }

    pub fn new(language: SourceLanguage) -> Self {
        Self::NEXT_ID.with(|next_id| {
            let mut inner = next_id.borrow_mut();
            let id = SourceFileID(*inner);
            *inner += 1;
            Self { language, id }
        })
    }
}

/// The type of content in a node in the source hierarchy
#[derive(Debug, Serialize, Deserialize)]
pub enum SourceHierContent {
    File {
        info: SourceFileInfo,
        last_modified_time: SystemTime,
    },
    UnsupportedFile {},
    Directory {
        entries: BTreeMap<OsString, SourceHierNode>,
    },
    Error {
        #[serde(skip)]
        source: LogError,
    },
    Unknown {},
}

impl SourceHierContent {
    fn entries_of(
        path: &Path,
    ) -> Result<BTreeMap<OsString, Result<fs::Metadata, io::Error>>, io::Error> {
        Ok(fs::read_dir(path)?
            .flat_map(|entry| match entry {
                Ok(entry) => Some((entry.file_name(), entry.metadata())),
                Err(_err) => None,
            })
            .collect())
    }

    fn from_dir(path: &Path, gi: &Gitignore) -> Self {
        match Self::entries_of(path) {
            Ok(entries) => Self::Directory {
                entries: entries
                    .into_iter()
                    .filter(|entry| {
                        !is_ignored_dir(&entry.0) && {
                            let child_path = path.join(&entry.0);
                            let is_dir = entry.1.as_ref().map(|m| m.is_dir()).unwrap_or(false);
                            !is_gitignored(gi, &child_path, is_dir)
                        }
                    })
                    .map(|(entry_name, meta)| {
                        (
                            entry_name.to_os_string(),
                            SourceHierNode::from_int(&path.join(entry_name), meta, gi),
                        )
                    })
                    .collect(),
            },
            Err(err) => Self::Error {
                source: LogError::CannotAccessPath {
                    path: path.to_path_buf(),
                    source: err.into(),
                },
            },
        }
    }

    fn from(path: &Path, metadata: Result<fs::Metadata, io::Error>, gi: &Gitignore) -> Self {
        match metadata {
            Ok(meta) => {
                if meta.is_dir() {
                    Self::from_dir(path, gi)
                } else if meta.is_file() {
                    match SourceLanguage::from_path(path) {
                        Some(language) => match meta.modified() {
                            Ok(last_modified_time) => Self::File {
                                info: SourceFileInfo::new(language),
                                last_modified_time,
                            },
                            Err(err) => Self::Error {
                                source: LogError::CannotAccessPath {
                                    path: path.to_path_buf(),
                                    source: err.into(),
                                },
                            },
                        },
                        None => Self::UnsupportedFile {},
                    }
                } else {
                    Self::Unknown {}
                }
            }
            Err(err) => Self::Error {
                source: LogError::CannotAccessPath {
                    path: path.to_path_buf(),
                    source: err.into(),
                },
            },
        }
    }

    fn shallow_check(
        path: &Path,
        metadata: &Result<fs::Metadata, io::Error>,
    ) -> ShallowCheckResult {
        match metadata {
            Ok(meta) => {
                if meta.is_file() {
                    match meta.modified() {
                        Ok(latest_modified_time) => ShallowCheckResult::File {
                            latest_modified_time,
                        },
                        Err(_) => ShallowCheckResult::Error,
                    }
                } else if meta.is_dir() {
                    match Self::entries_of(path) {
                        Ok(latest_entries) => ShallowCheckResult::Directory { latest_entries },
                        Err(_) => ShallowCheckResult::Error,
                    }
                } else {
                    ShallowCheckResult::Error
                }
            }
            Err(_) => ShallowCheckResult::Error,
        }
    }

    /// Synchronized this content with the current state on the file system.
    ///
    /// # Cases
    /// ## Files
    /// If a file exists and has the same modified time as the last sync, nothing is done.
    /// Otherwise, a new content value is created from the file system state and self is
    /// overwritten with that value.
    ///
    /// ## Directories
    /// A shallow scan of the directory is done to gather file names and metadata.  If files in
    /// the current state are not found in the shallow scan, ScanEvent::DeletedFile events will
    /// be added to the "deleted_events" vector.  If files in the current state are in the
    /// shallow state, they will be synced individually.  If there are files in the shallow scan
    /// that are not in the current state, they will be instantiated added as children of the
    /// directory.
    fn sync_int(
        &mut self,
        path: &Path,
        latest_meta: Result<fs::Metadata, io::Error>,
        deleted_events: &mut Vec<ScanEvent>,
        gi: &Gitignore,
    ) -> bool {
        let latest_content = Self::shallow_check(path, &latest_meta);
        *self = match self {
            SourceHierContent::File {
                last_modified_time,
                info,
                ..
            } => match latest_content {
                ShallowCheckResult::File {
                    latest_modified_time,
                    ..
                } if *last_modified_time == latest_modified_time => {
                    return false;
                }
                _ => {
                    deleted_events.push(ScanEvent::DeletedFile(PathBuf::from(path), info.id));
                    Self::from(path, latest_meta, gi)
                }
            },
            SourceHierContent::Directory { ref mut entries } => match latest_content {
                ShallowCheckResult::Directory { latest_entries } => {
                    let mut changed = false;
                    entries.retain(|name, node| {
                        let exists = latest_entries.contains_key(name);
                        if !exists {
                            node.deleted(path, name, deleted_events);
                            changed = true;
                        }
                        exists
                    });
                    let mut new_entries: Vec<(OsString, Result<fs::Metadata, io::Error>)> =
                        Vec::new();
                    for (name, meta) in latest_entries {
                        let child_path = path.join(&name);
                        let is_dir = meta.as_ref().map(|m| m.is_dir()).unwrap_or(false);
                        if is_ignored_dir(name.as_os_str())
                            || is_gitignored(gi, &child_path, is_dir)
                        {
                        } else if let Some(existing_entry) = entries.get_mut(&name) {
                            existing_entry.sync(&child_path, meta, deleted_events, gi)
                        } else {
                            new_entries.push((name, meta));
                            changed = true;
                        }
                    }
                    new_entries.into_iter().for_each(|(name, meta)| {
                        let node = SourceHierNode::from_int(&path.join(&name), meta, gi);
                        entries.insert(name, node);
                    });
                    return changed;
                }
                _ => Self::from(path, latest_meta, gi),
            },
            _ => Self::from(path, latest_meta, gi),
        };
        true
    }

    pub fn find_file(
        &self,
        self_path: &Path,
        desired_path: &Path,
        accum: &mut Vec<(PathBuf, SourceFileInfo)>,
    ) {
        match self {
            SourceHierContent::File { info, .. } if desired_path == Path::new("") => {
                accum.push((self_path.to_path_buf(), *info));
            }
            SourceHierContent::Directory { ref entries } => {
                let mut components = desired_path.components();
                if let Some(Component::Normal(name)) = components.next() {
                    if let Some(node) = entries.get(name) {
                        node.content
                            .find_file(&self_path.join(name), components.as_path(), accum);
                    } else {
                        for (name, entry) in entries {
                            let sub_path = self_path.join(name);
                            entry.content.find_file(&sub_path, desired_path, accum);
                        }
                    }
                }
            }
            _ => {}
        }
    }
}

/// A node in the SourceHierTree.  It contains information that is common to all types of content
/// and the content itself (e.g. file, directory, error, ...).
#[derive(Debug, Serialize, Deserialize)]
pub struct SourceHierNode {
    pub last_scan_time: Option<SystemTime>,
    pub content: SourceHierContent,
}

impl SourceHierNode {
    fn from_int(path: &Path, metadata: Result<fs::Metadata, io::Error>, gi: &Gitignore) -> Self {
        match metadata {
            Ok(meta) => {
                if meta.is_dir() {
                    Self {
                        last_scan_time: None,
                        content: SourceHierContent::from_dir(path, gi),
                    }
                } else if meta.is_file() {
                    match SourceLanguage::from_path(path) {
                        Some(language) => match meta.modified() {
                            Ok(last_modified_time) => Self {
                                last_scan_time: None,
                                content: SourceHierContent::File {
                                    info: SourceFileInfo::new(language),
                                    last_modified_time,
                                },
                            },
                            Err(err) => Self {
                                last_scan_time: None,
                                content: SourceHierContent::Error {
                                    source: LogError::CannotAccessPath {
                                        path: path.to_path_buf(),
                                        source: err.into(),
                                    },
                                },
                            },
                        },
                        None => Self {
                            last_scan_time: None,
                            content: SourceHierContent::UnsupportedFile {},
                        },
                    }
                } else {
                    Self {
                        last_scan_time: None,
                        content: SourceHierContent::Unknown {},
                    }
                }
            }
            Err(err) => Self {
                last_scan_time: None,
                content: SourceHierContent::Error {
                    source: LogError::CannotAccessPath {
                        path: path.to_path_buf(),
                        source: err.into(),
                    },
                },
            },
        }
    }

    /// Create a node with unknown content that will be synced at a later point.
    fn stub() -> Self {
        SourceHierNode {
            last_scan_time: None,
            content: SourceHierContent::Unknown {},
        }
    }

    fn deleted(&self, path: &Path, name: &OsStr, deleted_events: &mut Vec<ScanEvent>) {
        match &self.content {
            SourceHierContent::File { info, .. } => {
                deleted_events.push(ScanEvent::DeletedFile(path.join(name), info.id))
            }
            SourceHierContent::UnsupportedFile { .. } => {}
            SourceHierContent::Directory { entries } => {
                let dir_path = path.join(name);
                for (child_name, node) in entries {
                    node.deleted(&dir_path, child_name, deleted_events);
                }
            }
            SourceHierContent::Error { .. } => {}
            SourceHierContent::Unknown { .. } => {}
        }
    }

    fn sync(
        &mut self,
        path: &Path,
        meta: Result<fs::Metadata, io::Error>,
        deleted_events: &mut Vec<ScanEvent>,
        gi: &Gitignore,
    ) {
        if self.content.sync_int(path, meta, deleted_events, gi) {
            self.last_scan_time = None;
        }
    }
}

/// An event when iterating over the value returned by the [`scan()`](SourceHierTree::scan())
/// method.
#[derive(Debug, Serialize, Deserialize)]
pub enum ScanEvent {
    NewFile(PathBuf, SourceFileInfo),
    DeletedFile(PathBuf, SourceFileID),
}

struct TreeCursorMut<'a> {
    curr_path: PathBuf,
    curr_node: &'a mut SourceHierNode,
}

pub struct TreeScanner<'a> {
    deleted_events: Vec<ScanEvent>,
    stack: Vec<TreeCursorMut<'a>>,
}

impl Iterator for TreeScanner<'_> {
    type Item = ScanEvent;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(event) = self.deleted_events.pop() {
            return Some(event);
        }
        while let Some(cursor) = self.stack.pop() {
            let last_scan_time = cursor.curr_node.last_scan_time;
            cursor.curr_node.last_scan_time = Some(SystemTime::now());
            match &mut cursor.curr_node.content {
                SourceHierContent::File { info, .. } => match last_scan_time {
                    Some(_) => {}
                    _ => return Some(ScanEvent::NewFile(cursor.curr_path, *info)),
                },
                SourceHierContent::UnsupportedFile { .. } => {}
                SourceHierContent::Directory { ref mut entries } => {
                    for child in entries.iter_mut() {
                        self.stack.push(TreeCursorMut {
                            curr_path: cursor.curr_path.join(child.0),
                            curr_node: child.1,
                        });
                    }
                }
                SourceHierContent::Error { .. } => {}
                SourceHierContent::Unknown {} => {}
            }
        }
        None
    }
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct SourceHierStats {
    pub files: usize,
    pub unsupported_files: usize,
    pub directories: usize,
    pub errors: usize,
}

/// A SourceHierTree tracks the state of a source code hierarchy.
#[derive(Debug, Serialize, Deserialize)]
pub struct SourceHierTree {
    pub root_path: PathBuf,
    pub root_node: SourceHierNode,
    next_id: usize,
    #[serde(skip)]
    deleted_events: Vec<ScanEvent>,
    stats: SourceHierStats,
}

impl SourceHierTree {
    pub fn from(path: &Path) -> SourceHierTree {
        SourceHierTree {
            root_path: path.to_path_buf(),
            root_node: SourceHierNode::stub(),
            next_id: 0,
            deleted_events: Vec::new(),
            stats: SourceHierStats::default(),
        }
    }

    /// Synchronize the state of this tree with the file system.
    pub fn sync(&mut self) {
        let gi = build_gitignore(&self.root_path);
        SourceFileInfo::NEXT_ID.with(|id_opt| {
            *id_opt.borrow_mut() = self.next_id;
        });
        self.root_node.sync(
            &self.root_path,
            fs::metadata(&self.root_path),
            &mut self.deleted_events,
            &gi,
        );
        self.next_id = SourceFileInfo::NEXT_ID.with(|id_opt| *id_opt.borrow());
        self.stats = self.compute_stats();
    }

    /// Scan the tree for changes that have happened since the last scan.  Changes to the tree
    /// are introduced by the sync() method.
    pub fn scan(&'_ mut self) -> TreeScanner<'_> {
        let deleted_events = std::mem::take(&mut self.deleted_events);
        TreeScanner {
            deleted_events,
            stack: vec![TreeCursorMut {
                curr_path: self.root_path.clone(),
                curr_node: &mut self.root_node,
            }],
        }
    }

    pub fn find_file(&self, path: &Path) -> Vec<(PathBuf, SourceFileInfo)> {
        let path_to_find = if path.is_absolute() {
            match path.strip_prefix(&self.root_path) {
                Ok(sub_path) => sub_path,
                Err(_) => return vec![],
            }
        } else {
            path
        };
        let mut retval = vec![];
        self.root_node
            .content
            .find_file(&self.root_path, path_to_find, &mut retval);
        retval
    }

    /// Visit every node in the hierarchy, depth-first, calling `f` on each.
    pub fn visit<F>(&self, mut f: F)
    where
        F: FnMut(&SourceHierNode),
    {
        fn walk<F>(node: &SourceHierNode, f: &mut F)
        where
            F: FnMut(&SourceHierNode),
        {
            f(node);
            if let SourceHierContent::Directory { entries } = &node.content {
                for child in entries.values() {
                    walk(child, f);
                }
            }
        }
        walk(&self.root_node, &mut f);
    }

    fn compute_stats(&self) -> SourceHierStats {
        let mut retval = SourceHierStats::default();

        self.visit(|node| match node.content {
            SourceHierContent::File { .. } => retval.files += 1,
            SourceHierContent::UnsupportedFile { .. } => retval.unsupported_files += 1,
            SourceHierContent::Directory { .. } => retval.directories += 1,
            SourceHierContent::Error { .. } => retval.errors += 1,
            SourceHierContent::Unknown { .. } => {}
        });

        retval
    }

    pub fn stats(&self) -> &SourceHierStats {
        &self.stats
    }
}

#[cfg(test)]
mod test {
    use crate::source_hier::{ScanEvent, SourceFileID, SourceFileInfo, SourceHierTree};
    use crate::SourceLanguage;
    use fs_extra::dir::copy;
    use fs_extra::dir::CopyOptions;
    use insta::assert_yaml_snapshot;
    use std::fs;
    use std::fs::File;
    use std::io::Write;
    use std::path::Path;
    use std::path::PathBuf;
    use tempfile::{tempdir, TempDir};

    fn setup_test_environment(source_dir: &Path) -> TempDir {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let dest_path = temp_dir.path().to_path_buf();

        // Copy the source directory content to the temporary directory
        let mut options = CopyOptions::new();
        options.overwrite = true; // Overwrite if files exist
        options.copy_inside = true; // Copy contents of source_dir into dest_path

        copy(source_dir, &dest_path, &options)
            .expect("Failed to copy source directory to temporary directory");

        temp_dir
    }

    fn redact_event(event: ScanEvent) -> ScanEvent {
        match event {
            ScanEvent::NewFile(path, info) => {
                ScanEvent::NewFile(path.file_name().unwrap().into(), info)
            }
            ScanEvent::DeletedFile(path, id) => {
                ScanEvent::DeletedFile(path.file_name().unwrap().into(), id)
            }
        }
    }

    #[test]
    fn test_with_resources_dir() {
        let tests_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests");
        let temp_test_dir = setup_test_environment(&tests_path);
        fs::create_dir(temp_test_dir.path().join(".git")).unwrap();
        let _ = File::create_new(temp_test_dir.path().join(".git/config"))
            .unwrap()
            .write("abc".as_bytes())
            .unwrap();
        let basic_path = temp_test_dir.path().join("tests/java/Basic.java");
        {
            let metadata = fs::metadata(&basic_path).unwrap();
            let mut perms = metadata.permissions();
            make_writable(&mut perms);
            fs::set_permissions(&basic_path, perms).unwrap();
        }
        let mut tree = SourceHierTree::from(temp_test_dir.path());
        tree.sync();
        let events: Vec<ScanEvent> = tree.scan().map(redact_event).collect();
        assert_yaml_snapshot!(events);
        let find_res = tree.find_file(&basic_path);
        assert_eq!(
            find_res,
            vec![(
                basic_path.clone(),
                SourceFileInfo {
                    language: SourceLanguage::Java,
                    id: SourceFileID(1)
                }
            )]
        );
        let no_events: Vec<ScanEvent> = tree.scan().map(redact_event).collect();
        assert_yaml_snapshot!(no_events);
        fs::remove_file(temp_test_dir.path().join("tests/test_java.rs")).unwrap();
        let _ = File::create(temp_test_dir.path().join("new.rs"))
            .unwrap()
            .write("abc".as_bytes())
            .unwrap();
        let _ = File::options()
            .append(true)
            .open(&basic_path)
            .unwrap()
            .write("def".as_bytes())
            .unwrap();
        tree.sync();
        let new_and_updated_events: Vec<ScanEvent> = tree.scan().map(redact_event).collect();
        assert_yaml_snapshot!(new_and_updated_events);
        fs::remove_dir_all(temp_test_dir.path().join("tests/java")).unwrap();
        tree.sync();
        let deleted_dir_events: Vec<ScanEvent> = tree.scan().map(redact_event).collect();
        assert_yaml_snapshot!(deleted_dir_events);
    }

    #[cfg(unix)]
    fn make_writable(perms: &mut std::fs::Permissions) {
        use std::os::unix::fs::PermissionsExt;
        let mode = perms.mode();
        perms.set_mode(mode | 0o200);
    }

    #[cfg(not(unix))]
    fn make_writable(perms: &mut std::fs::Permissions) {
        perms.set_readonly(false);
    }

    #[test]
    fn test_gitignore_filtering() {
        let temp_dir = tempdir().expect("Failed to create temporary directory");
        let root = temp_dir.path();

        // Create a .gitignore that ignores *.log files and the "build/" directory
        let mut gitignore = File::create(root.join(".gitignore")).unwrap();
        writeln!(gitignore, "*.log").unwrap();
        writeln!(gitignore, "build/").unwrap();
        drop(gitignore);

        // Create files: one that should be found, ones that should be ignored
        fs::create_dir(root.join("src")).unwrap();
        fs::write(root.join("src/main.rs"), b"fn main() {}").unwrap();
        fs::write(root.join("debug.log"), b"some log").unwrap();
        fs::create_dir(root.join("build")).unwrap();
        fs::write(root.join("build/output.rs"), b"generated").unwrap();

        let mut tree = SourceHierTree::from(root);
        tree.sync();
        let events: Vec<ScanEvent> = tree.scan().map(redact_event).collect();

        // Only main.rs should appear as a NewFile event
        assert!(events
            .iter()
            .any(|e| matches!(e, ScanEvent::NewFile(p, _) if p == Path::new("main.rs"))));
        assert!(!events
            .iter()
            .any(|e| matches!(e, ScanEvent::NewFile(p, _) if p == Path::new("debug.log"))));
        assert!(!events
            .iter()
            .any(|e| matches!(e, ScanEvent::NewFile(p, _) if p == Path::new("output.rs"))));

        // Verify stats don't count ignored files
        let stats = tree.stats();
        assert_eq!(stats.files, 1);
    }
}