Skip to content

Commit

Permalink
🐛 fix ls with link cases
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <kweizh@gmail.com>
  • Loading branch information
zwpaper committed Aug 14, 2024
1 parent f550c07 commit 03c8dea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
17 changes: 2 additions & 15 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ fn inner_display_grid(
// Maybe skip showing the directory meta now; show its contents later.
if skip_dirs
&& (matches!(meta.file_type, FileType::Directory { .. })
|| (matches!(meta.file_type, FileType::SymLink { is_dir: true })))
|| (matches!(meta.file_type, FileType::SymLink { is_dir: true }))
&& flags.blocks.0.len() == 1)
{
continue;
}
Expand Down Expand Up @@ -961,24 +962,10 @@ mod tests {
std::os::unix::fs::symlink("dir", &link_path).unwrap();
let link = Meta::from_path(&link_path, false, PermissionFlag::Rwx).unwrap();

let grid_flags = Flags {
layout: Layout::Grid,
..Flags::default()
};

let oneline_flags = Flags {
layout: Layout::OneLine,
..Flags::default()
};

const YES: bool = true;
const NO: bool = false;

assert_eq!(should_display_folder_path(0, &[link.clone()]), NO);
assert_eq!(
should_display_folder_path(0, &[link.clone()]),
YES // doesn't matter since this link will be expanded as a directory
);

assert_eq!(
should_display_folder_path(0, &[file.clone(), link.clone()]),
Expand Down
2 changes: 1 addition & 1 deletion src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Meta {
match self.file_type {
FileType::Directory { .. } => (),
FileType::SymLink { is_dir: true } => {
if flags.layout == Layout::OneLine && depth != 1 {
if flags.blocks.0.len() > 1 {
return Ok((None, ExitCode::OK));
}
}
Expand Down
36 changes: 32 additions & 4 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,42 @@ fn test_list_broken_link_ok() {
.assert()
.stderr(predicate::str::contains(matched).not());
}

// ls link
// should show dir content
#[cfg(unix)]
#[test]
fn test_nosymlink_on_non_long() {
let dir = tempdir();
dir.child("target").touch().unwrap();
dir.child("target").child("inside").touch().unwrap();
let link = dir.path().join("link");
let link_icon = "⇒";
fs::symlink("target", &link).unwrap();

cmd()
.arg("-l")
.arg("--ignore-config")
.arg(&link)
.assert()
.stdout(predicate::str::contains(link_icon));
.stdout(predicate::str::contains(link_icon).not());
}

// ls -l link
// should show the link itself
#[cfg(unix)]
#[test]
fn test_symlink_on_long() {
let dir = tempdir();
dir.child("target").child("inside").touch().unwrap();
let link = dir.path().join("link");
let link_icon = "⇒";
fs::symlink("target", &link).unwrap();

cmd()
.arg("-l")
.arg("--ignore-config")
.arg(&link)
.assert()
.stdout(predicate::str::contains(link_icon).not());
.stdout(predicate::str::contains(link_icon));
}

#[cfg(unix)]
Expand Down Expand Up @@ -339,6 +354,8 @@ fn test_show_folder_content_of_symlink() {
.stdout(predicate::str::starts_with("inside"));
}

/// ls -l link
/// should show the link itself
#[cfg(unix)]
#[test]
fn test_no_show_folder_content_of_symlink_for_long() {
Expand All @@ -354,6 +371,17 @@ fn test_no_show_folder_content_of_symlink_for_long() {
.assert()
.stdout(predicate::str::starts_with("lrw"))
.stdout(predicate::str::contains("⇒"));
}

/// ls -l link/
/// should show the dir content
#[cfg(unix)]
#[test]
fn test_show_folder_content_of_symlink_for_long_tail_slash() {
let dir = tempdir();
dir.child("target").child("inside").touch().unwrap();
let link = dir.path().join("link");
fs::symlink("target", &link).unwrap();

Check failure on line 384 in tests/integration.rs

View workflow job for this annotation

GitHub Actions / Style (macos-latest)

the borrowed expression implements the required traits

Check failure on line 384 in tests/integration.rs

View workflow job for this annotation

GitHub Actions / Style (ubuntu-latest)

the borrowed expression implements the required traits

cmd()
.arg("-l")
Expand Down

0 comments on commit 03c8dea

Please sign in to comment.