Skip to content

Instantly share code, notes, and snippets.

@Geobert
Created January 3, 2018 18:35
Show Gist options
  • Select an option

  • Save Geobert/a989ea06281b0fad39ee2bc67269ad06 to your computer and use it in GitHub Desktop.

Select an option

Save Geobert/a989ea06281b0fad39ee2bc67269ad06 to your computer and use it in GitHub Desktop.
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;
lazy_static! {
static ref _CWD: PathBuf = env::current_dir().unwrap();
static ref CWD: &'static Path = _CWD.as_path();
// TODO test on release
static ref _BIN: PathBuf = CWD.join("target/debug/cobalt");
static ref BIN: &'static str = _BIN.to_str().unwrap();
}
#[test]
pub fn invalid_calls() {
println!("Binary: {:?}", BIN.to_owned());
assert_cli::Assert::main_binary()
.fails()
.stderr()
.contains("requires a subcommand")
.unwrap();
assert_cli::Assert::main_binary()
.with_args(&["--nonexistent-argument"])
.fails_with(1)
.stderr()
.contains(r"Found argument '--nonexistent-argument' which wasn't expected")
.unwrap();
}
#[test]
pub fn log_levels_trace() {
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();
assert_cli::Assert::main_binary()
.with_args(&["build", "-L", "trace", "-d", &dest_param])
.current_dir(CWD.join("tests/fixtures/example"))
.stderr()
.contains("[trace]")
.stderr()
.contains("[debug]")
.stderr()
.contains("[info]")
.unwrap();
destdir.close().unwrap();
}
#[test]
pub fn log_levels_trace_alias() {
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();
assert_cli::Assert::main_binary()
.with_args(&["build", "--trace", "-d", &dest_param])
.current_dir(CWD.join("tests/fixtures/example"))
.stderr()
.contains("[trace]")
.stderr()
.contains("[debug]")
.stderr()
.contains("[info]")
.unwrap();
destdir.close().unwrap();
}
#[test]
pub fn log_levels_debug() {
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();
assert_cli::Assert::main_binary()
.with_args(&["build", "-L", "debug", "-d", &dest_param])
.current_dir(CWD.join("tests/fixtures/example"))
.stderr()
.doesnt_contain("[trace]")
.stderr()
.contains("[debug]")
.stderr()
.contains("[info]")
.unwrap();
destdir.close().unwrap();
}
#[test]
pub fn log_levels_info() {
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();
assert_cli::Assert::main_binary()
.with_args(&["build", "-L", "info", "-d", &dest_param])
.current_dir(CWD.join("tests/fixtures/example"))
.stderr()
.doesnt_contain("[trace]")
.stderr()
.doesnt_contain("[debug]")
.stderr()
.contains("[info]")
.unwrap();
destdir.close().unwrap();
}
#[test]
pub fn log_levels_silent() {
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::Assert::main_binary()
.with_args(&["build", "--silent", "-d", &dest_param])
.current_dir(CWD.join("tests/fixtures/example"))
.stderr()
.is("")
.stdout()
.is("")
.unwrap();
destdir.close().unwrap();
}
#[test]
pub fn clean() {
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::Assert::main_binary()
.with_args(&["build", "--trace", "-d", &dest_param])
.current_dir(CWD.join("tests/fixtures/example"))
.unwrap();
assert_eq!(destdir.path().is_dir(), true);
assert_cli::Assert::main_binary()
.with_args(&["clean", "--trace", "-d", &dest_param])
.current_dir(CWD.join("tests/fixtures/example"))
.unwrap();
assert_eq!(destdir.path().is_dir(), false);
}
#[cfg(not(windows))]
#[test]
pub fn clean_warning() {
assert_cli::Assert::main_binary()
.with_args(&["clean"])
.current_dir(CWD.join("tests/fixtures/example"))
.fails_with(1)
.stderr()
.contains(
"Attempting to delete current directory, Cancelling the \
operation",
)
.unwrap();
}
#[test]
pub fn init_project_can_build() {
let initdir = TempDir::new("init").expect("Tempdir not created");
let destdir = TempDir::new("dest").expect("Tempdir not created");
let dest_param = destdir
.path()
.to_str()
.expect("Can't convert destdir to str")
.to_owned();
assert_cli::Assert::main_binary()
.with_args(&["init", "--trace"])
.current_dir(initdir.path())
.unwrap();
assert_cli::Assert::main_binary()
.with_args(&["build", "--trace", "-d", &dest_param])
.current_dir(initdir.path())
.unwrap();
destdir.close().unwrap();
initdir.close().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment