use std::path::PathBuf;
use tempfile::TempDir;
use tix::assignee::{AuthorIdentity, derive_segment, git_identity_for};
const CONFIGURED_GIT: &str = "#!/bin/sh\ncase \"$3\" in\n\"user.name\") printf '%s\\n' 'Herbert Maschke';;\n\"user.email\") printf '%s\\n' 'thyseus@pm.me';;\n*) exit 1;;\nesac\n";
const UNCONFIGURED_GIT: &str = "#!/bin/sh\nexit 1\n";
fn write_fake_git(dir: &TempDir, body: &str) -> PathBuf {
use std::os::unix::fs::PermissionsExt;
let path = dir.path().join("git");
std::fs::write(&path, body).unwrap();
let mut perms = std::fs::metadata(&path).unwrap().permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&path, perms).unwrap();
path
}
#[test]
fn configured_identity_yields_expected_author_and_full_string() {
let tmp = TempDir::new().unwrap();
let git = write_fake_git(&tmp, CONFIGURED_GIT);
let id: AuthorIdentity =
git_identity_for(git.to_str().unwrap()).expect("fake git must yield an identity");
assert_eq!(id.name, "Herbert Maschke");
assert_eq!(id.email.as_deref(), Some("thyseus@pm.me"));
assert_eq!(id.to_string(), "Herbert Maschke <thyseus@pm.me>");
}
#[test]
fn git_missing_from_path_is_none() {
let tmp = TempDir::new().unwrap();
assert_eq!(
git_identity_for(tmp.path().join("git").to_str().unwrap()),
None
);
}
#[test]
fn unconfigured_user_name_is_none() {
let tmp = TempDir::new().unwrap();
let git = write_fake_git(&tmp, UNCONFIGURED_GIT);
assert_eq!(git_identity_for(git.to_str().unwrap()), None);
}
#[test]
fn full_round_trip_derives_herbert_maschke_segment() {
let tmp = TempDir::new().unwrap();
let git = write_fake_git(&tmp, CONFIGURED_GIT);
let identity =
git_identity_for(git.to_str().unwrap()).expect("fake git must yield an identity");
let display = identity.to_string();
assert_eq!(derive_segment(Some(display.as_str())), "herbert-maschke");
}