use std::process::{Command, Output};
fn probe(flag: &str) -> Output {
Command::new(env!("CARGO_BIN_EXE_shep-deploy"))
.arg(flag)
.env_remove("SHEP_HOME")
.env_remove("SHEP_DOG_NAME")
.output()
.expect("the test binary's own sibling is on disk")
}
#[test]
fn the_version_answer_is_the_one_shep_reads() {
let output = probe("--version");
assert!(output.status.success(), "{:?}", output.status);
let text = String::from_utf8(output.stdout).expect("the answer is text");
let answer = shep_client::shep_core::dogs::parse_version_answer(&text)
.expect("the answer shep's own parser cannot read is the bug this pins");
assert_eq!(answer.version, env!("CARGO_PKG_VERSION"));
assert_eq!(
answer.protocol,
Some(shep_client::PROTOCOL_VERSION),
"the protocol answered is the one this binary was compiled against"
);
}
#[test]
fn the_schema_answer_is_json_describing_this_dogs_own_section() {
let output = probe("--schema");
assert!(output.status.success(), "{:?}", output.status);
let schema: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("the answer is JSON");
assert_eq!(
schema.get("title").and_then(serde_json::Value::as_str),
Some("deploy"),
"the pane heads its form with this, so it names the section rather than the Rust type"
);
assert!(
schema.pointer("/properties/interval").is_some(),
"the section's own keys are what the schema describes"
);
}
#[test]
fn an_ordinary_argument_is_not_a_probe() {
let output = probe("--not-a-flag-this-dog-knows");
assert!(
!output.status.success(),
"an unknown flag is still a usage error"
);
assert!(
output.stdout.is_empty(),
"nothing is printed to stdout, so shep cannot read a usage message as an answer"
);
}
fn by_hand(args: &[&str]) -> Output {
let home = tempfile::tempdir().expect("a temporary $SHEP_HOME");
Command::new(env!("CARGO_BIN_EXE_shep-deploy"))
.args(args)
.env("SHEP_HOME", home.path())
.env_remove("SHEP_DOG_NAME")
.output()
.expect("the test binary's own sibling is on disk")
}
#[test]
fn a_bare_run_by_hand_prints_usage_and_does_not_poll() {
let output = by_hand(&[]);
assert_eq!(output.status.code(), Some(2), "{:?}", output.status);
assert!(
String::from_utf8_lossy(&output.stderr).contains("usage: shep-deploy"),
"usage goes to stderr: {:?}",
output.stderr
);
assert!(output.stdout.is_empty(), "nothing is printed to stdout");
}
#[test]
fn help_prints_usage_on_stdout_and_exits_zero() {
for arg in ["-h", "--help", "help"] {
let output = by_hand(&[arg]);
assert!(output.status.success(), "{arg}: {:?}", output.status);
assert!(
String::from_utf8_lossy(&output.stdout).contains("usage: shep-deploy"),
"{arg}: usage goes to stdout: {:?}",
output.stdout
);
assert!(output.stderr.is_empty(), "{arg}: nothing on stderr");
}
}