Skip to content

Document how to use assert_cmd with rust-cross #139

Closed

Description

@ducaale

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 ducaale/xh#190 (comment)

Activity

  1. added
    enhancement
    on Dec 22, 2021
  2. epage commented on Dec 22, 2021

    @epage
    Contributor

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

  3. mfreeborn commented on Jul 30, 2022

    @mfreeborn

    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?

  4. ducaale commented on Jul 30, 2022

    @ducaale
    Author

    @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
    }
  5. mfreeborn commented on Jul 30, 2022

    @mfreeborn

    I can confirm that is working - thanks very much!

  6. mizuki0629 commented on Dec 16, 2023

    @mizuki0629

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

  7. added 3 commits that reference this issue on Jan 28, 2024
    c1d88ff
    646e083
    90b39b8
  8. added a commit that references this issue on Feb 19, 2024
    b3a290c
  9. added a commit that references this issue on Jan 27, 2026
    99922b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions