Skip to content

Commit

Permalink
fix(tests): Harden CLI tests
Browse files Browse the repository at this point in the history
The new traces are not safe against re-running with an overlapping source
and dest dir.  Ideally, we'd generate to tempdir's anyways but then the paths are
different, breaking traces.  So, in the end, we'd just mirroring the same
test between `trace` and `trace_alias`.
  • Loading branch information
epage committed May 24, 2017
1 parent 334c4d2 commit 0a73edd
Showing 1 changed file with 91 additions and 16 deletions.
107 changes: 91 additions & 16 deletions tests/cli.rs
@@ -1,14 +1,15 @@
#[macro_use]
extern crate difference;
#[macro_use]
extern crate assert_cli;
#[macro_use]
extern crate lazy_static;
extern crate tempdir;

use std::env;
use std::str;
use std::path::{Path, PathBuf};

use tempdir::TempDir;

static EMPTY: &'static [&'static str] = &[];
lazy_static! {
static ref _CWD: PathBuf = env::current_dir().unwrap();
Expand Down Expand Up @@ -46,41 +47,115 @@ pub fn invalid_calls() {
}

#[test]
pub fn log_levels() {
pub fn log_levels_trace() {
env::set_current_dir(CWD.join("tests/fixtures/example")).unwrap();

let output1 = assert_cli!(&BIN, &["build", "--trace"] => Success).unwrap();
let output1_stderr = str::from_utf8(&output1.stderr).to_owned().unwrap();
let destdir = TempDir::new("trace").expect("Tempdir not created");
let dest_param = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();

let output1 = assert_cli!(&BIN, &["build", "-L", "trace", "-d", &dest_param] => Success)
.unwrap();
assert_contains!(&output1.stderr, "[trace]");
assert_contains!(&output1.stderr, "[debug]");
assert_contains!(&output1.stderr, "[info]");

let output2 = assert_cli!(&BIN, &["build", "-L", "trace"] => Success).unwrap();
let output2_stderr = str::from_utf8(&output2.stderr).to_owned().unwrap();
assert_diff!(&output1_stderr, &output2_stderr, " ", 0);
destdir.close().unwrap();
}

#[test]
pub fn log_levels_trace_alias() {
env::set_current_dir(CWD.join("tests/fixtures/example")).unwrap();

let destdir = TempDir::new("trace_alias").expect("Tempdir not created");
let dest_param = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();

let output1 = assert_cli!(&BIN, &["build", "--trace", "-d", &dest_param] => Success).unwrap();
assert_contains!(&output1.stderr, "[trace]");
assert_contains!(&output1.stderr, "[debug]");
assert_contains!(&output1.stderr, "[info]");

destdir.close().unwrap();
}

#[test]
pub fn log_levels_debug() {
env::set_current_dir(CWD.join("tests/fixtures/example")).unwrap();

let output = assert_cli!(&BIN, &["build", "-L", "debug"] => Success).unwrap();
let destdir = TempDir::new("debug").expect("Tempdir not created");
let dest_param = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();

let output = assert_cli!(&BIN, &["build", "-L", "debug", "-d", &dest_param] => Success)
.unwrap();
assert_contains_not!(&output.stderr, "[trace]");
assert_contains!(&output.stderr, "[debug]");
assert_contains!(&output.stderr, "[info]");

let output = assert_cli!(&BIN, &["build", "-L", "info"] => Success).unwrap();
destdir.close().unwrap();
}

#[test]
pub fn log_levels_info() {
env::set_current_dir(CWD.join("tests/fixtures/example")).unwrap();

let destdir = TempDir::new("info").expect("Tempdir not created");
let dest_param = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();

let output = assert_cli!(&BIN, &["build", "-L", "info", "-d", &dest_param] => Success).unwrap();
assert_contains_not!(&output.stderr, "[trace]");
assert_contains_not!(&output.stderr, "[debug]");
assert_contains!(&output.stderr, "[info]");

assert_cli!(&BIN, &["build", "--silent"] => Success, "").unwrap();
destdir.close().unwrap();
}

#[test]
pub fn log_levels_silent() {
env::set_current_dir(CWD.join("tests/fixtures/example")).unwrap();

let destdir = TempDir::new("silent").expect("Tempdir not created");
let dest_param = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();

assert_cli!(&BIN, &["build", "--silent", "-d", &dest_param] => Success, "").unwrap();

destdir.close().unwrap();
}

#[test]
pub fn clean() {
env::set_current_dir(CWD.join("tests/fixtures/example")).unwrap();
assert_cli!(&BIN, &["build", "-d", "./test_dest"] => Success).unwrap();
assert_eq!(Path::new("./test_dest/").is_dir(), true);

let output = assert_cli!(&BIN, &["clean", "-d", "./test_dest"] => Success).unwrap();
assert_eq!(Path::new("./test_dest").is_dir(), false);
assert_contains!(&output.stderr, "directory \"./test_dest\" removed");
let destdir = TempDir::new("clean").expect("Tempdir not created");
let dest_param = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();

assert_cli!(&BIN, &["build", "-d", &dest_param] => Success).unwrap();
assert_eq!(destdir.path().is_dir(), true);

assert_cli!(&BIN, &["clean", "-d", &dest_param] => Success).unwrap();
assert_eq!(destdir.path().is_dir(), false);
}

#[cfg(not(windows))]
Expand Down

0 comments on commit 0a73edd

Please sign in to comment.