use assert_fs::prelude::*; use assert_cmd::prelude::*; use predicates::prelude::*; use std::process::Command; const TEST_CONTENT: &str = "A test\nActual content\nMore content\nAnother test"; const BIN_NAME: &str = "grrs"; fn run_test( args: &[&str], file_content: &str, expected_status: bool, expected_output: &str, output_type: OutputType, ) -> Result<(), Box> { let file = assert_fs::NamedTempFile::new("sample.txt")?; file.write_str(file_content)?; let mut cmd = Command::cargo_bin(BIN_NAME)?; cmd.args(args).arg(file.path()); let mut assert = cmd.assert(); if expected_status { assert = assert.success(); } else { assert = assert.failure(); }; match output_type { OutputType::Stdout => assert.stdout(predicate::str::contains(expected_output)), OutputType::Stderr => assert.stderr(predicate::str::contains(expected_output)), }; Ok(()) } enum OutputType { Stdout, Stderr, } #[test] fn find_a_match() -> Result<(), Box> { run_test( &["test"], TEST_CONTENT, true, "A test\nAnother test", OutputType::Stdout, ) } #[test] fn find_no_match() -> Result<(), Box> { run_test( &["nomatch"], TEST_CONTENT, false, "No matches found for pattern: nomatch", OutputType::Stderr, ) } #[test] fn empty_pattern() -> Result<(), Box> { run_test( &[""], TEST_CONTENT, false, "Empty pattern is not allowed", OutputType::Stderr, ) } #[test] fn empty_file() -> Result<(), Box> { run_test( &["test"], "", false, "No matches found for pattern: test", OutputType::Stderr, ) }