forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

follow.rs112 lines, 3.9 KiBHistoryRawDownload
1//! Live follow: the decision on a project file another writer saved, the stamp
2//! that says when a file is settled, and the menu action that reloads it.
3
4use super::*;
5use crate::follow::{Follow, Stamp, follow_decision, stamp};
6use std::time::{Duration, SystemTime};
7
8fn at(secs: u64, len: u64) -> Stamp {
9 Stamp {
10 mtime: SystemTime::UNIX_EPOCH + Duration::from_secs(secs),
11 len,
12 }
13}
14
15/// The table in `follow.rs`, row for row.
16#[test]
17fn follow_decision_table() {
18 let known = Some(at(10, 100));
19 // Nothing settled to read: wait, whatever the window holds.
20 assert_eq!(follow_decision(known, None, false), Follow::Wait);
21 assert_eq!(follow_decision(known, None, true), Follow::Wait);
22 assert_eq!(follow_decision(None, None, false), Follow::Wait);
23 // No stamp yet: take the file as it is, silently.
24 assert_eq!(
25 follow_decision(None, Some(at(10, 100)), false),
26 Follow::Adopt
27 );
28 assert_eq!(
29 follow_decision(None, Some(at(10, 100)), true),
30 Follow::Adopt
31 );
32 // Unchanged: nothing, edits or no edits.
33 assert_eq!(
34 follow_decision(known, Some(at(10, 100)), false),
35 Follow::Idle
36 );
37 assert_eq!(
38 follow_decision(known, Some(at(10, 100)), true),
39 Follow::Idle
40 );
41 // Changed with no edits held: reload.
42 assert_eq!(
43 follow_decision(known, Some(at(11, 100)), false),
44 Follow::Reload
45 );
46 assert_eq!(
47 follow_decision(known, Some(at(10, 101)), false),
48 Follow::Reload
49 );
50 // Changed under edits held: ask.
51 assert_eq!(follow_decision(known, Some(at(11, 100)), true), Follow::Ask);
52 assert_eq!(follow_decision(known, Some(at(10, 101)), true), Follow::Ask);
53}
54
55/// A same-length rewrite in the same second is still a change: the mtime is
56/// what moves, and the length alone must not hide it.
57#[test]
58fn same_length_rewrite_in_the_same_second_is_a_change_only_by_mtime() {
59 let known = Some(at(10, 100));
60 let later = Stamp {
61 mtime: at(10, 100).mtime + Duration::from_millis(250),
62 len: 100,
63 };
64 assert_eq!(follow_decision(known, Some(later), false), Follow::Reload);
65}
66
67/// A settled file stamps; a file the engine is still writing, or that is
68/// empty, or that is gone, does not.
69#[test]
70fn stamp_only_reads_a_settled_file() {
71 let dir = std::env::temp_dir().join(format!("edith-follow-test-{}", std::process::id()));
72 std::fs::create_dir_all(&dir).unwrap();
73 let project = dir.join("cut.edith");
74
75 assert_eq!(stamp(&project), None, "a missing file has no stamp");
76 assert_eq!(stamp(std::path::Path::new("")), None, "no path, no stamp");
77
78 std::fs::write(&project, b"edith-project").unwrap();
79 let settled = stamp(&project).expect("a written file stamps");
80 assert_eq!(settled.len, 13);
81
82 // The engine writes `<path>.part` and renames it over the file: while the
83 // sibling exists the file is not final, whatever it holds.
84 let part = dir.join("cut.edith.part");
85 std::fs::write(&part, b"partial").unwrap();
86 assert_eq!(
87 stamp(&project),
88 None,
89 "a file with a .part beside it is mid-write"
90 );
91 std::fs::remove_file(&part).unwrap();
92 assert_eq!(stamp(&project), Some(settled));
93
94 std::fs::write(&project, b"").unwrap();
95 assert_eq!(stamp(&project), None, "an empty file is not settled");
96
97 let _ = std::fs::remove_dir_all(&dir);
98}
99
100/// The menu and palette action: registered, named, with no stroke, filed under
101/// File. The keys card lists it from [`ActionId::ALL`].
102#[test]
103fn reload_project_is_a_menu_action_with_no_stroke() {
104 assert!(ActionId::ALL.contains(&ActionId::ReloadProject));
105 assert_eq!(ActionId::ReloadProject.label(), "Reload project from disk");
106 let keys = crate::keymap::Keymap::defaults();
107 assert_eq!(keys.chord(ActionId::ReloadProject), "--");
108 assert_eq!(
109 ActionId::ReloadProject.category(),
110 crate::keymap::Category::File
111 );
112}