use super::*;
fn config(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
return pairs
.iter()
.map(|(key, value)| return (String::from(*key), String::from(*value)))
.collect();
}
const DETACHED: &[&str] = &["maintenance", "run", "--auto", "--quiet", "--detach"];
const FOREGROUND: &[&str] = &["maintenance", "run", "--auto", "--quiet", "--no-detach"];
#[test]
fn config_records_keep_the_last_value_and_read_a_bare_key_as_true() {
let parsed: BTreeMap<String, String> = parse_config_records(
b"gc.auto\n1024\0maintenance.autodetach\ntrue\0maintenance.autodetach\nfalse\0gc.autodetach\0",
)
.expect("ASCII records");
assert_eq!(parsed, config(&[
("gc.auto", "1024"),
("maintenance.autodetach", "false"),
("gc.autodetach", "true"),
]));
}
#[test]
fn empty_config_output_parses_to_no_keys() {
assert_eq!(parse_config_records(b"").expect("empty"), BTreeMap::new());
assert_eq!(parse_config_records(b"\0\0").expect("only separators"), BTreeMap::new());
}
#[test]
fn config_records_that_are_not_utf8_are_refused() {
assert_eq!(
parse_config_records(b"gc.auto\n\xff\0"),
Err(MaintenanceError::ConfigNotUtf8)
);
}
#[test]
fn unset_configuration_detaches() {
assert_eq!(
auto_maintenance_arguments(&config(&[])),
MaintenanceDecision::Run(DETACHED.to_vec())
);
}
#[test]
fn explicit_maintenance_auto_false_or_zero_disables() {
assert_eq!(
auto_maintenance_arguments(&config(&[("maintenance.auto", "false")])),
MaintenanceDecision::Disabled
);
assert_eq!(
auto_maintenance_arguments(&config(&[("maintenance.auto", "0")])),
MaintenanceDecision::Disabled
);
}
#[test]
fn explicit_maintenance_auto_overrides_gc_auto() {
assert_eq!(
auto_maintenance_arguments(&config(&[("maintenance.auto", "true"), ("gc.auto", "0")])),
MaintenanceDecision::Run(DETACHED.to_vec())
);
}
#[test]
fn non_positive_gc_auto_disables() {
assert_eq!(
auto_maintenance_arguments(&config(&[("gc.auto", "0")])),
MaintenanceDecision::Disabled
);
assert_eq!(
auto_maintenance_arguments(&config(&[("gc.auto", "-1")])),
MaintenanceDecision::Disabled
);
}
#[test]
fn positive_gc_auto_enables() {
assert_eq!(
auto_maintenance_arguments(&config(&[("gc.auto", "6700")])),
MaintenanceDecision::Run(DETACHED.to_vec())
);
}
#[test]
fn boolean_gc_auto_disables() {
assert_eq!(
auto_maintenance_arguments(&config(&[("gc.auto", "true")])),
MaintenanceDecision::Disabled
);
assert_eq!(
auto_maintenance_arguments(&config(&[("gc.auto", "false")])),
MaintenanceDecision::Disabled
);
}
#[test]
fn unparsable_maintenance_auto_enables() {
assert_eq!(
auto_maintenance_arguments(&config(&[("maintenance.auto", "maybe")])),
MaintenanceDecision::Run(DETACHED.to_vec())
);
}
#[test]
fn maintenance_autodetach_false_runs_in_the_foreground() {
assert_eq!(
auto_maintenance_arguments(&config(&[("maintenance.autodetach", "false")])),
MaintenanceDecision::Run(FOREGROUND.to_vec())
);
}
#[test]
fn maintenance_autodetach_precedes_gc_autodetach() {
assert_eq!(
auto_maintenance_arguments(&config(&[
("maintenance.autodetach", "true"),
("gc.autodetach", "false"),
])),
MaintenanceDecision::Run(DETACHED.to_vec())
);
}
#[test]
fn gc_autodetach_false_runs_in_the_foreground() {
assert_eq!(
auto_maintenance_arguments(&config(&[("gc.autodetach", "false")])),
MaintenanceDecision::Run(FOREGROUND.to_vec())
);
}
#[test]
fn integer_detach_settings_are_true_when_nonzero() {
assert_eq!(
auto_maintenance_arguments(&config(&[("maintenance.autodetach", "2")])),
MaintenanceDecision::Run(DETACHED.to_vec())
);
assert_eq!(
auto_maintenance_arguments(&config(&[("maintenance.autodetach", "0")])),
MaintenanceDecision::Run(FOREGROUND.to_vec())
);
}