use assert_cmd::Command;
use predicates::prelude::*;
fn copy_zhao_binary_to_a_fresh_temp_file() -> tempfile::TempPath {
let original = Command::cargo_bin("zhao")
.expect("binary should build")
.get_program()
.to_os_string();
let temp_file = tempfile::NamedTempFile::new().expect("should create temp file");
std::fs::copy(&original, temp_file.path()).expect("should copy the built binary");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = std::fs::metadata(temp_file.path())
.expect("should stat copy")
.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(temp_file.path(), perms).expect("should chmod copy");
}
temp_file.into_temp_path()
}
#[test]
fn update_to_a_pinned_version_replaces_the_binary() {
let temp_copy = copy_zhao_binary_to_a_fresh_temp_file();
let before = std::fs::read(&temp_copy).expect("should read the pre-update binary");
let status = std::process::Command::new(&temp_copy)
.arg("update")
.arg("v0.2.0")
.status()
.expect("command should run");
assert!(status.success(), "zhao update v0.2.0 should succeed");
let after = std::fs::read(&temp_copy).expect("should read the post-update binary");
assert_ne!(
before, after,
"the binary's contents should have actually changed"
);
}
#[test]
fn update_to_a_nonexistent_tag_produces_a_clear_error_and_leaves_the_binary_untouched() {
let temp_copy = copy_zhao_binary_to_a_fresh_temp_file();
let before = std::fs::read(&temp_copy).expect("should read the pre-update binary");
Command::from_std(std::process::Command::new(&temp_copy))
.arg("update")
.arg("v99.99.99-does-not-exist")
.assert()
.code(2)
.stderr(
predicate::str::contains("error:")
.and(predicate::str::contains("v99.99.99-does-not-exist")),
);
let after = std::fs::read(&temp_copy).expect("should read the binary after the failed update");
assert_eq!(
before, after,
"a failed update should never leave a partial/broken binary in place"
);
}
#[test]
fn nightly_and_a_version_argument_are_mutually_exclusive() {
Command::cargo_bin("zhao")
.expect("binary should build")
.arg("update")
.arg("v0.1.0")
.arg("--nightly")
.assert()
.code(2)
.stderr(predicate::str::contains("cannot be used with"));
}