Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add disable option for permission #882

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
🔍 🔨 fix test for adding disable permission
Signed-off-by: Wei Zhang <kweizh@gmail.com>
  • Loading branch information
zwpaper committed Sep 19, 2023
commit 53e333d8bfb35a73b3c0a4a74342f6f60bd5fa0e
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;
@@ -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};
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;
@@ -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))
22 changes: 11 additions & 11 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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),
@@ -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,
2 changes: 1 addition & 1 deletion src/flags/blocks.rs
Original file line number Diff line number Diff line change
@@ -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]
7 changes: 2 additions & 5 deletions src/flags/ignore_globs.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -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() {
2 changes: 1 addition & 1 deletion src/flags/recursion.rs
Original file line number Diff line number Diff line change
@@ -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]
22 changes: 11 additions & 11 deletions src/icon.rs
Original file line number Diff line number Diff line change
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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,
@@ -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);
@@ -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,
@@ -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);
@@ -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);
2 changes: 1 addition & 1 deletion src/meta/filetype.rs
Original file line number Diff line number Diff line change
@@ -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");

22 changes: 17 additions & 5 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
@@ -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)
}
}
@@ -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");
@@ -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()
6 changes: 3 additions & 3 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
@@ -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);

@@ -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);

@@ -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);

Loading