Skip to content

Commit

Permalink
🔍 🔨 fix test for adding disable permission
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Sep 20, 2023
1 parent 50140d5 commit 177c910
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/config_file.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This module provides methods to handle the program's config files and
//! operations related to this.
use crate::flags::display::Display;
use crate::flags::icons::{IconOption, IconTheme};
use crate::flags::layout::Layout;
Expand All @@ -6,8 +8,6 @@ use crate::flags::size::SizeFlag;
use crate::flags::sorting::{DirGrouping, SortColumn};
use crate::flags::HyperlinkOption;
use crate::flags::{ColorOption, ThemeOption};
///! This module provides methods to handle the program's config files and operations related to
///! this.
use crate::print_error;

use std::path::{Path, PathBuf};
Expand Down
26 changes: 14 additions & 12 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::color::Colors;
use crate::display;
use crate::flags::{
ColorOption, Display, Flags, HyperlinkOption, Layout, Literal, SortOrder, PermissionFlag, ThemeOption,
ColorOption, Display, Flags, HyperlinkOption, Layout, Literal, PermissionFlag, SortOrder,
ThemeOption,
};
use crate::git::GitCache;
use crate::icon::Icons;
Expand Down Expand Up @@ -105,17 +106,18 @@ impl Core {
};

for path in paths {
let mut meta =
match Meta::from_path(&path,
self.flags.dereference.0,
self.flags.permission == PermissionFlag::Disable) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};
let mut meta = match Meta::from_path(
&path,
self.flags.dereference.0,
self.flags.permission == PermissionFlag::Disable,
) {
Ok(meta) => meta,
Err(err) => {
print_error!("{}: {}.", path.display(), err);
exit_code.set_if_greater(ExitCode::MajorIssue);
continue;
}
};

let cache = if self.flags.blocks.0.contains(&Block::GitStatus) {
Some(GitCache::new(&path))
Expand Down
22 changes: 11 additions & 11 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ mod tests {
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
dir.child("one.d/.hidden").touch().unwrap();
let mut metas = Meta::from_path(Path::new(dir.path()), false)
let mut metas = Meta::from_path(Path::new(dir.path()), false, false)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -716,7 +716,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false)
let metas = Meta::from_path(Path::new(dir.path()), false, false)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -757,7 +757,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("dir").create_dir_all().unwrap();
dir.child("dir/file").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false)
let metas = Meta::from_path(Path::new(dir.path()), false, false)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -797,7 +797,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("one.d").create_dir_all().unwrap();
dir.child("one.d/two").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false)
let metas = Meta::from_path(Path::new(dir.path()), false, false)
.unwrap()
.recurse_into(42, &flags, None)
.unwrap()
Expand Down Expand Up @@ -828,7 +828,7 @@ mod tests {
let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
dir.child("test").touch().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false)
let metas = Meta::from_path(Path::new(dir.path()), false, false)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -862,7 +862,7 @@ mod tests {

let dir = assert_fs::TempDir::new().unwrap();
dir.child("testdir").create_dir_all().unwrap();
let metas = Meta::from_path(Path::new(dir.path()), false)
let metas = Meta::from_path(Path::new(dir.path()), false, false)
.unwrap()
.recurse_into(1, &flags, None)
.unwrap()
Expand Down Expand Up @@ -892,11 +892,11 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false).unwrap();
let file = Meta::from_path(&file_path, false, false).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false).unwrap();
let dir = Meta::from_path(&dir_path, false, false).unwrap();

assert_eq!(
display_folder_path(&dir),
Expand Down Expand Up @@ -942,15 +942,15 @@ mod tests {

let file_path = tmp_dir.path().join("file");
std::fs::File::create(&file_path).expect("failed to create the file");
let file = Meta::from_path(&file_path, false).unwrap();
let file = Meta::from_path(&file_path, false, false).unwrap();

let dir_path = tmp_dir.path().join("dir");
std::fs::create_dir(&dir_path).expect("failed to create the dir");
let dir = Meta::from_path(&dir_path, false).unwrap();
let dir = Meta::from_path(&dir_path, false, false).unwrap();

let link_path = tmp_dir.path().join("link");
std::os::unix::fs::symlink("dir", &link_path).unwrap();
let link = Meta::from_path(&link_path, false).unwrap();
let link = Meta::from_path(&link_path, false, false).unwrap();

let grid_flags = Flags {
layout: Layout::Grid,
Expand Down
2 changes: 1 addition & 1 deletion src/flags/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ mod test_blocks {
fn test_from_cli_none() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert!(matches!(Blocks::from_cli(&cli), None));
assert!(Blocks::from_cli(&cli).is_none());
}

#[test]
Expand Down
7 changes: 2 additions & 5 deletions src/flags/ignore_globs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,11 @@ mod test {
fn test_from_cli_none() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert!(matches!(IgnoreGlobs::from_cli(&cli), None));
assert!(IgnoreGlobs::from_cli(&cli).is_none());
}

#[test]
fn test_from_config_none() {
assert!(matches!(
IgnoreGlobs::from_config(&Config::with_none()),
None
));
assert!(IgnoreGlobs::from_config(&Config::with_none()).is_none());
}
}
10 changes: 10 additions & 0 deletions src/flags/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ mod test {
assert_eq!(Some(PermissionFlag::Octal), PermissionFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_permissions_disable() {
let argv = ["lsd", "--permission", "disable"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(
Some(PermissionFlag::Disable),
PermissionFlag::from_cli(&cli)
);
}

#[test]
#[should_panic]
fn test_from_cli_unknown() {
Expand Down
2 changes: 1 addition & 1 deletion src/flags/recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod test {
fn test_depth_from_cli_empty() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert!(matches!(cli.depth, None));
assert!(cli.depth.is_none());
}

#[test]
Expand Down
22 changes: 11 additions & 11 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icons = Icons::new(true, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -97,7 +97,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icons = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -110,7 +110,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icons = Icons::new(false, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -122,7 +122,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icons = Icons::new(true, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
Expand All @@ -135,7 +135,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icon = Icons::new(true, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -148,7 +148,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -161,7 +161,7 @@ mod test {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icon = Icons::new(
false,
Expand All @@ -178,7 +178,7 @@ mod test {
fn get_icon_default_directory() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false).unwrap();
let meta = Meta::from_path(file_path, false, false).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -190,7 +190,7 @@ mod test {
fn get_icon_default_directory_unicode() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false).unwrap();
let meta = Meta::from_path(file_path, false, false).unwrap();

let icon = Icons::new(
false,
Expand All @@ -210,7 +210,7 @@ mod test {
for (file_name, file_icon) in &IconTheme::get_default_icons_by_name() {
let file_path = tmp_dir.path().join(file_name);
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand All @@ -226,7 +226,7 @@ mod test {
for (ext, file_icon) in &IconTheme::get_default_icons_by_extension() {
let file_path = tmp_dir.path().join(format!("file.{ext}"));
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
Expand Down
2 changes: 1 addition & 1 deletion src/meta/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod test {
fn test_dir_type() {
let tmp_dir = tempdir().expect("failed to create temp dir");
#[cfg(not(windows))]
let meta = crate::meta::Meta::from_path(tmp_dir.path(), false)
let meta = crate::meta::Meta::from_path(tmp_dir.path(), false, false)
.expect("failed to get tempdir path");
let metadata = tmp_dir.path().metadata().expect("failed to get metas");

Expand Down
22 changes: 17 additions & 5 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl Meta {
// path.symlink_metadata would have errored out
if dereference {
broken_link = true;
eprintln!("lsd: {}: {}\n", path.to_str().unwrap_or(""), e);
eprintln!("lsd: {}: {}", path.to_str().unwrap_or(""), e);
}
}
}
Expand All @@ -292,7 +292,11 @@ impl Meta {
match windows_utils::get_file_data(path) {
Ok((owner, permissions)) => (Some(owner), Some(permissions)),
Err(e) => {
eprintln!("lsd: {}: {}\n", path.to_str().unwrap_or(""), e);
eprintln!(
"lsd: {}: {}(Hint: Consider using `--permission disabled`.)",
path.to_str().unwrap_or(""),
e
);
(None, None)
}
}
Expand Down Expand Up @@ -352,17 +356,25 @@ mod tests {
#[test]
fn test_from_path_path() {
let dir = assert_fs::TempDir::new().unwrap();
let meta = Meta::from_path(dir.path(), false).unwrap();
let meta = Meta::from_path(dir.path(), false, false).unwrap();
assert_eq!(meta.path, dir.path())
}

#[test]
fn test_from_path_disable_permission() {
let dir = assert_fs::TempDir::new().unwrap();
let meta = Meta::from_path(dir.path(), false, true).unwrap();
assert!(meta.permissions.is_none());
assert!(meta.owner.is_none());
}

#[test]
fn test_from_path() {
let tmp_dir = tempdir().expect("failed to create temp dir");

let path_a = tmp_dir.path().join("aaa.aa");
File::create(&path_a).expect("failed to create file");
let meta_a = Meta::from_path(&path_a, false).expect("failed to get meta");
let meta_a = Meta::from_path(&path_a, false, false).expect("failed to get meta");

let path_b = tmp_dir.path().join("bbb.bb");
let path_c = tmp_dir.path().join("ccc.cc");
Expand All @@ -377,7 +389,7 @@ mod tests {
std::os::windows::fs::symlink_file(&path_c, &path_b)
.expect("failed to create broken symlink");

let meta_b = Meta::from_path(&path_b, true).expect("failed to get meta");
let meta_b = Meta::from_path(&path_b, true, false).expect("failed to get meta");

assert!(
meta_a.inode.is_some()
Expand Down
6 changes: 3 additions & 3 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ mod test {
// Create the directory
let dir_path = tmp_dir.path().join("directory");
fs::create_dir(&dir_path).expect("failed to create the dir");
let meta = Meta::from_path(&dir_path, false).unwrap();
let meta = Meta::from_path(&dir_path, false, false).unwrap();

let colors = Colors::new(color::ThemeOption::NoLscolors);

Expand Down Expand Up @@ -398,7 +398,7 @@ mod test {
// Create the file;
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let colors = Colors::new(color::ThemeOption::NoColor);

Expand All @@ -424,7 +424,7 @@ mod test {
// Create the file;
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();
let meta = Meta::from_path(&file_path, false, false).unwrap();

let colors = Colors::new(color::ThemeOption::NoColor);

Expand Down
Loading

0 comments on commit 177c910

Please sign in to comment.