use assert_cmd::crate_name; use assert_cmd::prelude::*; // Add methods on commands use predicates::prelude::*; // Used for writing assertions use std::io::Write; use std::process::Command; // Run programs use tempfile::NamedTempFile; #[test] fn find_a_match() { let mut result = Vec::new(); galaxy::find_matches("lorem ipsum\ndolor sit amet", "lorem", &mut result); assert_eq!(result, b" 1| lorem ipsum\n"); } #[test] fn file_doesnt_exist() -> Result<(), Box> { let mut cmd = Command::cargo_bin(crate_name!())?; cmd.arg("foobar").arg("test/file/doesnt/exist"); cmd.assert() .failure() .stderr(predicate::str::contains("No such file or directory")); Ok(()) } #[test] fn find_content_in_file() -> Result<(), Box> { let mut file = NamedTempFile::new()?; writeln!(file, "A test\nActual content\nMore content\nAnother test")?; let mut cmd = Command::cargo_bin(crate_name!())?; cmd.arg("test").arg(file.path()); cmd.assert() .success() .stdout(predicate::str::contains(" 1| A test\n 4| Another test")); Ok(()) }