lundi 27 juillet 2020

How to forward comand line arguments from 'cargo test' to std::process::Command?

I have a binary project and I want to test it. I normally run my project like this:

cargo run -- -b localhost:8399

How can I do the same with cargo test?

use std::sync::Once;

static INIT: Once = Once::new();

pub fn init() {
    INIT.call_once(|| {
        // initialization code here
        let args: Vec<String> = std::env::args().collect();

        print!("got this as cmd args:-");

        for i in &args {
            print!(" {}", i);
        }
        println!("");

        //do something cool like
        // let _server = Command::new("cargo").args(args).spawn();
    });
}

#[test]
fn working() {
    init();
    println!("working!!!!!!!!");
}

#[test]
fn working2() {
    init();
    println!("working2222222222!!!!!!!!");
}

when I execute the above code, I get this:

❯ cargo test -- --nocapture --arg1 blabla --cool value --watch anime --everyday true
    Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running target/debug/deps/examer-2c07d411874ae585
error: Unrecognized option: 'arg1'
error: test failed, to rerun pass '--bin examer'

I expect it to output something like this:

got this as cmd args:- --arg1 blabla --cool value --watch anime --everyday true
working!!!!!!!!
working2222222222!!!!!!!!
test working ... ok
test working2 ... ok

Aucun commentaire:

Enregistrer un commentaire