mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
ls: fix subdirectory name
This commit is contained in:
parent
0edbbe914c
commit
28c7800f73
2 changed files with 70 additions and 18 deletions
|
@ -1122,14 +1122,21 @@ impl PathData {
|
|||
fn new(
|
||||
p_buf: PathBuf,
|
||||
file_type: Option<std::io::Result<FileType>>,
|
||||
file_name: Option<String>,
|
||||
config: &Config,
|
||||
command_line: bool,
|
||||
) -> Self {
|
||||
let name = p_buf
|
||||
.file_name()
|
||||
.unwrap_or_else(|| p_buf.iter().next_back().unwrap())
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
// We cannot use `Path::ends_with` or `Path::Components`, because they remove occurrences of '.'
|
||||
// For '..', the filename is None
|
||||
let name = if let Some(name) = file_name {
|
||||
name
|
||||
} else {
|
||||
p_buf
|
||||
.file_name()
|
||||
.unwrap_or_else(|| p_buf.iter().next_back().unwrap())
|
||||
.to_string_lossy()
|
||||
.into_owned()
|
||||
};
|
||||
let must_dereference = match &config.dereference {
|
||||
Dereference::All => true,
|
||||
Dereference::Args => command_line,
|
||||
|
@ -1192,7 +1199,7 @@ fn list(locs: Vec<String>, config: Config) -> i32 {
|
|||
continue;
|
||||
}
|
||||
|
||||
let path_data = PathData::new(p, None, &config, true);
|
||||
let path_data = PathData::new(p, None, None, &config, true);
|
||||
|
||||
let show_dir_contents = if let Some(ft) = path_data.file_type() {
|
||||
!config.directory && ft.is_dir()
|
||||
|
@ -1283,8 +1290,8 @@ fn should_display(entry: &DirEntry, config: &Config) -> bool {
|
|||
fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter<Stdout>) {
|
||||
let mut entries: Vec<_> = if config.files == Files::All {
|
||||
vec![
|
||||
PathData::new(dir.p_buf.join("."), None, config, false),
|
||||
PathData::new(dir.p_buf.join(".."), None, config, false),
|
||||
PathData::new(dir.p_buf.join("."), None, Some(".".into()), config, false),
|
||||
PathData::new(dir.p_buf.join(".."), None, Some("..".into()), config, false),
|
||||
]
|
||||
} else {
|
||||
vec![]
|
||||
|
@ -1293,7 +1300,7 @@ fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter<Stdout>)
|
|||
let mut temp: Vec<_> = safe_unwrap!(fs::read_dir(&dir.p_buf))
|
||||
.map(|res| safe_unwrap!(res))
|
||||
.filter(|e| should_display(e, config))
|
||||
.map(|e| PathData::new(DirEntry::path(&e), Some(e.file_type()), config, false))
|
||||
.map(|e| PathData::new(DirEntry::path(&e), Some(e.file_type()), None, config, false))
|
||||
.collect();
|
||||
|
||||
sort_entries(&mut temp, config);
|
||||
|
|
|
@ -43,23 +43,68 @@ fn test_ls_a() {
|
|||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
at.touch(".test-1");
|
||||
at.mkdir("some-dir");
|
||||
at.touch(
|
||||
Path::new("some-dir")
|
||||
.join(".test-2")
|
||||
.as_os_str()
|
||||
.to_str()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let result = scene.ucmd().succeeds();
|
||||
let stdout = result.stdout_str();
|
||||
assert!(!stdout.contains(".test-1"));
|
||||
assert!(!stdout.contains(".."));
|
||||
let re_pwd = Regex::new(r"^\.\n").unwrap();
|
||||
|
||||
// Using the present working directory
|
||||
scene
|
||||
.ucmd()
|
||||
.succeeds()
|
||||
.stdout_does_not_contain(".test-1")
|
||||
.stdout_does_not_contain("..")
|
||||
.stdout_does_not_match(&re_pwd);
|
||||
|
||||
scene
|
||||
.ucmd()
|
||||
.arg("-a")
|
||||
.succeeds()
|
||||
.stdout_contains(&".test-1")
|
||||
.stdout_contains(&"..");
|
||||
.stdout_contains(&"..")
|
||||
.stdout_matches(&re_pwd);
|
||||
|
||||
let result = scene.ucmd().arg("-A").succeeds();
|
||||
result.stdout_contains(".test-1");
|
||||
let stdout = result.stdout_str();
|
||||
assert!(!stdout.contains(".."));
|
||||
scene
|
||||
.ucmd()
|
||||
.arg("-A")
|
||||
.succeeds()
|
||||
.stdout_contains(".test-1")
|
||||
.stdout_does_not_contain("..")
|
||||
.stdout_does_not_match(&re_pwd);
|
||||
|
||||
// Using a subdirectory
|
||||
scene
|
||||
.ucmd()
|
||||
.arg("some-dir")
|
||||
.succeeds()
|
||||
.stdout_does_not_contain(".test-2")
|
||||
.stdout_does_not_contain("..")
|
||||
.stdout_does_not_match(&re_pwd);
|
||||
|
||||
scene
|
||||
.ucmd()
|
||||
.arg("-a")
|
||||
.arg("some-dir")
|
||||
.succeeds()
|
||||
.stdout_contains(&".test-2")
|
||||
.stdout_contains(&"..")
|
||||
.no_stderr()
|
||||
.stdout_matches(&re_pwd);
|
||||
|
||||
scene
|
||||
.ucmd()
|
||||
.arg("-A")
|
||||
.arg("some-dir")
|
||||
.succeeds()
|
||||
.stdout_contains(".test-2")
|
||||
.stdout_does_not_contain("..")
|
||||
.stdout_does_not_match(&re_pwd);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue