Fix(test): No more manual test setup
- Test setup now done automatically via `cargo test`. Testing fixtures are automatically injected before tests.
This commit is contained in:
57
tests/cli.rs
57
tests/cli.rs
@@ -1,9 +1,43 @@
|
||||
use assert_cmd::Command;
|
||||
use predicates::prelude::*;
|
||||
use rstest::{fixture, rstest};
|
||||
use std::fs;
|
||||
|
||||
type TestResult = Result<(), Box<dyn std::error::Error>>;
|
||||
|
||||
#[fixture]
|
||||
#[once]
|
||||
pub fn pre_test() -> bool {
|
||||
let script_path = "./test_script.sh";
|
||||
|
||||
// Ensure the script file has the necessary permissions to be executed
|
||||
let mut permissions_cmd = Command::new("chmod");
|
||||
permissions_cmd.arg("+x").arg(script_path);
|
||||
permissions_cmd
|
||||
.output()
|
||||
.expect("Cannot make test_script.sh executable");
|
||||
|
||||
// Run the bash script using the `Command` struct
|
||||
let output = Command::new("bash")
|
||||
.arg(script_path)
|
||||
.output()
|
||||
.expect("Running bash script failed!");
|
||||
|
||||
if output.status.success() {
|
||||
// The script ran successfully
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
println!("Script Output:\n{}", stdout);
|
||||
|
||||
true
|
||||
} else {
|
||||
// The script encountered an error
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
println!("Script Error:\n{}", stderr);
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dies_no_args() -> TestResult {
|
||||
let mut cmd = Command::cargo_bin("echor")?;
|
||||
@@ -25,19 +59,26 @@ fn run(args: &[&str], expected_file: &str) -> TestResult {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hello1() -> TestResult {
|
||||
#[rstest]
|
||||
fn hello1(pre_test: &bool) -> TestResult {
|
||||
assert_eq!(pre_test, &true);
|
||||
run(&["Hello there"], "tests/expected/hello1.txt")
|
||||
}
|
||||
#[test]
|
||||
fn hello2() -> TestResult {
|
||||
|
||||
#[rstest]
|
||||
fn hello2(pre_test: &bool) -> TestResult {
|
||||
assert_eq!(pre_test, &true);
|
||||
run(&["Hello", "there"], "tests/expected/hello2.txt")
|
||||
}
|
||||
#[test]
|
||||
fn hello1_no_newline() -> TestResult {
|
||||
|
||||
#[rstest]
|
||||
fn hello1_no_newline(pre_test: &bool) -> TestResult {
|
||||
assert_eq!(pre_test, &true);
|
||||
run(&["Hello there", "-n"], "tests/expected/hello1.n.txt")
|
||||
}
|
||||
#[test]
|
||||
fn hello2_no_newline() -> TestResult {
|
||||
|
||||
#[rstest]
|
||||
fn hello2_no_newline(pre_test: &bool) -> TestResult {
|
||||
assert_eq!(pre_test, &true);
|
||||
run(&["-n", "Hello", "there"], "tests/expected/hello2.n.txt")
|
||||
}
|
||||
|
Reference in New Issue
Block a user