assert_cmd icon indicating copy to clipboard operation
assert_cmd copied to clipboard

Document how to use assert_cmd with rust-cross

Open ducaale opened this issue 4 years ago • 4 comments

It would be nice to mention that when running tests in cross, one needs to prepend their command with the value contained in CARGO_TARGET_*_RUNNER i.e qemu-aarch64 xh

fn find_runner() -> Option<String> {
    for (key, value) in std::env::vars() {
        if key.starts_with("CARGO_TARGET_") && key.ends_with("_RUNNER") && value.contains("qemu") {
            return Some(value);
        }
    }
    None
}

fn get_base_command() -> Command {
    let mut cmd;
    let path = assert_cmd::cargo::cargo_bin("xh");
    if let Some(runner) = find_runner() {
        cmd = Command::new(runner);
        cmd.arg(path);
    } else {
        cmd = Command::new(path);
    }
    cmd
}

Also, see https://github.com/ducaale/xh/pull/190#issuecomment-968932013

ducaale avatar Dec 22 '21 11:12 ducaale

Thanks for providing an example on how to do this. This will be good to add to the docs here and for trycmd

epage avatar Dec 22 '21 14:12 epage

I wonder, has this changed at all? I'm struggling to get my integration tests running with cross v0.2.4 and assert_cmd.

When running under cross for --target armv7-unknown-linux-gnueabihf, CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_RUNNER = /linux-runner armv7 i.e. the value does not contain qemu, which is what is tested for in find_runner().

Experimenting with adding the prefix manually, the following do not work: /linux-runner armv7, qemu-armv7 and qemu-aarch64.

What does work is qemu-arm, but that is nowhere to be found in the environment variables.

Hence I ask, what is the best way to handle this case at present?

mfreeborn avatar Jul 30 '22 07:07 mfreeborn

@mfreeborn, can you try if the following works for you? I think the values of CARGO_TARGET_*_RUNNER have been updated in cross v0.2.2

fn find_runner() -> Option<String> {
    for (key, value) in std::env::vars() {
        if key.starts_with("CARGO_TARGET_") && key.ends_with("_RUNNER") && !value.is_empty() {
            return Some(value);
        }
    }
    None
}

fn get_base_command() -> Command {
    let mut cmd;
    let path = assert_cmd::cargo::cargo_bin("xh");
    if let Some(runner) = find_runner() {
        let mut runner = runner.split_whitespace();
        cmd = Command::new(runner.next().unwrap());
        for arg in runner {
            cmd.arg(arg);
        }
        cmd.arg(path);
    } else {
        cmd = Command::new(path);
    }
    cmd
}

ducaale avatar Jul 30 '22 12:07 ducaale

I can confirm that is working - thanks very much!

mfreeborn avatar Jul 30 '22 12:07 mfreeborn

Thank you for helping me find this method. Please document it for future reference.

mizuki0629 avatar Dec 16 '23 07:12 mizuki0629