assert_cmd :: cargo_bin use Car go .toml из другого каталога - PullRequest
1 голос
/ 02 мая 2020

Итак, я разрабатываю CLI с такими командами:

sandwich [command]

create <destination> - Creates a Rust library at the specified destination to act as the sandwich.
eat - Used from within a library to eat the sandwich.

и сейчас пишу для него тесты, которые работают нормально, пока мне не нужно писать тесты для eat:

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

// It should create a sandwich then eat it.
#[test]
fn create_sandwich_then_eat_it() -> Result<(), Box<dyn std::error::Error>> {
    std::env::set_current_dir("tests").expect("Unable to change to test directory");

    // This runs the cli and the create command which in turn runs `cargo new pastrami --lib`.
    let mut cmd = Command::cargo_bin("sandwichy")?;
    cmd.arg("create").arg("veggie");
    cmd.assert().success();

    // Now we want to eat the sandwich but we need to go into the sandwich directory because the eat command can only be used from a created sandwich.
    std::env::set_current_dir("veggie").expect("Unable to change to sandwich directory");

    // We try to eat but get an error because the pastrami has a Cargo.toml which is trying to be used to run the command.
    let mut cmd_eat = Command::cargo_bin("sandwichy")?;
    cmd_eat.arg("eat");
    cmd_eat.assert().success();

    Ok(())
}

В приведенном выше примере я получаю следующую ошибку:

---- create_sandwich_then_eat_it stdout ----
Error: CargoError { cause Some(CargoError { kind: CommandFailed, context: Some("error: failed to parse manifest ...etc

Итак, я посмотрел на документы и там написано, что cargo_bin запускает команду из текущего каталога, и так как я изменил в каталог сэндвичей, у которого есть Car go .toml. Я предполагаю, что он пытается запустить что-то оттуда, чего не существует.

Я понял, что это не ошибка со стороны cargo_bin, потому что он делает то, что должен делать, но есть ли способ выполнить то, что я пытаюсь сделать, с этим или без него?

...