From 491a77a529ebc4ce77e333f1a850548aa6093620 Mon Sep 17 00:00:00 2001 From: Rakuram Date: Thu, 2 Jan 2025 23:27:39 +0530 Subject: [PATCH] Update chapter 2 --- cli-book/echor/Cargo.toml | 6 +++ cli-book/echor/mk-outs.sh | 9 ++++ cli-book/echor/src/main.rs | 20 +++++-- cli-book/echor/tests/cli.rs | 62 ++++++++++++++++++++++ cli-book/echor/tests/expected/hello1.n.txt | 1 + cli-book/echor/tests/expected/hello1.txt | 1 + cli-book/echor/tests/expected/hello2.n.txt | 1 + cli-book/echor/tests/expected/hello2.txt | 1 + 8 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 cli-book/echor/mk-outs.sh create mode 100644 cli-book/echor/tests/cli.rs create mode 100644 cli-book/echor/tests/expected/hello1.n.txt create mode 100644 cli-book/echor/tests/expected/hello1.txt create mode 100644 cli-book/echor/tests/expected/hello2.n.txt create mode 100644 cli-book/echor/tests/expected/hello2.txt diff --git a/cli-book/echor/Cargo.toml b/cli-book/echor/Cargo.toml index 0e3c571..d11b986 100644 --- a/cli-book/echor/Cargo.toml +++ b/cli-book/echor/Cargo.toml @@ -5,3 +5,9 @@ edition = "2021" [dependencies] clap = "4.5.23" + +[dev-dependencies] +anyhow = "1.0.79" +assert_cmd = "2.0.13" +predicates = "3.0.4" +pretty_assertions = "1.4.0" \ No newline at end of file diff --git a/cli-book/echor/mk-outs.sh b/cli-book/echor/mk-outs.sh new file mode 100644 index 0000000..ea55598 --- /dev/null +++ b/cli-book/echor/mk-outs.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +OUTDIR="tests/expected" +[[ ! -d "$OUTDIR" ]] && mkdir -p "$OUTDIR" + +echo "Hello there" > $OUTDIR/hello1.txt +echo "Hello" "there" > $OUTDIR/hello2.txt +echo -n "Hello there" > $OUTDIR/hello1.n.txt +echo -n "Hello" "there" > $OUTDIR/hello2.n.txt \ No newline at end of file diff --git a/cli-book/echor/src/main.rs b/cli-book/echor/src/main.rs index 7e79d07..01a6612 100644 --- a/cli-book/echor/src/main.rs +++ b/cli-book/echor/src/main.rs @@ -26,7 +26,8 @@ fn main() { .value_name("TEXT") .help("Input Text") .required(true) - .num_args(1..), + .num_args(1..) + .action(ArgAction::Append), ) .arg( Arg::new("omit_newline") @@ -36,5 +37,18 @@ fn main() { ) .get_matches(); - println!("{:#?}", matches); -} + // println!("{:#?}", matches); + + let text: Vec = matches + .get_many("text") + .expect("text is required") + .cloned() + .collect(); + + // println!("{:#?}", text); + + let omit_newline = matches.get_flag("omit_newline"); + // println!("{:#?}", omit_newline); + + print!("{}{}", text.join(" "), if omit_newline { "" } else { "\n" }); +} \ No newline at end of file diff --git a/cli-book/echor/tests/cli.rs b/cli-book/echor/tests/cli.rs new file mode 100644 index 0000000..ec9450c --- /dev/null +++ b/cli-book/echor/tests/cli.rs @@ -0,0 +1,62 @@ +use assert_cmd::Command; +use predicates::prelude::*; +use std::fs; +use anyhow::Result; + +#[test] +fn dies_no_args() -> Result<()> { + let mut cmd = Command::cargo_bin("echor")?; + cmd.assert() + .failure() + .stderr(predicate::str::contains("Usage")); + Ok(()) +} + +#[test] +fn hello1() -> Result<()> { + let expected = fs::read_to_string("tests/expected/hello1.txt")?; + let mut cmd = Command::cargo_bin("echor")?; + cmd.arg("Hello there").assert().success().stdout(expected); + Ok(()) +} + +#[test] +fn hello2() -> Result<()> { + let expected = fs::read_to_string("tests/expected/hello2.txt")?; + let mut cmd = Command::cargo_bin("echor")?; + cmd.args(vec!["Hello", "there"]) + .assert() + .success() + .stdout(expected); + Ok(()) +} + +fn run(args: &[&str], expected_file: &str) -> Result<()> { + let expected = fs::read_to_string(expected_file)?; + Command::cargo_bin("echor")? + .args(args) + .assert() + .success() + .stdout(expected); + Ok(()) +} + +// #[test] +// fn hello1() -> Result<()> { +// run(&["Hello there"], "tests/expected/hello1.txt") +// } + +// #[test] +// fn hello2() -> Result<()> { + // run(&["Hello", "there"], "tests/expected/hello2.txt") +// } + +#[test] +fn hello1_no_newline() -> Result<()> { + run(&["Hello there", "-n"], "tests/expected/hello1.n.txt") // Two spaces! +} + +#[test] +fn hello2_no_newline() -> Result<()> { + run(&["-n", "Hello", "there"], "tests/expected/hello2.n.txt") +} \ No newline at end of file diff --git a/cli-book/echor/tests/expected/hello1.n.txt b/cli-book/echor/tests/expected/hello1.n.txt new file mode 100644 index 0000000..b1011d0 --- /dev/null +++ b/cli-book/echor/tests/expected/hello1.n.txt @@ -0,0 +1 @@ +Hello there \ No newline at end of file diff --git a/cli-book/echor/tests/expected/hello1.txt b/cli-book/echor/tests/expected/hello1.txt new file mode 100644 index 0000000..d6613f5 --- /dev/null +++ b/cli-book/echor/tests/expected/hello1.txt @@ -0,0 +1 @@ +Hello there diff --git a/cli-book/echor/tests/expected/hello2.n.txt b/cli-book/echor/tests/expected/hello2.n.txt new file mode 100644 index 0000000..466c153 --- /dev/null +++ b/cli-book/echor/tests/expected/hello2.n.txt @@ -0,0 +1 @@ +Hello there \ No newline at end of file diff --git a/cli-book/echor/tests/expected/hello2.txt b/cli-book/echor/tests/expected/hello2.txt new file mode 100644 index 0000000..d6613f5 --- /dev/null +++ b/cli-book/echor/tests/expected/hello2.txt @@ -0,0 +1 @@ +Hello there