grrs-masterbongo 0.1.0

A simple implementation of grep
Documentation
use anyhow::Result; // Error helpers
use assert_cmd::prelude::*; // Add methods on commands
use assert_fs::prelude::*; // Add methods on temp dir / temp file
use predicates::prelude::*; // Used for writing assertions
use std::process::Command; // Run programs

/// Checks that the run fails and the error message is correct
#[test]
fn file_doesnt_exist() -> Result<()> {
    let mut cmd = Command::cargo_bin("grrs")?; // runs binary from the current crate

    cmd.arg("foobar").arg("test/file/doesnt/exist"); // pass arguments to the command

    // assert_cmd allows assertions on commands and their output
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("Error reading file"));

    Ok(())
}

/// Checks that the run succeeds and the output is correct
#[test]
fn find_content_in_file() -> Result<()> {
    // open / create a temp file and write to it with assert_fs
    let file = assert_fs::NamedTempFile::new("sample.txt")?;
    file.write_str("A test\nActual content\nMore content\nAnother test")?;

    // use assert_cmd to run the program and check the result
    let mut cmd = Command::cargo_bin("grrs")?;
    cmd.arg("test").arg(file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("A test\nAnother test"));

    Ok(())
}