Cotton code
132 lines · 4.8 KB · RustRaw
//! The self-watch decision, over a file a test owns.use super::*;fn print(size: u64, modified_secs: i64) -> Fingerprint {    Fingerprint {        size,        modified_secs,    }}#[test]fn an_unchanged_executable_is_kept_however_many_sessions_are_running() {    let same = print(1024, 1_758_000_000);    assert_eq!(verdict(Some(same), Some(same), 0), Verdict::Keep);    assert_eq!(verdict(Some(same), Some(same), 7), Verdict::Keep);}#[test]fn a_replaced_executable_with_no_session_running_exits_so_launchd_restarts_it() {    let before = print(1024, 1_758_000_000);    let after = print(2048, 1_758_600_000);    assert_eq!(verdict(Some(before), Some(after), 0), Verdict::Exit);}#[test]fn a_replaced_executable_defers_while_a_session_is_alive() {    let before = print(1024, 1_758_000_000);    let after = print(1024, 1_758_600_000);    assert_eq!(verdict(Some(before), Some(after), 1), Verdict::Defer);    assert_eq!(verdict(Some(before), Some(after), 12), Verdict::Defer);}#[test]fn a_same_sized_rebuild_is_still_a_replacement() {    // The size alone would miss it: a rebuild of the same source at the same    // version is byte-for-byte the same length surprisingly often.    let before = print(4_823_104, 1_758_000_000);    let after = print(4_823_104, 1_758_600_001);    assert_eq!(verdict(Some(before), Some(after), 0), Verdict::Exit);}#[test]fn an_executable_that_vanished_mid_update_counts_as_replaced() {    let before = print(1024, 1_758_000_000);    assert_eq!(verdict(Some(before), None, 0), Verdict::Exit);    assert_eq!(verdict(Some(before), None, 3), Verdict::Defer);}#[test]fn a_helper_that_could_not_fingerprint_itself_never_exits_on_a_guess() {    let after = print(2048, 1_758_600_000);    assert_eq!(verdict(None, Some(after), 0), Verdict::Keep);    assert_eq!(verdict(None, None, 0), Verdict::Keep);}#[test]fn the_short_form_is_two_hex_numbers_and_differs_when_either_half_does() {    assert_eq!(print(255, 16).short(), "ff-10");    assert_ne!(print(255, 16).short(), print(255, 17).short());    assert_ne!(print(255, 16).short(), print(256, 16).short());}#[test]fn a_watch_reads_the_file_it_was_pointed_at_and_reports_its_fingerprint() {    let dir = tempfile::tempdir().expect("a temp dir");    let path = dir.path().join("cotton-session-host");    std::fs::write(&path, b"the first build").expect("it writes");    let watch = Watch::of(&path);    assert!(!watch.fingerprint().is_empty(), "the file was there");    assert!(!watch.update_pending());    assert_eq!(watch.check(0), Verdict::Keep);    assert_eq!(watch.report().fingerprint, watch.fingerprint());}#[test]fn a_watch_over_a_replaced_file_defers_while_sessions_run_then_exits() {    let dir = tempfile::tempdir().expect("a temp dir");    let path = dir.path().join("cotton-session-host");    std::fs::write(&path, b"the first build").expect("it writes");    let watch = Watch::of(&path);    // A different LENGTH, so the verdict does not depend on a filesystem's    // modification-time resolution.    std::fs::write(&path, b"the second build, which is longer").expect("it writes");    assert_eq!(watch.check(2), Verdict::Defer);    assert!(watch.update_pending(), "a deferral is in the sample");    assert!(watch.report().update_pending);    assert_eq!(watch.check(0), Verdict::Exit);    // An exit is not a pending restart: the process is going now.    assert!(!watch.update_pending());}#[test]fn a_watch_over_nothing_keeps_and_reports_no_fingerprint() {    let watch = Watch::unwatched();    assert_eq!(watch.check(0), Verdict::Keep);    assert_eq!(watch.fingerprint(), "");    assert!(!watch.update_pending());}#[test]fn a_host_that_cannot_read_its_own_executable_says_it_will_never_restart_itself() {    let said = Watch::unwatched().line(Verdict::Keep, 0);    assert!(said.contains("cannot read its own executable"), "{said}");    assert!(said.contains("will not restart itself"), "{said}");}#[test]fn a_host_that_can_names_the_file_and_its_fingerprint() {    let dir = tempfile::tempdir().expect("a temp dir");    let path = dir.path().join("cotton-session-host");    std::fs::write(&path, b"a build").expect("it writes");    let watch = Watch::of(&path);    let said = watch.line(Verdict::Keep, 0);    assert!(said.contains("cotton-session-host"), "{said}");    assert!(said.contains(&watch.fingerprint()), "{said}");    assert!(said.contains("unchanged"), "{said}");}#[test]fn every_verdict_says_what_it_decided_and_they_are_three_different_lines() {    let watch = Watch::unwatched();    let keep = watch.line(Verdict::Keep, 0);    let exit = watch.line(Verdict::Exit, 0);    let defer = watch.line(Verdict::Defer, 2);    assert!(defer.contains('2'), "the deferral names the sessions — {defer}");    assert_ne!(keep, exit);    assert_ne!(exit, defer);    assert_ne!(keep, defer);}