Skip to content

cargo_bin!() can’t take non-literals binary names #268

@woshilapin

Description

@woshilapin

Following https://gitlab.com/woshilapin/cargo-sonar/-/issues/16 (thank you for the report by the way), I tried to update my tests but got the following error.

error: expected a literal
  --> tests/bin.rs:28:41
   |
28 |     Command::new(assert_cmd::cargo_bin!(binary.to_string()))
   |                                         ^^^^^^^^^^^^^^^^^^
   |
   = note: only literals (like `"foo"`, `-42` and `3.14`) can be passed to `concat!()`

And indeed, in my project, I have 2 binaries that slightly differs in their functionality: they produce a different output, but take mostly the same input. It is therefore easy to have almost the same test with something along these lines (simplified for brevity and might not compile, but I hope it helps understanding the problem).

enum Binary {
  First,
  Second,
}

impl Display for Binary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            Binary::First => write!(f, "first"),
            Binary::Second => write!(f, "second"),
        }
    }
}

fn run_binary(binary: Binary) -> tempfile::NamedTempFile {
    let output_tempfile = tempfile::NamedTempFile::new().unwrap();
    let output_path = output_tempfile.path();
    Command::new(assert_cmd::cargo_bin!(binary.to_string()))
      .unwrap()
      .arg("--output")
      .arg(output_path)
      .assert()
      .success();
    output_path
}

#[test]
fn first() {
  let output_path = run_binary(Binary::First);
  // whatever assertions for the output of the `first` binary
}

#[test]
fn second() {
  let output_path = run_binary(Binary::Second);
  // whatever assertions for the output of the `second` binary
}

I hope I didn’t miss an obvious solution to this, and also that my example is a valid use case. I felt the report didn’t need a working minimal example (the code I wrote above is only here to illustrate the issue, and I hope it is sufficient). If you require a working minimal example, just ask, and I will spend the time to provide it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions