diff options
Diffstat (limited to 'crates/ra_vfs/tests/vfs.rs')
-rw-r--r-- | crates/ra_vfs/tests/vfs.rs | 222 |
1 files changed, 0 insertions, 222 deletions
diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs deleted file mode 100644 index 200a03e54..000000000 --- a/crates/ra_vfs/tests/vfs.rs +++ /dev/null | |||
@@ -1,222 +0,0 @@ | |||
1 | use std::{collections::HashSet, fs, time::Duration}; | ||
2 | |||
3 | // use flexi_logger::Logger; | ||
4 | use crossbeam_channel::RecvTimeoutError; | ||
5 | use ra_vfs::{Vfs, VfsChange}; | ||
6 | use tempfile::tempdir; | ||
7 | |||
8 | /// Processes exactly `num_tasks` events waiting in the `vfs` message queue. | ||
9 | /// | ||
10 | /// Panics if there are not exactly that many tasks enqueued for processing. | ||
11 | fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { | ||
12 | process_tasks_in_range(vfs, num_tasks, num_tasks); | ||
13 | } | ||
14 | |||
15 | /// Processes up to `max_count` events waiting in the `vfs` message queue. | ||
16 | /// | ||
17 | /// Panics if it cannot process at least `min_count` events. | ||
18 | /// Panics if more than `max_count` events are enqueued for processing. | ||
19 | fn process_tasks_in_range(vfs: &mut Vfs, min_count: u32, max_count: u32) { | ||
20 | for i in 0..max_count { | ||
21 | let task = match vfs.task_receiver().recv_timeout(Duration::from_secs(3)) { | ||
22 | Err(RecvTimeoutError::Timeout) if i >= min_count => return, | ||
23 | otherwise => otherwise.unwrap(), | ||
24 | }; | ||
25 | log::debug!("{:?}", task); | ||
26 | vfs.handle_task(task); | ||
27 | } | ||
28 | assert!(vfs.task_receiver().is_empty()); | ||
29 | } | ||
30 | |||
31 | macro_rules! assert_match { | ||
32 | ($x:expr, $pat:pat) => { | ||
33 | assert_match!($x, $pat, ()) | ||
34 | }; | ||
35 | ($x:expr, $pat:pat, $assert:expr) => { | ||
36 | match $x { | ||
37 | $pat => $assert, | ||
38 | x => assert!(false, "Expected {}, got {:?}", stringify!($pat), x), | ||
39 | }; | ||
40 | }; | ||
41 | } | ||
42 | |||
43 | #[test] | ||
44 | fn test_vfs_works() -> std::io::Result<()> { | ||
45 | // Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap(); | ||
46 | |||
47 | let files = [("a/foo.rs", "hello"), ("a/bar.rs", "world"), ("a/b/baz.rs", "nested hello")]; | ||
48 | |||
49 | let dir = tempdir().unwrap(); | ||
50 | for (path, text) in files.iter() { | ||
51 | let file_path = dir.path().join(path); | ||
52 | fs::create_dir_all(file_path.parent().unwrap()).unwrap(); | ||
53 | fs::write(file_path, text)? | ||
54 | } | ||
55 | |||
56 | let a_root = dir.path().join("a"); | ||
57 | let b_root = dir.path().join("a/b"); | ||
58 | |||
59 | let (mut vfs, _) = Vfs::new(vec![a_root, b_root]); | ||
60 | process_tasks(&mut vfs, 2); | ||
61 | { | ||
62 | let files = vfs | ||
63 | .commit_changes() | ||
64 | .into_iter() | ||
65 | .flat_map(|change| { | ||
66 | let files = match change { | ||
67 | VfsChange::AddRoot { files, .. } => files, | ||
68 | _ => panic!("unexpected change"), | ||
69 | }; | ||
70 | files.into_iter().map(|(_id, path, text)| { | ||
71 | let text: String = (&*text).clone(); | ||
72 | (format!("{}", path.display()), text) | ||
73 | }) | ||
74 | }) | ||
75 | .collect::<HashSet<_>>(); | ||
76 | |||
77 | let expected_files = [("foo.rs", "hello"), ("bar.rs", "world"), ("baz.rs", "nested hello")] | ||
78 | .iter() | ||
79 | .map(|(path, text)| (path.to_string(), text.to_string())) | ||
80 | .collect::<HashSet<_>>(); | ||
81 | |||
82 | assert_eq!(files, expected_files); | ||
83 | } | ||
84 | |||
85 | // rust-analyzer#734: fsevents has a bunch of events still sitting around. | ||
86 | process_tasks_in_range(&mut vfs, 0, if cfg!(target_os = "macos") { 7 } else { 0 }); | ||
87 | match vfs.commit_changes().as_slice() { | ||
88 | [] => {} | ||
89 | |||
90 | // This arises on fsevents (unless we wait 30 seconds before | ||
91 | // calling `Vfs::new` above). We need to churn through these | ||
92 | // events so that we can focus on the event that arises from | ||
93 | // the `fs::write` below. | ||
94 | [VfsChange::ChangeFile { .. }, // hello | ||
95 | VfsChange::ChangeFile { .. }, // world | ||
96 | VfsChange::AddFile { .. }, // b/baz.rs, nested hello | ||
97 | VfsChange::ChangeFile { .. }, // hello | ||
98 | VfsChange::ChangeFile { .. }, // world | ||
99 | VfsChange::ChangeFile { .. }, // nested hello | ||
100 | VfsChange::ChangeFile { .. }, // nested hello | ||
101 | ] => {} | ||
102 | |||
103 | changes => panic!("Expected events for setting up initial files, got: {GOT:?}", | ||
104 | GOT=changes), | ||
105 | } | ||
106 | |||
107 | fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); | ||
108 | process_tasks(&mut vfs, 1); | ||
109 | assert_match!( | ||
110 | vfs.commit_changes().as_slice(), | ||
111 | [VfsChange::ChangeFile { text, .. }], | ||
112 | assert_eq!(text.as_str(), "quux") | ||
113 | ); | ||
114 | |||
115 | vfs.add_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); | ||
116 | assert_match!( | ||
117 | vfs.commit_changes().as_slice(), | ||
118 | [VfsChange::ChangeFile { text, .. }], | ||
119 | assert_eq!(text.as_str(), "m") | ||
120 | ); | ||
121 | |||
122 | // changing file on disk while overlayed doesn't generate a VfsChange | ||
123 | fs::write(&dir.path().join("a/b/baz.rs"), "corge").unwrap(); | ||
124 | process_tasks(&mut vfs, 1); | ||
125 | assert_match!(vfs.commit_changes().as_slice(), []); | ||
126 | |||
127 | // removing overlay restores data on disk | ||
128 | vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); | ||
129 | assert_match!( | ||
130 | vfs.commit_changes().as_slice(), | ||
131 | [VfsChange::ChangeFile { text, .. }], | ||
132 | assert_eq!(text.as_str(), "corge") | ||
133 | ); | ||
134 | |||
135 | vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); | ||
136 | assert_match!(vfs.commit_changes().as_slice(), [VfsChange::AddFile { text, path, .. }], { | ||
137 | assert_eq!(text.as_str(), "spam"); | ||
138 | assert_eq!(path, "spam.rs"); | ||
139 | }); | ||
140 | |||
141 | vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); | ||
142 | assert_match!( | ||
143 | vfs.commit_changes().as_slice(), | ||
144 | [VfsChange::RemoveFile { path, .. }], | ||
145 | assert_eq!(path, "spam.rs") | ||
146 | ); | ||
147 | |||
148 | fs::create_dir_all(dir.path().join("a/sub1/sub2")).unwrap(); | ||
149 | fs::write(dir.path().join("a/sub1/sub2/new.rs"), "new hello").unwrap(); | ||
150 | process_tasks(&mut vfs, 1); | ||
151 | assert_match!(vfs.commit_changes().as_slice(), [VfsChange::AddFile { text, path, .. }], { | ||
152 | assert_eq!(text.as_str(), "new hello"); | ||
153 | assert_eq!(path, "sub1/sub2/new.rs"); | ||
154 | }); | ||
155 | |||
156 | fs::rename(&dir.path().join("a/sub1/sub2/new.rs"), &dir.path().join("a/sub1/sub2/new1.rs")) | ||
157 | .unwrap(); | ||
158 | |||
159 | // rust-analyzer#734: For testing purposes, work-around | ||
160 | // passcod/notify#181 by processing either 1 or 2 events. (In | ||
161 | // particular, Mac can hand back either 1 or 2 events in a | ||
162 | // timing-dependent fashion.) | ||
163 | // | ||
164 | // rust-analyzer#827: Windows generates extra `Write` events when | ||
165 | // renaming? meaning we have extra tasks to process. | ||
166 | process_tasks_in_range(&mut vfs, 1, if cfg!(windows) { 4 } else { 2 }); | ||
167 | match vfs.commit_changes().as_slice() { | ||
168 | [VfsChange::RemoveFile { path: removed_path, .. }, VfsChange::AddFile { text, path: added_path, .. }] => | ||
169 | { | ||
170 | assert_eq!(removed_path, "sub1/sub2/new.rs"); | ||
171 | assert_eq!(added_path, "sub1/sub2/new1.rs"); | ||
172 | assert_eq!(text.as_str(), "new hello"); | ||
173 | } | ||
174 | |||
175 | // Hopefully passcod/notify#181 will be addressed in some | ||
176 | // manner that will reliably emit an event mentioning | ||
177 | // `sub1/sub2/new.rs`. But until then, must accept that | ||
178 | // debouncing loses information unrecoverably. | ||
179 | [VfsChange::AddFile { text, path: added_path, .. }] => { | ||
180 | assert_eq!(added_path, "sub1/sub2/new1.rs"); | ||
181 | assert_eq!(text.as_str(), "new hello"); | ||
182 | } | ||
183 | |||
184 | changes => panic!( | ||
185 | "Expected events for rename of {OLD} to {NEW}, got: {GOT:?}", | ||
186 | OLD = "sub1/sub2/new.rs", | ||
187 | NEW = "sub1/sub2/new1.rs", | ||
188 | GOT = changes | ||
189 | ), | ||
190 | } | ||
191 | |||
192 | fs::remove_file(&dir.path().join("a/sub1/sub2/new1.rs")).unwrap(); | ||
193 | process_tasks(&mut vfs, 1); | ||
194 | assert_match!( | ||
195 | vfs.commit_changes().as_slice(), | ||
196 | [VfsChange::RemoveFile { path, .. }], | ||
197 | assert_eq!(path, "sub1/sub2/new1.rs") | ||
198 | ); | ||
199 | |||
200 | { | ||
201 | vfs.add_file_overlay(&dir.path().join("a/memfile.rs"), "memfile".to_string()); | ||
202 | assert_match!( | ||
203 | vfs.commit_changes().as_slice(), | ||
204 | [VfsChange::AddFile { text, .. }], | ||
205 | assert_eq!(text.as_str(), "memfile") | ||
206 | ); | ||
207 | fs::write(&dir.path().join("a/memfile.rs"), "ignore me").unwrap(); | ||
208 | process_tasks(&mut vfs, 1); | ||
209 | assert_match!(vfs.commit_changes().as_slice(), []); | ||
210 | } | ||
211 | |||
212 | // should be ignored | ||
213 | fs::create_dir_all(dir.path().join("a/target")).unwrap(); | ||
214 | fs::write(&dir.path().join("a/target/new.rs"), "ignore me").unwrap(); | ||
215 | |||
216 | assert_match!( | ||
217 | vfs.task_receiver().recv_timeout(Duration::from_millis(300)), // slightly more than watcher debounce delay | ||
218 | Err(RecvTimeoutError::Timeout) | ||
219 | ); | ||
220 | |||
221 | Ok(()) | ||
222 | } | ||