diff options
Diffstat (limited to 'crates/ra_vfs/tests/vfs.rs')
-rw-r--r-- | crates/ra_vfs/tests/vfs.rs | 131 |
1 files changed, 82 insertions, 49 deletions
diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index 8266a0bd5..71b25a5c9 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs | |||
@@ -11,9 +11,21 @@ fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { | |||
11 | } | 11 | } |
12 | } | 12 | } |
13 | 13 | ||
14 | macro_rules! assert_match { | ||
15 | ($x:expr, $pat:pat) => { | ||
16 | assert_match!($x, $pat, assert!(true)) | ||
17 | }; | ||
18 | ($x:expr, $pat:pat, $assert:expr) => { | ||
19 | match $x { | ||
20 | $pat => $assert, | ||
21 | x => assert!(false, "Expected {}, got {:?}", stringify!($pat), x), | ||
22 | }; | ||
23 | }; | ||
24 | } | ||
25 | |||
14 | #[test] | 26 | #[test] |
15 | fn test_vfs_works() -> std::io::Result<()> { | 27 | fn test_vfs_works() -> std::io::Result<()> { |
16 | // Logger::with_str("debug").start().unwrap(); | 28 | // Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap(); |
17 | 29 | ||
18 | let files = [ | 30 | let files = [ |
19 | ("a/foo.rs", "hello"), | 31 | ("a/foo.rs", "hello"), |
@@ -21,13 +33,16 @@ fn test_vfs_works() -> std::io::Result<()> { | |||
21 | ("a/b/baz.rs", "nested hello"), | 33 | ("a/b/baz.rs", "nested hello"), |
22 | ]; | 34 | ]; |
23 | 35 | ||
24 | let dir = tempdir()?; | 36 | let dir = tempdir().unwrap(); |
25 | for (path, text) in files.iter() { | 37 | for (path, text) in files.iter() { |
26 | let file_path = dir.path().join(path); | 38 | let file_path = dir.path().join(path); |
27 | fs::create_dir_all(file_path.parent().unwrap())?; | 39 | fs::create_dir_all(file_path.parent().unwrap()).unwrap(); |
28 | fs::write(file_path, text)? | 40 | fs::write(file_path, text)? |
29 | } | 41 | } |
30 | 42 | ||
43 | let gitignore = dir.path().join("a/.gitignore"); | ||
44 | fs::write(gitignore, "/target").unwrap(); | ||
45 | |||
31 | let a_root = dir.path().join("a"); | 46 | let a_root = dir.path().join("a"); |
32 | let b_root = dir.path().join("a/b"); | 47 | let b_root = dir.path().join("a/b"); |
33 | 48 | ||
@@ -62,79 +77,97 @@ fn test_vfs_works() -> std::io::Result<()> { | |||
62 | } | 77 | } |
63 | 78 | ||
64 | fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); | 79 | fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); |
65 | // 2 tasks per watcher change, first for HandleChange then for LoadChange | 80 | // 2 tasks per change, HandleChange and then LoadChange |
66 | process_tasks(&mut vfs, 2); | 81 | process_tasks(&mut vfs, 2); |
67 | match vfs.commit_changes().as_slice() { | 82 | assert_match!( |
68 | [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), | 83 | vfs.commit_changes().as_slice(), |
69 | xs => panic!("unexpected changes {:?}", xs), | 84 | [VfsChange::ChangeFile { text, .. }], |
70 | } | 85 | assert_eq!(text.as_str(), "quux") |
86 | ); | ||
71 | 87 | ||
72 | vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); | 88 | vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); |
73 | match vfs.commit_changes().as_slice() { | 89 | assert_match!( |
74 | [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "m"), | 90 | vfs.commit_changes().as_slice(), |
75 | xs => panic!("unexpected changes {:?}", xs), | 91 | [VfsChange::ChangeFile { text, .. }], |
76 | } | 92 | assert_eq!(text.as_str(), "m") |
93 | ); | ||
77 | 94 | ||
78 | // removing overlay restores data on disk | 95 | // removing overlay restores data on disk |
79 | vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); | 96 | vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); |
80 | match vfs.commit_changes().as_slice() { | 97 | assert_match!( |
81 | [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), | 98 | vfs.commit_changes().as_slice(), |
82 | xs => panic!("unexpected changes {:?}", xs), | 99 | [VfsChange::ChangeFile { text, .. }], |
83 | } | 100 | assert_eq!(text.as_str(), "quux") |
101 | ); | ||
84 | 102 | ||
85 | vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); | 103 | vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); |
86 | match vfs.commit_changes().as_slice() { | 104 | assert_match!( |
87 | [VfsChange::AddFile { text, path, .. }] => { | 105 | vfs.commit_changes().as_slice(), |
106 | [VfsChange::AddFile { text, path, .. }], | ||
107 | { | ||
88 | assert_eq!(text.as_str(), "spam"); | 108 | assert_eq!(text.as_str(), "spam"); |
89 | assert_eq!(path, "spam.rs"); | 109 | assert_eq!(path, "spam.rs"); |
90 | } | 110 | } |
91 | xs => panic!("unexpected changes {:?}", xs), | 111 | ); |
92 | } | ||
93 | 112 | ||
94 | vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); | 113 | vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); |
95 | match vfs.commit_changes().as_slice() { | 114 | assert_match!( |
96 | [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "spam.rs"), | 115 | vfs.commit_changes().as_slice(), |
97 | xs => panic!("unexpected changes {:?}", xs), | 116 | [VfsChange::RemoveFile { path, .. }], |
98 | } | 117 | assert_eq!(path, "spam.rs") |
99 | 118 | ); | |
100 | fs::write(&dir.path().join("a/new.rs"), "new hello").unwrap(); | 119 | |
101 | process_tasks(&mut vfs, 2); | 120 | fs::create_dir_all(dir.path().join("a/c")).unwrap(); |
102 | match vfs.commit_changes().as_slice() { | 121 | fs::write(dir.path().join("a/c/new.rs"), "new hello").unwrap(); |
103 | [VfsChange::AddFile { text, path, .. }] => { | 122 | process_tasks(&mut vfs, 4); |
123 | assert_match!( | ||
124 | vfs.commit_changes().as_slice(), | ||
125 | [VfsChange::AddFile { text, path, .. }], | ||
126 | { | ||
104 | assert_eq!(text.as_str(), "new hello"); | 127 | assert_eq!(text.as_str(), "new hello"); |
105 | assert_eq!(path, "new.rs"); | 128 | assert_eq!(path, "c/new.rs"); |
106 | } | 129 | } |
107 | xs => panic!("unexpected changes {:?}", xs), | 130 | ); |
108 | } | ||
109 | 131 | ||
110 | fs::rename(&dir.path().join("a/new.rs"), &dir.path().join("a/new1.rs")).unwrap(); | 132 | fs::rename( |
133 | &dir.path().join("a/c/new.rs"), | ||
134 | &dir.path().join("a/c/new1.rs"), | ||
135 | ) | ||
136 | .unwrap(); | ||
111 | process_tasks(&mut vfs, 4); | 137 | process_tasks(&mut vfs, 4); |
112 | match vfs.commit_changes().as_slice() { | 138 | assert_match!( |
139 | vfs.commit_changes().as_slice(), | ||
113 | [VfsChange::RemoveFile { | 140 | [VfsChange::RemoveFile { |
114 | path: removed_path, .. | 141 | path: removed_path, .. |
115 | }, VfsChange::AddFile { | 142 | }, VfsChange::AddFile { |
116 | text, | 143 | text, |
117 | path: added_path, | 144 | path: added_path, |
118 | .. | 145 | .. |
119 | }] => { | 146 | }], |
120 | assert_eq!(removed_path, "new.rs"); | 147 | { |
121 | assert_eq!(added_path, "new1.rs"); | 148 | assert_eq!(removed_path, "c/new.rs"); |
149 | assert_eq!(added_path, "c/new1.rs"); | ||
122 | assert_eq!(text.as_str(), "new hello"); | 150 | assert_eq!(text.as_str(), "new hello"); |
123 | } | 151 | } |
124 | xs => panic!("unexpected changes {:?}", xs), | 152 | ); |
125 | } | ||
126 | 153 | ||
127 | fs::remove_file(&dir.path().join("a/new1.rs")).unwrap(); | 154 | fs::remove_file(&dir.path().join("a/c/new1.rs")).unwrap(); |
128 | process_tasks(&mut vfs, 2); | 155 | process_tasks(&mut vfs, 2); |
129 | match vfs.commit_changes().as_slice() { | 156 | assert_match!( |
130 | [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "new1.rs"), | 157 | vfs.commit_changes().as_slice(), |
131 | xs => panic!("unexpected changes {:?}", xs), | 158 | [VfsChange::RemoveFile { path, .. }], |
132 | } | 159 | assert_eq!(path, "c/new1.rs") |
133 | 160 | ); | |
134 | match vfs.task_receiver().try_recv() { | 161 | |
135 | Err(crossbeam_channel::TryRecvError::Empty) => (), | 162 | fs::create_dir_all(dir.path().join("a/target")).unwrap(); |
136 | res => panic!("unexpected {:?}", res), | 163 | // should be ignored |
137 | } | 164 | fs::write(&dir.path().join("a/target/new.rs"), "ignore me").unwrap(); |
165 | process_tasks(&mut vfs, 1); // 1 task because no LoadChange will happen, just HandleChange for dir creation | ||
166 | |||
167 | assert_match!( | ||
168 | vfs.task_receiver().try_recv(), | ||
169 | Err(crossbeam_channel::TryRecvError::Empty) | ||
170 | ); | ||
138 | 171 | ||
139 | vfs.shutdown().unwrap(); | 172 | vfs.shutdown().unwrap(); |
140 | Ok(()) | 173 | Ok(()) |