ANN: `assert_cmd` released


#1

This is the spiritual successor to assert_cli. We plan to move assert_cli to being higher level, see Issue 41.

Next up is making it easier to setup file system fixtures and write assertions on the result of programs.


#2

And here’s the crates.io page for the project: assert_cmd


#3

I have a command line app, where a typical test case is like follows:

  • I call the cli app with parameter: -d testdir1
  • I have a good output in a file testdir1.out
  • I want to compare the output of the invocation with the output in file testdir1.out
  • In certain cases I expect a specific (unequal zero) return code from my cli app

Any hints are much appreciated.

Thanks, Manfred


#4

An approximate answer (haven’t compiled it) is:

use std::path;
use std::process;

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

#[test]
fn example() {
    process::Command::main_binary()
        .expect("singe-binary package that compiles")
        .args(&["-d", test_dir])
        .assert()
        .code(10) // or .success()
        .stdout(predicate::eq_file(path::Path::new("tests/fixtures/expected"));
}

I’m assuming from your parameter (-d testdir1) you are wanting to write to a directory and maybe verify the contents. This is where assert_fs comes in.

use std::path;
use std::process;

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

#[test]
fn example() {
    let temp = assert_fs::TempDir::new().unwrap();
    process::Command::main_binary()
        .expect("singe-binary package that compiles")
        .args(&["-d", temp.path()])
        .assert()
        .code(10) // or .success()
        .stdout(predicate::eq_file(path::Path::new("tests/fixtures/expected"));
    temp.child("bar.txt").assert(predicate::path::exists());
}

Please open issues for whatever you felt wasn’t clear in the documentation that you needed me to write this example.


#5

Thanks a lot for your quick and very helpful reply.

Actually I examine contents in the directory and want to compare the output of the cli app with some good output.

So, your first example did it very nicely. Nevertheless, the second example is also very instructive.

Perhaps, you could include both in the documentation of the assert_cmd crate.


#6

Do you mean that -d is an input to your CLI or that you want to verify the directory against a fixture like you did stdout?

The latter is just as easily supported. You can use an eq_file predicate with the assert_fs assertion. I want to eventually add directory diffing, the main concern being how to handle binary vs text files because you’d expect richer failure messages with text but how do you know when to treat a file as text?


#7

perhaps tree_magic can help, though it would be nice if it returned something richer than a String


#8

In my case -d specifies a directory which contains files and possibly subdirectories which my CLI examines according to a certain policy. If it detects errors it writes messages to stdout to tell what is wrong.


#9

perhaps tree_magic 2 can help, though it would be nice if it returned something richer than a String

I am assuming I’ll use mimetypes, thanks for the tip on a crate to use. And yes, it’d be nice if it offered more than a stringly typed API.