aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_vfs/tests/vfs.rs74
1 files changed, 67 insertions, 7 deletions
diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs
index c76e6ea26..200a03e54 100644
--- a/crates/ra_vfs/tests/vfs.rs
+++ b/crates/ra_vfs/tests/vfs.rs
@@ -5,12 +5,27 @@ use crossbeam_channel::RecvTimeoutError;
5use ra_vfs::{Vfs, VfsChange}; 5use ra_vfs::{Vfs, VfsChange};
6use tempfile::tempdir; 6use tempfile::tempdir;
7 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.
8fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { 11fn process_tasks(vfs: &mut Vfs, num_tasks: u32) {
9 for _ in 0..num_tasks { 12 process_tasks_in_range(vfs, num_tasks, num_tasks);
10 let task = vfs.task_receiver().recv_timeout(Duration::from_secs(3)).unwrap(); 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.
19fn 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 };
11 log::debug!("{:?}", task); 25 log::debug!("{:?}", task);
12 vfs.handle_task(task); 26 vfs.handle_task(task);
13 } 27 }
28 assert!(vfs.task_receiver().is_empty());
14} 29}
15 30
16macro_rules! assert_match { 31macro_rules! assert_match {
@@ -67,6 +82,28 @@ fn test_vfs_works() -> std::io::Result<()> {
67 assert_eq!(files, expected_files); 82 assert_eq!(files, expected_files);
68 } 83 }
69 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
70 fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); 107 fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap();
71 process_tasks(&mut vfs, 1); 108 process_tasks(&mut vfs, 1);
72 assert_match!( 109 assert_match!(
@@ -118,16 +155,39 @@ fn test_vfs_works() -> std::io::Result<()> {
118 155
119 fs::rename(&dir.path().join("a/sub1/sub2/new.rs"), &dir.path().join("a/sub1/sub2/new1.rs")) 156 fs::rename(&dir.path().join("a/sub1/sub2/new.rs"), &dir.path().join("a/sub1/sub2/new1.rs"))
120 .unwrap(); 157 .unwrap();
121 process_tasks(&mut vfs, 2); 158
122 assert_match!( 159 // rust-analyzer#734: For testing purposes, work-around
123 vfs.commit_changes().as_slice(), 160 // passcod/notify#181 by processing either 1 or 2 events. (In
124 [VfsChange::RemoveFile { path: removed_path, .. }, VfsChange::AddFile { text, path: added_path, .. }], 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, .. }] =>
125 { 169 {
126 assert_eq!(removed_path, "sub1/sub2/new.rs"); 170 assert_eq!(removed_path, "sub1/sub2/new.rs");
127 assert_eq!(added_path, "sub1/sub2/new1.rs"); 171 assert_eq!(added_path, "sub1/sub2/new1.rs");
128 assert_eq!(text.as_str(), "new hello"); 172 assert_eq!(text.as_str(), "new hello");
129 } 173 }
130 ); 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 }
131 191
132 fs::remove_file(&dir.path().join("a/sub1/sub2/new1.rs")).unwrap(); 192 fs::remove_file(&dir.path().join("a/sub1/sub2/new1.rs")).unwrap();
133 process_tasks(&mut vfs, 1); 193 process_tasks(&mut vfs, 1);