How Can I Write An Integration Tests For A Rust CLI Tools That Use Inquire?

Here is an example of some code I would like to test:

fn main() {
    let ans = Confirm::new("Yes or no?")
        .with_default(false)
        .with_help_message("(It's not a trick question)")
        .prompt();

    let res =  match ans {
        Ok(true) => "You said yes!".to_string(),
        Ok(false) => "You said no...".to_string(),
        Err(_) => "Error with questionnaire, try again later".to_string(),
    };

    println!("{}", res);
}

I am trying to use the "Command" library to write a test, but I have having some problems. My first problem is sending the different types of user input when prompted (eg. enter with no input, y, Y, yes, Yes, YES, n, no, No, NO, nO, false, foo) and then asserting that the appropriate response is logged to the console.

I would also like to assert that the correct initial prompt message and help message are logged to the console. How can I do this?

Here' my not working test:

#[test]
fn asks_are_you_cool() -> Result<(), Box<dyn std::error::Error>> {

    let output_bytes = Command::cargo_bin("cool_bool")?.output().unwrap().stdout;

    let output_str = match str::from_utf8(&output_bytes) {
        Ok(val) => val,
        Err(_) => panic!("got non UTF-8 data from stdout"),
    };

    assert_eq!(output_str, "That's too bad, I though you might have been.\n");

    Ok(())
}

Thanks!



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation