use std::path::Path;
use std::process::Command;
use assert_cmd::prelude::*;
use tempfile::TempDir;
fn mnem(repo: &Path, args: &[&str]) -> Command {
let mut cmd = Command::cargo_bin("mnem").expect("built mnem binary");
cmd.current_dir(repo);
cmd.arg("-R").arg(repo);
for a in args {
cmd.arg(a);
}
cmd
}
#[test]
fn deferred_verbs_surface_in_help() {
let out = Command::cargo_bin("mnem")
.unwrap()
.arg("--help")
.assert()
.success();
let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
for verb in [
"fetch", "push", "pull", "merge", "revert", "fsck", "gc", "branch", "blame", "cat-file",
"remote", "clone",
] {
assert!(
stdout.contains(verb),
"--help must list `{verb}`, got: {stdout}"
);
}
}
#[test]
fn fsck_exits_zero_on_clean_repo() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
mnem(dir.path(), &["fsck"]).assert().success();
}
#[test]
fn gc_exits_zero_on_clean_repo() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
mnem(dir.path(), &["gc"]).assert().success();
}
#[test]
fn revert_bad_cid_exits_nonzero() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
mnem(dir.path(), &["revert", "badinput"]).assert().failure();
}
#[test]
fn revert_missing_cid_exits_nonzero() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
mnem(
dir.path(),
&[
"revert",
"bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
],
)
.assert()
.failure();
}
#[test]
fn revert_real_op_exits_zero() {
let dir = TempDir::new().unwrap();
mnem(dir.path(), &["init", dir.path().to_str().unwrap()])
.assert()
.success();
mnem(
dir.path(),
&["add", "node", "--summary", "revert-me", "--no-embed"],
)
.assert()
.success();
let log_out = mnem(dir.path(), &["log", "-n", "1"]).assert().success();
let stdout = String::from_utf8_lossy(&log_out.get_output().stdout).to_string();
let op_cid = stdout
.lines()
.find_map(|l| l.strip_prefix("op ").map(str::trim).map(str::to_string))
.expect("mnem log -n 1 must emit an 'op <cid>' line");
mnem(dir.path(), &["revert", &op_cid]).assert().success();
}