impl FailingCronShim {
fn new() -> Self {
use std::os::unix::fs::PermissionsExt;
let base = std::env::temp_dir().join(format!("moadim-fcshim-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&base).unwrap();
let script = base.join("crontab-fail.sh");
std::fs::write(&script, "#!/bin/sh\nexit 1\n").unwrap();
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
let previous = std::env::var_os("MOADIM_CRONTAB_BIN");
unsafe {
std::env::set_var("MOADIM_CRONTAB_BIN", &script);
}
Self { base, previous }
}
}
impl Drop for FailingCronShim {
fn drop(&mut self) {
unsafe {
match self.previous.take() {
Some(val) => std::env::set_var("MOADIM_CRONTAB_BIN", val),
None => std::env::remove_var("MOADIM_CRONTAB_BIN"),
}
}
let _ = std::fs::remove_dir_all(&self.base);
}
}
#[test]
fn unlock_routines_succeeds_when_crontab_sync_passes() {
let _home = TempHome::set();
let _shim = SucceedingCronShim::new();
let handler = make_handler();
let result = handler
.unlock_routines(Parameters(UnlockRoutinesInput {
scope: "all".into(),
}))
.unwrap();
assert!(!result.is_error.unwrap_or(false));
}
#[test]
fn unlock_routines_logs_warn_when_crontab_sync_fails() {
let _home = TempHome::set();
let _shim = FailingCronShim::new();
let handler = make_handler();
let result = handler
.unlock_routines(Parameters(UnlockRoutinesInput {
scope: "all".into(),
}))
.unwrap();
assert!(!result.is_error.unwrap_or(false));
}
#[test]
fn unlock_routines_returns_error_when_set_lock_fails() {
let dir = std::env::temp_dir().join(format!("moadim-unlockfail-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(dir.join(".config").join("moadim")).unwrap();
unsafe {
std::env::set_var("MOADIM_HOME_OVERRIDE", &dir);
}
let lock_path = crate::paths::global_lock_path();
std::fs::create_dir_all(&lock_path).unwrap();
let handler = make_handler();
let result = handler
.unlock_routines(Parameters(UnlockRoutinesInput {
scope: "shared".into(),
}))
.unwrap();
assert!(result.is_error.unwrap_or(false));
unsafe {
std::env::remove_var("MOADIM_HOME_OVERRIDE");
}
let _ = std::fs::remove_dir_all(&dir);
}