Skip to content
Myr

Use a struct-like variant for broken symlinks and test max/exact depth

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Address review feedback. Make DirEntryInner::BrokenSymlink a struct-like variant with named path and depth fields, so the recorded depth is clear at every use site. Add regression tests that a broken symlink is filtered by --max-depth and --exact-depth the same way a normal entry is, both through the real directory tree and through a followed symlink.

authored by hexbinoct · committed by hexbinoct · parent 4a89aff · Browse files · 2 files+86 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+86 added−8 removed

  • tests:+78 added−0 removed
  • other:+8 added−8 removed

2 files+86 added−8 removed

modified:src/dir_entry.rs+8 added-8 removed
11 unmodified lines
12enum DirEntryInner {13 Normal(ignore::DirEntry),14 // Broken symlinks are surfaced by the walker as errors that carry no depth,15 // so we record the depth (relative to the search root) at creation time.16 BrokenSymlink(PathBuf, usize),16 BrokenSymlink { path: PathBuf, depth: usize },17}1819#[derive(Debug)]20pub struct DirEntry {
13 unmodified lines
34 }3536 pub fn broken_symlink(path: PathBuf, depth: usize) -> Self {37 Self {38 inner: DirEntryInner::BrokenSymlink(path, depth),38 inner: DirEntryInner::BrokenSymlink { path, depth },39 metadata: OnceCell::new(),40 style: OnceCell::new(),41 }42 }4344 pub fn path(&self) -> &Path {45 match &self.inner {46 DirEntryInner::Normal(e) => e.path(),47 DirEntryInner::BrokenSymlink(pathbuf, _) => pathbuf.as_path(),47 DirEntryInner::BrokenSymlink { path, .. } => path.as_path(),48 }49 }5051 pub fn into_path(self) -> PathBuf {52 match self.inner {53 DirEntryInner::Normal(e) => e.into_path(),54 DirEntryInner::BrokenSymlink(p, _) => p,54 DirEntryInner::BrokenSymlink { path, .. } => path,55 }56 }5758 /// Returns the path as it should be presented to the user.
24 unmodified lines
8384 pub fn file_type(&self) -> Option<FileType> {85 match &self.inner {86 DirEntryInner::Normal(e) => e.file_type(),87 DirEntryInner::BrokenSymlink(..) => self.metadata().map(|m| m.file_type()),87 DirEntryInner::BrokenSymlink { .. } => self.metadata().map(|m| m.file_type()),88 }89 }9091 pub fn metadata(&self) -> Option<&Metadata> {92 self.metadata93 .get_or_init(|| match &self.inner {94 DirEntryInner::Normal(e) => e.metadata().ok(),95 DirEntryInner::BrokenSymlink(path, _) => path.symlink_metadata().ok(),95 DirEntryInner::BrokenSymlink { path, .. } => path.symlink_metadata().ok(),96 })97 .as_ref()98 }99100 pub fn depth(&self) -> Option<usize> {101 match &self.inner {102 DirEntryInner::Normal(e) => Some(e.depth()),103 DirEntryInner::BrokenSymlink(_, depth) => Some(*depth),103 DirEntryInner::BrokenSymlink { depth, .. } => Some(*depth),104 }105 }106107 pub fn style(&self, ls_colors: &LsColors) -> Option<&Style> {
37 unmodified lines
145146 fn file_name(&self) -> OsString {147 let name = match &self.inner {148 DirEntryInner::Normal(e) => e.file_name(),149 DirEntryInner::BrokenSymlink(path, _) => {149 DirEntryInner::BrokenSymlink { path, .. } => {150 // Path::file_name() only works if the last component is Normal,151 // but we want it for all component types, so we open code it.152 // Copied from LsColors::style_for_path_with_metadata().153 path.components()
38 unmodified lines
modified:tests/tests.rs+78 added
1283 unmodified lines
1284 "",1285 );1286}12871288/// Maximum depth with a broken symlink (#1017)1289///1290/// A broken symlink must be filtered by --max-depth like any other entry. The1291/// default environment also exposes it through the followed `symlink` directory1292/// (`symlink -> one/two`), so it is reachable at depth 2 as well as depth 3.1293#[test]1294fn test_max_depth_broken_symlink() {1295 let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1296 te.create_broken_symlink("one/two/broken_symlink")1297 .expect("Failed to create broken symlink.");12981299 // --max-depth 3 keeps both routes to the broken symlink.1300 te.assert_output(1301 &[1302 "--follow",1303 "--type",1304 "symlink",1305 "--max-depth",1306 "3",1307 "broken_symlink",1308 ],1309 "one/two/broken_symlink1310 symlink/broken_symlink",1311 );13121313 // A --max-depth below either route must exclude it.1314 te.assert_output(1315 &[1316 "--follow",1317 "--type",1318 "symlink",1319 "--max-depth",1320 "1",1321 "broken_symlink",1322 ],1323 "",1324 );1325}13261327/// Exact depth with a broken symlink (#1017)1328///1329/// A broken symlink must be kept only at its exact depth. It is reachable at1330/// depth 3 (`one/two/broken_symlink`) and, through the followed `symlink`1331/// directory, at depth 2 (`symlink/broken_symlink`).1332#[test]1333fn test_exact_depth_broken_symlink() {1334 let mut te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1335 te.create_broken_symlink("one/two/broken_symlink")1336 .expect("Failed to create broken symlink.");13371338 // Only the depth-3 route matches --exact-depth 3.1339 te.assert_output(1340 &[1341 "--follow",1342 "--type",1343 "symlink",1344 "--exact-depth",1345 "3",1346 "broken_symlink",1347 ],1348 "one/two/broken_symlink",1349 );13501351 // Only the depth-2 route (via the followed symlink) matches --exact-depth 2,1352 // which confirms the depth is computed relative to the search root.1353 te.assert_output(1354 &[1355 "--follow",1356 "--type",1357 "symlink",1358 "--exact-depth",1359 "2",1360 "broken_symlink",1361 ],1362 "symlink/broken_symlink",1363 );1364}13651366/// Exact depth (--exact-depth)1367#[test]1368fn test_exact_depth() {1369 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
1664 unmodified lines