From 35462e9fbfa37a410abacbc8dcf776e1d907396d Mon Sep 17 00:00:00 2001 From: Nicolas Antinori Date: Fri, 25 Oct 2024 19:51:52 -0300 Subject: [PATCH 1/3] added stage4 tests --- tests/stage4.rs | 38 ++++++++++++++++++++++++++++++++++++++ tests/stage5.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tests/stage4.rs create mode 100644 tests/stage5.rs diff --git a/tests/stage4.rs b/tests/stage4.rs new file mode 100644 index 0000000..8a594ca --- /dev/null +++ b/tests/stage4.rs @@ -0,0 +1,38 @@ +#![cfg(feature = "stage4")] + +mod helpers; + +use helpers::{split_ls_column_output, split_ls_output_by_newline}; +use rstest::rstest; +use std::process::Command; + +const EXPECTED_HELP_TEXT: &str = r#"A tiny version of ls command + +Usage: oxidar-ls [OPTIONS] + +Arguments: + Path to list + +Options: + -a, --all Show hidden files + -F, --format Format file names. Appendds a symbol at the end indicating the type of file. Slash ('/') is for directories and at sign ('@') is for symbolic links + -l, --list Show the files in a list + -h, --human Show the files in a list + --help +"#; + +#[rstest] +#[case::arguments_help(&["--help"])] +fn test_stage_4(#[case] ls_arguments: &[&'static str]) { + let output = Command::new("./target/debug/oxidar-ls") + .args([ls_arguments, &["./test_dir"]].concat()) + .output() + .expect("failed to execute process"); + + assert!(output.status.success()); + + assert_eq!( + EXPECTED_HELP_TEXT, + std::str::from_utf8(&output.stdout).unwrap() + ); +} diff --git a/tests/stage5.rs b/tests/stage5.rs new file mode 100644 index 0000000..a017b71 --- /dev/null +++ b/tests/stage5.rs @@ -0,0 +1,49 @@ +#![cfg(feature = "stage5")] + +mod helpers; + +use helpers::{split_ls_column_output, split_ls_output_by_newline}; +use rstest::rstest; +use std::process::Command; + +#[rstest] +#[case::arguments_l(&["-l"])] +#[case::arguments_lF(&["-l", "-F"])] +#[case::arguments_lFh(&["-l", "-F", "-h"])] +#[case::arguments_lFha(&["-l", "-F", "-h", "-a"])] +#[case::arguments_lFha(&["-lFha"])] // This tests tha clap was introduced and works as expected +fn test_stage_5(#[case] ls_arguments: &[&'static str]) { + let output = Command::new("./target/debug/oxidar-ls") + .args([ls_arguments, &["./test_dir"]].concat()) + .output() + .expect("failed to execute process"); + + let expected = Command::new("/bin/ls") + .args([ls_arguments, &["./test_dir"]].concat()) + .output() + .expect("failed to execute process"); + + assert!(output.status.success()); + + let expected_lines = split_ls_output_by_newline(&expected.stdout); + let output_lines = split_ls_output_by_newline(&output.stdout); + + // We use expected_lines[1..] because the first element in the /bin/ls output is the total + // column + assert_eq!(expected_lines[1..].len(), output_lines.len()); + + for (expected, output) in expected_lines[1..].iter().zip(output_lines) { + let expected = split_ls_column_output(*expected); + let output = split_ls_column_output(&*output); + // Assert permissions + assert_eq!(expected[0], output[0]); + + // We ignore the . and ../ directories on purpose because is not that easy to calculate + // their size + if output[2] != "." && output[2] != "./" && output[2] != ".." && output[2] != "../" { + assert_eq!(expected[4], output[1]); + } + // Assert name + assert_eq!(expected[8], output[2]); + } +} From 238f8a0b5ddc3a129de4df0d5a7e93077c2b4749 Mon Sep 17 00:00:00 2001 From: Nicolas Antinori Date: Sat, 26 Oct 2024 11:43:18 -0300 Subject: [PATCH 2/3] Upload Cargo.toml --- Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 434b508..e00309a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,12 +7,13 @@ authors = [ "Nicolas ", ] edition = "2021" +description = "A tiny version of ls command" [dependencies] anyhow = "1.0" bytes = "1.3" +clap = { version = "4.5.20", features = ["derive"] } thiserror = "1.0" -clap = "4.5.20" [dev-dependencies] rstest = "0.23.0" @@ -25,4 +26,5 @@ stage0 = [] stage1 = [] stage2 = [] stage3 = [] +stage4 = [] stage5 = [] From fd38b9c8cec2241958886d1f1cc0ed2fad6cc616 Mon Sep 17 00:00:00 2001 From: Nicolas Antinori Date: Mon, 28 Oct 2024 10:14:45 -0300 Subject: [PATCH 3/3] Fixed --human description --- Cargo.lock | 19 +++++++++++++++++++ tests/stage4.rs | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 6c6be30..3bddbb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -91,6 +91,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -105,6 +106,18 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "clap_lex" version = "0.7.2" @@ -230,6 +243,12 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "indexmap" version = "2.6.0" diff --git a/tests/stage4.rs b/tests/stage4.rs index 8a594ca..ea30c4c 100644 --- a/tests/stage4.rs +++ b/tests/stage4.rs @@ -17,7 +17,7 @@ Options: -a, --all Show hidden files -F, --format Format file names. Appendds a symbol at the end indicating the type of file. Slash ('/') is for directories and at sign ('@') is for symbolic links -l, --list Show the files in a list - -h, --human Show the files in a list + -h, --human Show the file size in a human-redable format --help "#;