Skip to content

Commit

Permalink
Update chapter 2
Browse files Browse the repository at this point in the history
  • Loading branch information
rakurame96 committed Jan 2, 2025
1 parent d091c03 commit 491a77a
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cli-book/echor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions cli-book/echor/mk-outs.sh
Original file line number Diff line number Diff line change
@@ -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
20 changes: 17 additions & 3 deletions cli-book/echor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -36,5 +37,18 @@ fn main() {
)
.get_matches();

println!("{:#?}", matches);
}
// println!("{:#?}", matches);

let text: Vec<String> = 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" });
}
62 changes: 62 additions & 0 deletions cli-book/echor/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -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")
}
1 change: 1 addition & 0 deletions cli-book/echor/tests/expected/hello1.n.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello there
1 change: 1 addition & 0 deletions cli-book/echor/tests/expected/hello1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello there
1 change: 1 addition & 0 deletions cli-book/echor/tests/expected/hello2.n.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello there
1 change: 1 addition & 0 deletions cli-book/echor/tests/expected/hello2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello there

0 comments on commit 491a77a

Please sign in to comment.