Skip to content

Commit 491a77a

Browse files
committed
Update chapter 2
1 parent d091c03 commit 491a77a

File tree

8 files changed

+98
-3
lines changed

8 files changed

+98
-3
lines changed

cli-book/echor/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ edition = "2021"
55

66
[dependencies]
77
clap = "4.5.23"
8+
9+
[dev-dependencies]
10+
anyhow = "1.0.79"
11+
assert_cmd = "2.0.13"
12+
predicates = "3.0.4"
13+
pretty_assertions = "1.4.0"

cli-book/echor/mk-outs.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
OUTDIR="tests/expected"
4+
[[ ! -d "$OUTDIR" ]] && mkdir -p "$OUTDIR"
5+
6+
echo "Hello there" > $OUTDIR/hello1.txt
7+
echo "Hello" "there" > $OUTDIR/hello2.txt
8+
echo -n "Hello there" > $OUTDIR/hello1.n.txt
9+
echo -n "Hello" "there" > $OUTDIR/hello2.n.txt

cli-book/echor/src/main.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ fn main() {
2626
.value_name("TEXT")
2727
.help("Input Text")
2828
.required(true)
29-
.num_args(1..),
29+
.num_args(1..)
30+
.action(ArgAction::Append),
3031
)
3132
.arg(
3233
Arg::new("omit_newline")
@@ -36,5 +37,18 @@ fn main() {
3637
)
3738
.get_matches();
3839

39-
println!("{:#?}", matches);
40-
}
40+
// println!("{:#?}", matches);
41+
42+
let text: Vec<String> = matches
43+
.get_many("text")
44+
.expect("text is required")
45+
.cloned()
46+
.collect();
47+
48+
// println!("{:#?}", text);
49+
50+
let omit_newline = matches.get_flag("omit_newline");
51+
// println!("{:#?}", omit_newline);
52+
53+
print!("{}{}", text.join(" "), if omit_newline { "" } else { "\n" });
54+
}

cli-book/echor/tests/cli.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use assert_cmd::Command;
2+
use predicates::prelude::*;
3+
use std::fs;
4+
use anyhow::Result;
5+
6+
#[test]
7+
fn dies_no_args() -> Result<()> {
8+
let mut cmd = Command::cargo_bin("echor")?;
9+
cmd.assert()
10+
.failure()
11+
.stderr(predicate::str::contains("Usage"));
12+
Ok(())
13+
}
14+
15+
#[test]
16+
fn hello1() -> Result<()> {
17+
let expected = fs::read_to_string("tests/expected/hello1.txt")?;
18+
let mut cmd = Command::cargo_bin("echor")?;
19+
cmd.arg("Hello there").assert().success().stdout(expected);
20+
Ok(())
21+
}
22+
23+
#[test]
24+
fn hello2() -> Result<()> {
25+
let expected = fs::read_to_string("tests/expected/hello2.txt")?;
26+
let mut cmd = Command::cargo_bin("echor")?;
27+
cmd.args(vec!["Hello", "there"])
28+
.assert()
29+
.success()
30+
.stdout(expected);
31+
Ok(())
32+
}
33+
34+
fn run(args: &[&str], expected_file: &str) -> Result<()> {
35+
let expected = fs::read_to_string(expected_file)?;
36+
Command::cargo_bin("echor")?
37+
.args(args)
38+
.assert()
39+
.success()
40+
.stdout(expected);
41+
Ok(())
42+
}
43+
44+
// #[test]
45+
// fn hello1() -> Result<()> {
46+
// run(&["Hello there"], "tests/expected/hello1.txt")
47+
// }
48+
49+
// #[test]
50+
// fn hello2() -> Result<()> {
51+
// run(&["Hello", "there"], "tests/expected/hello2.txt")
52+
// }
53+
54+
#[test]
55+
fn hello1_no_newline() -> Result<()> {
56+
run(&["Hello there", "-n"], "tests/expected/hello1.n.txt") // Two spaces!
57+
}
58+
59+
#[test]
60+
fn hello2_no_newline() -> Result<()> {
61+
run(&["-n", "Hello", "there"], "tests/expected/hello2.n.txt")
62+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there

0 commit comments

Comments
 (0)