mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57: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(
|
fn new(
|
||||||
p_buf: PathBuf,
|
p_buf: PathBuf,
|
||||||
file_type: Option<std::io::Result<FileType>>,
|
file_type: Option<std::io::Result<FileType>>,
|
||||||
|
file_name: Option<String>,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
command_line: bool,
|
command_line: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let name = p_buf
|
// We cannot use `Path::ends_with` or `Path::Components`, because they remove occurrences of '.'
|
||||||
.file_name()
|
// For '..', the filename is None
|
||||||
.unwrap_or_else(|| p_buf.iter().next_back().unwrap())
|
let name = if let Some(name) = file_name {
|
||||||
.to_string_lossy()
|
name
|
||||||
.into_owned();
|
} 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 {
|
let must_dereference = match &config.dereference {
|
||||||
Dereference::All => true,
|
Dereference::All => true,
|
||||||
Dereference::Args => command_line,
|
Dereference::Args => command_line,
|
||||||
|
@ -1192,7 +1199,7 @@ fn list(locs: Vec<String>, config: Config) -> i32 {
|
||||||
continue;
|
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() {
|
let show_dir_contents = if let Some(ft) = path_data.file_type() {
|
||||||
!config.directory && ft.is_dir()
|
!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>) {
|
fn enter_directory(dir: &PathData, config: &Config, out: &mut BufWriter<Stdout>) {
|
||||||
let mut entries: Vec<_> = if config.files == Files::All {
|
let mut entries: Vec<_> = if config.files == Files::All {
|
||||||
vec![
|
vec![
|
||||||
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, config, false),
|
PathData::new(dir.p_buf.join(".."), None, Some("..".into()), config, false),
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
vec![]
|
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))
|
let mut temp: Vec<_> = safe_unwrap!(fs::read_dir(&dir.p_buf))
|
||||||
.map(|res| safe_unwrap!(res))
|
.map(|res| safe_unwrap!(res))
|
||||||
.filter(|e| should_display(e, config))
|
.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();
|
.collect();
|
||||||
|
|
||||||
sort_entries(&mut temp, config);
|
sort_entries(&mut temp, config);
|
||||||
|
|
|
@ -43,23 +43,68 @@ fn test_ls_a() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let scene = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &scene.fixtures;
|
||||||
at.touch(".test-1");
|
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 re_pwd = Regex::new(r"^\.\n").unwrap();
|
||||||
let stdout = result.stdout_str();
|
|
||||||
assert!(!stdout.contains(".test-1"));
|
// Using the present working directory
|
||||||
assert!(!stdout.contains(".."));
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.succeeds()
|
||||||
|
.stdout_does_not_contain(".test-1")
|
||||||
|
.stdout_does_not_contain("..")
|
||||||
|
.stdout_does_not_match(&re_pwd);
|
||||||
|
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("-a")
|
.arg("-a")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_contains(&".test-1")
|
.stdout_contains(&".test-1")
|
||||||
.stdout_contains(&"..");
|
.stdout_contains(&"..")
|
||||||
|
.stdout_matches(&re_pwd);
|
||||||
|
|
||||||
let result = scene.ucmd().arg("-A").succeeds();
|
scene
|
||||||
result.stdout_contains(".test-1");
|
.ucmd()
|
||||||
let stdout = result.stdout_str();
|
.arg("-A")
|
||||||
assert!(!stdout.contains(".."));
|
.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]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue