book icon indicating copy to clipboard operation
book copied to clipboard

Add details to stdin section of Machine Communication

Open brettchalupa opened this issue 3 years ago • 2 comments

Closes https://github.com/rust-cli/book/issues/55; based on convo there.

Let me know if I should redo the sample code to use clap and include tests for it. I wasn't sure if that's necessary or not.

brettchalupa avatar Oct 05 '22 20:10 brettchalupa

Thanks for the feedback and working with me through all of this!

I pushed up the requested changes and verified the program works. I tried to add a separate test file in src/in-depth/tests but cargo test wasn't running. Is there anything in particular I need to get that to work or should I just let it be without the separate test?

It was this at src/in-depth/tests/machine-communication-stdin.rs:

use assert_cmd::Command;

#[test]
fn single_file() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("machine-communication-stdin")?;

    cmd.arg("haiku.txt");
    cmd.assert().success().stdout(String::from("7\n"));

    Ok(())
}

#[test]
fn stdin() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("machine-communication-stdin")?;

    cmd.arg("-")
        .write_stdin("Hello, world! My first name is Standard, my last name is In.\n");
    cmd.assert().success().stdout(String::from("12\n"));

    Ok(())
}

#[test]
fn file_doesnt_exist() -> Result<(), Box<dyn std::error::Error>> {
    let mut cmd = Command::cargo_bin("machine-communication-stdin")?;

    cmd.arg("tests/file/doesnt/exist");
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("No such file or directory"))

    Ok(())
}

brettchalupa avatar Oct 07 '22 00:10 brettchalupa

I've tried to address all of the feedback. 👍 I'd like to squash some (or all) of the commits whenever this is ready to merge, as I have some fixup commits for a few mistakes I corrected.

brettchalupa avatar Oct 11 '22 23:10 brettchalupa

@epage would you be up for reviewing this again when you have a chance? Thank you!

brettchalupa avatar Oct 26 '22 14:10 brettchalupa