Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zackw opened this issue Jul 9, 2023 · 1 comment
Open

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

zackw opened this issue Jul 9, 2023 · 1 comment

Comments

@zackw
Copy link

zackw commented Jul 9, 2023

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
@epage
Copy link
Contributor

epage commented Jul 9, 2023

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()
        ));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants