Skip to content

Commit f8462cb

Browse files
committed
fix: ensure SUBDIRED positions are printed for multiple directories without -R
This commit fixes an issue where `//SUBDIRED//` positions were not being printed when using `--dired` with multiple directory arguments but without the `-R` (recursive) flag. The SUBDIRED positions are needed whenever directory headings are printed, which occurs with multiple arguments regardless of whether recursion is enabled.
1 parent cd1711a commit f8462cb

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/uu/ls/src/dired.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub fn print_dired_output(
9898
if !dired.dired_positions.is_empty() {
9999
print_positions("//DIRED//", &dired.dired_positions);
100100
}
101+
// SUBDIRED is needed whenever directory headings are printed (multiple args or -R),
102+
// so don't gate it on config.recursive.
101103
if !dired.subdired_positions.is_empty() {
102104
print_positions("//SUBDIRED//", &dired.subdired_positions);
103105
}

tests/by-util/test_ls.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5205,6 +5205,51 @@ fn test_ls_dired_recursive() {
52055205
.stdout_contains("//DIRED-OPTIONS// --quoting-style");
52065206
}
52075207

5208+
#[test]
5209+
fn test_ls_dired_subdired_multiple_dirs_non_recursive() {
5210+
let scene = TestScenario::new(util_name!());
5211+
let at = &scene.fixtures;
5212+
5213+
at.mkdir("dir1");
5214+
at.mkdir("dir2");
5215+
at.touch("dir1/a");
5216+
at.touch("dir2/b");
5217+
5218+
let result = scene
5219+
.ucmd()
5220+
.arg("--dired")
5221+
.arg("dir1")
5222+
.arg("dir2")
5223+
.succeeds();
5224+
5225+
let output = result.stdout_str().to_string();
5226+
let subdired_line = output
5227+
.lines()
5228+
.find(|&line| line.starts_with("//SUBDIRED//"))
5229+
.unwrap();
5230+
let positions: Vec<usize> = subdired_line
5231+
.split_whitespace()
5232+
.skip(1)
5233+
.map(|s| s.parse().unwrap())
5234+
.collect();
5235+
5236+
assert_eq!(positions.len() % 2, 0); // Ensure there's an even number of positions
5237+
5238+
let dirnames: Vec<String> = positions
5239+
.chunks(2)
5240+
.map(|chunk| {
5241+
let start_pos = chunk[0];
5242+
let end_pos = chunk[1];
5243+
String::from_utf8(output.as_bytes()[start_pos..end_pos].to_vec())
5244+
.unwrap()
5245+
.trim()
5246+
.to_string()
5247+
})
5248+
.collect();
5249+
5250+
assert_eq!(dirnames, vec!["dir1", "dir2"]);
5251+
}
5252+
52085253
#[test]
52095254
fn test_ls_dired_outputs_parent_offset() {
52105255
let scene = TestScenario::new(util_name!());

0 commit comments

Comments
 (0)