use std::path::Path;
use std::process::Command;
use assert_cmd::prelude::*;
use tempfile::TempDir;
fn mnem_with_home(repo: &Path, fake_home: &Path, args: &[&str]) -> Command {
let mut cmd = Command::cargo_bin("mnem").expect("built mnem binary");
cmd.current_dir(repo);
cmd.arg("-R").arg(repo);
#[cfg(unix)]
cmd.env("HOME", fake_home);
#[cfg(windows)]
cmd.env("USERPROFILE", fake_home);
for a in args {
cmd.arg(a);
}
cmd
}
#[test]
fn global_set_writes_home_mnem_config() {
let repo_dir = TempDir::new().unwrap();
let fake_home = TempDir::new().unwrap();
mnem_with_home(
repo_dir.path(),
fake_home.path(),
&["init", repo_dir.path().to_str().unwrap()],
)
.assert()
.success();
let out = mnem_with_home(
repo_dir.path(),
fake_home.path(),
&["config", "--global", "set", "user.name", "Alice"],
)
.assert()
.success();
let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
assert!(
stdout.contains("user.name = Alice"),
"expected 'user.name = Alice' in output, got: {stdout}"
);
let cfg_path = fake_home.path().join(".mnem").join("config.toml");
assert!(
cfg_path.exists(),
"expected ~/.mnem/config.toml to be created, but it is absent"
);
let repo_cfg_path = repo_dir.path().join(".mnem").join("config.toml");
if repo_cfg_path.exists() {
let content = std::fs::read_to_string(&repo_cfg_path).unwrap();
assert!(
!content.contains("Alice"),
"per-repo config must not contain the global-only value 'Alice'"
);
}
}
#[test]
fn global_get_reads_home_mnem_config() {
let repo_dir = TempDir::new().unwrap();
let fake_home = TempDir::new().unwrap();
mnem_with_home(
repo_dir.path(),
fake_home.path(),
&["init", repo_dir.path().to_str().unwrap()],
)
.assert()
.success();
let home_mnem = fake_home.path().join(".mnem");
std::fs::create_dir_all(&home_mnem).unwrap();
std::fs::write(
home_mnem.join("config.toml"),
"[user]\nname = \"GlobalUser\"\n",
)
.unwrap();
let out = mnem_with_home(
repo_dir.path(),
fake_home.path(),
&["config", "-g", "get", "user.name"],
)
.assert()
.success();
let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
assert!(
stdout.trim() == "GlobalUser",
"expected 'GlobalUser' from global config, got: {stdout}"
);
}
#[test]
fn global_list_works_without_per_repo_config() {
let repo_dir = TempDir::new().unwrap();
let fake_home = TempDir::new().unwrap();
mnem_with_home(
repo_dir.path(),
fake_home.path(),
&["init", repo_dir.path().to_str().unwrap()],
)
.assert()
.success();
let home_mnem = fake_home.path().join(".mnem");
std::fs::create_dir_all(&home_mnem).unwrap();
std::fs::write(
home_mnem.join("config.toml"),
"[user]\nemail = \"g@example.com\"\n",
)
.unwrap();
let out = mnem_with_home(
repo_dir.path(),
fake_home.path(),
&["config", "--global", "list"],
)
.assert()
.success();
let stdout = String::from_utf8_lossy(&out.get_output().stdout).to_string();
assert!(
stdout.contains("user.email = g@example.com"),
"expected 'user.email = g@example.com' in global list output, got: {stdout}"
);
}