Skip to content

Commit 531bcca

Browse files
committed
test(fetcher/code/scan): cover the tracked walkers' early-continue branches — for_each_tracked_path and for_each_tracked_file skipping a symlink that fails is_regular_file and (Linux-only) a non-UTF-8 path that fails the str::from_utf8 decode, asserting only the regular keep.rs entry is ever visited
1 parent 5dd4af4 commit 531bcca

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎src/fetcher/code/scan.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,40 @@ mod tests {
368368
.unwrap();
369369
}
370370

371+
/// Index entries that aren't regular files (symlinks) and paths whose bytes aren't valid
372+
/// UTF-8 take the early-`continue` branches in both walkers: the symlink fails
373+
/// `is_regular_file` (its index mode is `SYMLINK`, not `FILE`), and a non-UTF-8 path fails
374+
/// the `str::from_utf8` decode before any classification or read. The non-UTF-8 half is
375+
/// Linux-only — APFS / NTFS reject such filenames at the filesystem layer.
376+
#[test]
377+
#[cfg(unix)]
378+
fn tracked_walkers_skip_symlinks_and_non_utf8_paths() {
379+
use std::os::unix::fs::symlink;
380+
381+
let (_tmp, repo) = make_repo();
382+
let dir = repo.workdir().unwrap();
383+
write_file(dir, "keep.rs", "fn keep() {}\n");
384+
symlink("keep.rs", dir.join("link.rs")).unwrap();
385+
#[cfg(target_os = "linux")]
386+
{
387+
use std::os::unix::ffi::OsStrExt;
388+
let bad = dir.join(std::ffi::OsStr::from_bytes(b"bad\xffname.txt"));
389+
std::fs::write(&bad, "bytes\n").unwrap();
390+
}
391+
git(dir, &["add", "."]);
392+
git(dir, &["commit", "-q", "-m", "fixture"]);
393+
394+
// Only `keep.rs` survives: `link.rs` is skipped as a non-regular file and the
395+
// non-UTF-8 entry is skipped as an undecodable path — neither is ever visited.
396+
let mut path_hits = Vec::new();
397+
for_each_tracked_path(&repo, |path| path_hits.push(path.to_string())).unwrap();
398+
assert_eq!(path_hits, vec!["keep.rs".to_string()]);
399+
400+
let mut file_hits = Vec::new();
401+
for_each_tracked_file(&repo, |path, _| file_hits.push(path.to_string())).unwrap();
402+
assert_eq!(file_hits, vec!["keep.rs".to_string()]);
403+
}
404+
371405
#[test]
372406
fn tokei_stat_walker_reports_classified_files_only() {
373407
let (_tmp, repo) = make_repo();

0 commit comments

Comments
 (0)