Merge pull request #1820 from tertsdiepraam/master

ls: move from getopts to clap and structure configuration
This commit is contained in:
Sylvestre Ledru 2021-03-19 09:55:27 +01:00 committed by GitHub
commit e521b46c09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 694 additions and 287 deletions

View file

@ -15,7 +15,7 @@ edition = "2018"
path = "src/ls.rs"
[dependencies]
getopts = "0.2.18"
clap = "2.33"
lazy_static = "1.0.1"
number_prefix = "0.4"
term_grid = "0.1.5"

File diff suppressed because it is too large Load diff

View file

@ -57,6 +57,36 @@ fn test_ls_a() {
assert!(!result.stdout.contains(".."));
}
#[test]
fn test_ls_columns() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-columns-1"));
at.touch(&at.plus_as_string("test-columns-2"));
// Columns is the default
let result = scene.ucmd().run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
assert!(result.success);
#[cfg(not(windows))]
assert_eq!(result.stdout, "test-columns-1\ntest-columns-2\n");
#[cfg(windows)]
assert_eq!(result.stdout, "test-columns-1 test-columns-2\n");
for option in &["-C", "--format=columns"] {
let result = scene.ucmd().arg(option).run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
assert!(result.success);
#[cfg(not(windows))]
assert_eq!(result.stdout, "test-columns-1\ntest-columns-2\n");
#[cfg(windows)]
assert_eq!(result.stdout, "test-columns-1 test-columns-2\n");
}
}
#[test]
fn test_ls_long() {
#[cfg(not(windows))]
@ -71,9 +101,12 @@ fn test_ls_long() {
}
}
let (at, mut ucmd) = at_and_ucmd!();
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-long"));
let result = ucmd.arg("-l").arg("test-long").succeeds();
for arg in &["-l", "--long", "--format=long", "--format=verbose"] {
let result = scene.ucmd().arg(arg).arg("test-long").succeeds();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
#[cfg(not(windows))]
@ -81,6 +114,7 @@ fn test_ls_long() {
#[cfg(windows)]
assert!(result.stdout.contains("---------- 1 somebody somegroup"));
}
#[cfg(not(windows))]
{
@ -90,6 +124,24 @@ fn test_ls_long() {
}
}
#[test]
fn test_ls_oneline() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-oneline-1"));
at.touch(&at.plus_as_string("test-oneline-2"));
// Bit of a weird situation: in the tests oneline and columns have the same output,
// except on Windows.
for option in &["-1", "--format=single-column"] {
let result = scene.ucmd().arg(option).run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
assert!(result.success);
assert_eq!(result.stdout, "test-oneline-1\ntest-oneline-2\n");
}
}
#[test]
fn test_ls_deref() {
let scene = TestScenario::new(util_name!());
@ -166,27 +218,54 @@ fn test_ls_order_size() {
}
#[test]
fn test_ls_order_creation() {
fn test_ls_long_ctime() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("test-long-ctime-1");
let result = scene.ucmd().arg("-lc").succeeds();
// Should show the time on Unix, but question marks on windows.
#[cfg(unix)]
assert!(result.stdout.contains(":"));
#[cfg(not(unix))]
assert!(result.stdout.contains("???"));
}
#[test]
fn test_ls_order_time() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("test-1");
at.append("test-1", "1");
sleep(Duration::from_millis(500));
sleep(Duration::from_millis(100));
at.touch("test-2");
at.append("test-2", "22");
sleep(Duration::from_millis(500));
sleep(Duration::from_millis(100));
at.touch("test-3");
at.append("test-3", "333");
sleep(Duration::from_millis(500));
sleep(Duration::from_millis(100));
at.touch("test-4");
at.append("test-4", "4444");
sleep(Duration::from_millis(100));
// Read test-3, only changing access time
at.read("test-3");
// Set permissions of test-2, only changing ctime
std::fs::set_permissions(
at.plus_as_string("test-2"),
at.metadata("test-2").permissions(),
)
.unwrap();
let result = scene.ucmd().arg("-al").run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
assert!(result.success);
// ctime was changed at write, so the order is 4 3 2 1
let result = scene.ucmd().arg("-t").run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
@ -196,7 +275,7 @@ fn test_ls_order_creation() {
#[cfg(windows)]
assert_eq!(result.stdout, "test-4 test-3 test-2 test-1\n");
let result = scene.ucmd().arg("-t").arg("-r").run();
let result = scene.ucmd().arg("-tr").run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
assert!(result.success);
@ -204,6 +283,31 @@ fn test_ls_order_creation() {
assert_eq!(result.stdout, "test-1\ntest-2\ntest-3\ntest-4\n");
#[cfg(windows)]
assert_eq!(result.stdout, "test-1 test-2 test-3 test-4\n");
// 3 was accessed last in the read
// So the order should be 2 3 4 1
let result = scene.ucmd().arg("-tu").run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
assert!(result.success);
#[cfg(not(windows))]
assert_eq!(result.stdout, "test-3\ntest-4\ntest-2\ntest-1\n");
// Access time does not seem to be set on Windows on read call
// so the order is 4 3 2 1
#[cfg(windows)]
assert_eq!(result.stdout, "test-4 test-3 test-2 test-1\n");
// test-2 had the last ctime change when the permissions were set
// So the order should be 2 4 3 1
#[cfg(unix)]
{
let result = scene.ucmd().arg("-tc").run();
println!("stderr = {:?}", result.stderr);
println!("stdout = {:?}", result.stdout);
assert!(result.success);
assert_eq!(result.stdout, "test-2\ntest-4\ntest-3\ntest-1\n");
}
}
#[test]
@ -270,19 +374,56 @@ fn test_ls_recursive() {
assert!(result.stdout.contains("a\\b:\nb"));
}
#[cfg(unix)]
#[test]
fn test_ls_ls_color() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.mkdir("a");
at.mkdir("a/nested_dir");
at.mkdir("z");
at.touch(&at.plus_as_string("a/a"));
scene.ucmd().arg("--color").succeeds();
scene.ucmd().arg("--color=always").succeeds();
scene.ucmd().arg("--color=never").succeeds();
scene.ucmd().arg("--color").arg("a").succeeds();
scene.ucmd().arg("--color=always").arg("a/a").succeeds();
scene.ucmd().arg("--color=never").arg("z").succeeds();
at.touch(&at.plus_as_string("a/nested_file"));
at.touch("test-color");
let a_with_colors = "\x1b[01;34ma\x1b[0m";
let z_with_colors = "\x1b[01;34mz\x1b[0m";
let nested_dir_with_colors = "\x1b[01;34mnested_dir\x1b[0m";
// Color is disabled by default
let result = scene.ucmd().succeeds();
assert!(!result.stdout.contains(a_with_colors));
assert!(!result.stdout.contains(z_with_colors));
// Color should be enabled
let result = scene.ucmd().arg("--color").succeeds();
assert!(result.stdout.contains(a_with_colors));
assert!(result.stdout.contains(z_with_colors));
// Color should be enabled
let result = scene.ucmd().arg("--color=always").succeeds();
assert!(result.stdout.contains(a_with_colors));
assert!(result.stdout.contains(z_with_colors));
// Color should be disabled
let result = scene.ucmd().arg("--color=never").succeeds();
assert!(!result.stdout.contains(a_with_colors));
assert!(!result.stdout.contains(z_with_colors));
// Nested dir should be shown and colored
let result = scene.ucmd().arg("--color").arg("a").succeeds();
assert!(result.stdout.contains(nested_dir_with_colors));
// Color has no effect
let result = scene
.ucmd()
.arg("--color=always")
.arg("a/nested_file")
.succeeds();
assert!(result.stdout.contains("a/nested_file\n"));
// No output
let result = scene.ucmd().arg("--color=never").arg("z").succeeds();
assert_eq!(result.stdout, "");
}
#[cfg(not(any(target_vendor = "apple", target_os = "windows")))] // Truncate not available on mac or win