use std::path::Path;
use super::*;
#[test]
fn an_absent_plist_never_blocks_an_install() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(PLIST_FILE_NAME);
assert!(guard_managed(&path, false, "overwrite").is_ok());
}
#[test]
fn a_plist_we_wrote_may_be_overwritten_and_removed() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(PLIST_FILE_NAME);
let rendered = plist::render_plist(&RenderParams {
scope: Scope::System,
exec_path: Path::new("/opt/all-smi/bin/all-smi"),
log_path: Path::new("/var/log/all-smi/all-smi.log"),
service_user: None,
})
.unwrap();
fs::write(&path, &rendered).unwrap();
assert!(guard_managed(&path, false, "overwrite").is_ok());
assert!(guard_managed(&path, false, "remove").is_ok());
}
#[test]
fn a_foreign_plist_is_refused_and_the_message_says_how_to_proceed() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(PLIST_FILE_NAME);
fs::write(&path, "<plist version=\"1.0\"><dict/></plist>\n").unwrap();
let err = guard_managed(&path, false, "remove").expect_err("a foreign plist must be refused");
assert!(matches!(err, ServiceError::Conflict(_)));
let msg = err.to_string();
assert!(msg.contains("--force"), "must name the override: {msg}");
assert!(msg.contains(plist::MANAGED_MARKER), "must quote the marker");
assert!(guard_managed(&path, true, "remove").is_ok());
}
#[test]
fn a_written_plist_is_not_group_or_world_writable() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(PLIST_FILE_NAME);
write_plist(&path, "<plist/>\n").unwrap();
assert_eq!(fs::read_to_string(&path).unwrap(), "<plist/>\n");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o644, "expected 0644, got {mode:o}");
}
}
#[test]
fn writing_leaves_no_temporary_behind_and_is_idempotent() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(PLIST_FILE_NAME);
write_plist(&path, "<plist>first</plist>\n").unwrap();
write_plist(&path, "<plist>second</plist>\n").unwrap();
assert_eq!(
fs::read_to_string(&path).unwrap(),
"<plist>second</plist>\n"
);
let entries: Vec<String> = fs::read_dir(dir.path())
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(entries, vec![PLIST_FILE_NAME.to_string()]);
}
#[test]
fn the_log_directory_is_created_readable() {
let dir = tempfile::tempdir().unwrap();
let log = dir.path().join("all-smi").join("all-smi.log");
create_log_dir(&log).unwrap();
let parent = log.parent().unwrap();
assert!(parent.is_dir());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = fs::metadata(parent).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o755, "expected 0755, got {mode:o}");
}
create_log_dir(&log).unwrap();
}