1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

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 <daniel.hofstetter@42dh.com>

* ls: add cognitive_complexity to silent a warning

---------

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
This commit is contained in:
Sylvestre Ledru 2023-10-19 14:17:34 +02:00 committed by GitHub
parent 20cc68a29d
commit f971a69d69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 314 additions and 37 deletions

View file

@ -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<usize> = 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<String> = 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<usize> = 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<String> = 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() {