impl Drop for EnvGuard {
fn drop(&mut self) {
unsafe {
match self.previous.take() {
Some(value) => std::env::set_var(self.name, value),
None => std::env::remove_var(self.name),
}
}
}
}
impl Drop for CronShim {
fn drop(&mut self) {
unsafe {
match self.previous.take() {
Some(value) => std::env::set_var("MOADIM_CRONTAB_BIN", value),
None => std::env::remove_var("MOADIM_CRONTAB_BIN"),
}
}
let _ = std::fs::remove_dir_all(&self.base);
}
}
#[test]
fn read_crontab_returns_store_contents_on_success() {
let shim = CronShim::new(Some("0 0 * * * /bin/existing\n"));
let result = read_crontab().unwrap();
assert_eq!(result, "0 0 * * * /bin/existing\n");
drop(shim);
}
#[test]
fn read_crontab_empty_when_no_crontab() {
let shim = CronShim::new(None);
let result = read_crontab().unwrap();
assert_eq!(result, "");
drop(shim);
}
#[test]
fn read_crontab_errors_on_non_no_crontab_failure() {
let shim = CronShim::failing();
let err = read_crontab().unwrap_err();
match err {
SyncError::CrontabCommand(msg) => assert!(msg.contains("boom"), "unexpected msg: {msg}"),
SyncError::Io(io) => panic!("expected CrontabCommand, got Io({io})"),
}
drop(shim);
}
#[test]
fn write_crontab_persists_content_on_success() {
let shim = CronShim::new(Some(""));
write_crontab("hello # moadim-routine:z\n").unwrap();
assert_eq!(shim.store_contents(), "hello # moadim-routine:z\n");
drop(shim);
}
#[test]
fn write_crontab_errors_on_non_success_exit() {
let shim = CronShim::failing();
let err = write_crontab("anything\n").unwrap_err();
match err {
SyncError::CrontabCommand(msg) => assert!(
msg.contains("exited with"),
"expected exit message, got: {msg}"
),
SyncError::Io(io) => panic!("expected CrontabCommand, got Io({io})"),
}
drop(shim);
}
#[allow(
clippy::missing_docs_in_private_items,
reason = "split-out module keeps the file under the linecheck limit"
)]
#[path = "write_crontab_errors_instead_of_panicking_on_broken_pipe_tests.rs"]
mod write_crontab_errors_instead_of_panicking_on_broken_pipe_tests;