predicates-rs icon indicating copy to clipboard operation
predicates-rs copied to clipboard

Why does `predicate::str::diff` require a `'static` input?

Open zackw opened this issue 11 months ago • 1 comments

Most of the string predicates accept an argument that can vary at runtime (usually as Into<String>) but str::diff asks instead for an argument that is Into<Cow<'static, str>>, which is not satisfied by strings created at runtime. This means I can't do something like

use std::str;

use assert_cmd::Command;
use predicates::prelude::*;

fn my_program() -> Command {
    Command::cargo_bin("my_program").unwrap()
}

#[test]
fn help_options() {
    let help_output =
        my_program().arg("--help").assert()
        .success().stderr("").get_output().to_owned();

    my_program().arg("-h").assert()
        .success()
        .stderr("")
        .stdout(predicate::str::diff(
            str::from_utf8(&help_output.stdout).unwrap()
        ));
}

I don't see why this couldn't be made to work, but with the current implementation it's a compile-time error:

error[E0277]: the trait bound `std::borrow::Cow<'static, str>: std::convert::From<std::vec::Vec<u8>>` is not satisfied
  --> my_crate/tests/cli_tests.rs:26:38
   |
26 |         .stdout(predicate::str::diff(help_status.get_output().stdout));
   |                 -------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::vec::Vec<u8>>` is not implemented for `std::borrow::Cow<'static, str>`
   |                 |
   |                 required by a bound introduced by this call
   |

Meta

predicates-rs version: 3.0.3 rustc --version --verbose:

rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: aarch64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2

zackw avatar Jul 09 '23 01:07 zackw

Huh, can't remember why I did that but it doesn't mean you can't pass in strings at runtime but that you must transfer ownership. A Cow can be both a borrowed and an owned string.

        .stdout(predicate::str::diff(
            str::from_utf8(help_output.stdout.clone()).unwrap()
        ));

epage avatar Jul 09 '23 02:07 epage