From f971a69d69c57eed1fe31807cd069cfbdc923463 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 19 Oct 2023 14:17:34 +0200 Subject: ls --dired -R: fix the positions (#5341) * move get_offset_from_previous_line into a specific function * dired: improve the -R support * dired: fix the display with subdir * ls --dired -R: fix the positions * ls --dired -R: verify also the SUBDIRED coordinate * ls --dired -R: add a long file name and fix a windows test * dired: always put dired first in the args + minor fixes Co-authored-by: Daniel Hofstetter * ls: add cognitive_complexity to silent a warning --------- Co-authored-by: Daniel Hofstetter --- tests/by-util/test_ls.rs | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) (limited to 'tests/by-util/test_ls.rs') diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 8b0032065..15893b0e2 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -3580,6 +3580,57 @@ fn test_ls_dired_recursive() { .stdout_contains("//DIRED-OPTIONS// --quoting-style"); } +#[test] +fn test_ls_dired_recursive_multiple() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir("d"); + at.mkdir("d/d1"); + at.mkdir("d/d2"); + at.touch("d/d2/a"); + at.touch("d/d2/c2"); + at.touch("d/d1/f1"); + at.touch("d/d1/file-long"); + + let mut cmd = scene.ucmd(); + cmd.arg("--dired").arg("-l").arg("-R").arg("d"); + + let result = cmd.succeeds(); + + let output = result.stdout_str().to_string(); + println!("Output:\n{}", output); + + let dired_line = output + .lines() + .find(|&line| line.starts_with("//DIRED//")) + .unwrap(); + let positions: Vec = dired_line + .split_whitespace() + .skip(1) + .map(|s| s.parse().unwrap()) + .collect(); + println!("Parsed byte positions: {:?}", positions); + assert_eq!(positions.len() % 2, 0); // Ensure there's an even number of positions + + let filenames: Vec = positions + .chunks(2) + .map(|chunk| { + let start_pos = chunk[0]; + let end_pos = chunk[1]; + let filename = String::from_utf8(output.as_bytes()[start_pos..=end_pos].to_vec()) + .unwrap() + .trim() + .to_string(); + println!("Extracted filename: {}", filename); + filename + }) + .collect(); + + println!("Extracted filenames: {:?}", filenames); + assert_eq!(filenames, vec!["d1", "d2", "f1", "file-long", "a", "c2"]); +} + #[test] fn test_ls_dired_simple() { let scene = TestScenario::new(util_name!()); @@ -3679,6 +3730,57 @@ fn test_ls_dired_complex() { assert_eq!(filenames, vec!["a1", "a22", "a333", "a4444", "d"]); } +#[test] +fn test_ls_subdired_complex() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.mkdir("dir1"); + at.mkdir("dir1/d"); + at.mkdir("dir1/c2"); + at.touch("dir1/a1"); + at.touch("dir1/a22"); + at.touch("dir1/a333"); + at.touch("dir1/c2/a4444"); + + let mut cmd = scene.ucmd(); + cmd.arg("--dired").arg("-l").arg("-R").arg("dir1"); + let result = cmd.succeeds(); + + let output = result.stdout_str().to_string(); + println!("Output:\n{}", output); + + let dired_line = output + .lines() + .find(|&line| line.starts_with("//SUBDIRED//")) + .unwrap(); + let positions: Vec = dired_line + .split_whitespace() + .skip(1) + .map(|s| s.parse().unwrap()) + .collect(); + println!("Parsed byte positions: {:?}", positions); + assert_eq!(positions.len() % 2, 0); // Ensure there's an even number of positions + + let dirnames: Vec = positions + .chunks(2) + .map(|chunk| { + let start_pos = chunk[0]; + let end_pos = chunk[1]; + let dirname = + String::from_utf8(output.as_bytes()[start_pos..end_pos].to_vec()).unwrap(); + println!("Extracted dirname: {}", dirname); + dirname + }) + .collect(); + + println!("Extracted dirnames: {:?}", dirnames); + #[cfg(unix)] + assert_eq!(dirnames, vec!["dir1", "dir1/c2", "dir1/d"]); + #[cfg(windows)] + assert_eq!(dirnames, vec!["dir1", "dir1\\c2", "dir1\\d"]); +} + #[ignore = "issue #5396"] #[test] fn test_ls_cf_output_should_be_delimited_by_tab() { -- cgit v1.2.3