diff options
Diffstat (limited to 'crates/ra_vfs/tests/vfs.rs')
-rw-r--r-- | crates/ra_vfs/tests/vfs.rs | 106 |
1 files changed, 76 insertions, 30 deletions
diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index f56fc4603..8634be9c4 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs | |||
@@ -1,14 +1,13 @@ | |||
1 | use std::{ | 1 | use std::{collections::HashSet, fs}; |
2 | fs, | ||
3 | collections::HashSet, | ||
4 | }; | ||
5 | |||
6 | use tempfile::tempdir; | ||
7 | 2 | ||
3 | use flexi_logger::Logger; | ||
8 | use ra_vfs::{Vfs, VfsChange}; | 4 | use ra_vfs::{Vfs, VfsChange}; |
5 | use tempfile::tempdir; | ||
9 | 6 | ||
10 | #[test] | 7 | #[test] |
11 | fn test_vfs_works() -> std::io::Result<()> { | 8 | fn test_vfs_works() -> std::io::Result<()> { |
9 | Logger::with_str("debug").start().unwrap(); | ||
10 | |||
12 | let files = [ | 11 | let files = [ |
13 | ("a/foo.rs", "hello"), | 12 | ("a/foo.rs", "hello"), |
14 | ("a/bar.rs", "world"), | 13 | ("a/bar.rs", "world"), |
@@ -58,42 +57,89 @@ fn test_vfs_works() -> std::io::Result<()> { | |||
58 | assert_eq!(files, expected_files); | 57 | assert_eq!(files, expected_files); |
59 | } | 58 | } |
60 | 59 | ||
61 | vfs.add_file_overlay(&dir.path().join("a/b/baz.rs"), "quux".to_string()); | 60 | // on disk change |
62 | let change = vfs.commit_changes().pop().unwrap(); | 61 | fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); |
63 | match change { | 62 | let change = vfs.change_receiver().recv().unwrap(); |
64 | VfsChange::ChangeFile { text, .. } => assert_eq!(&*text, "quux"), | 63 | vfs.handle_change(change); |
65 | _ => panic!("unexpected change"), | 64 | match vfs.commit_changes().as_slice() { |
65 | [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), | ||
66 | _ => panic!("unexpected changes"), | ||
66 | } | 67 | } |
67 | 68 | ||
68 | vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); | 69 | // in memory change |
69 | let change = vfs.commit_changes().pop().unwrap(); | 70 | vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), Some("m".to_string())); |
70 | match change { | 71 | match vfs.commit_changes().as_slice() { |
71 | VfsChange::ChangeFile { text, .. } => assert_eq!(&*text, "m"), | 72 | [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "m"), |
72 | _ => panic!("unexpected change"), | 73 | _ => panic!("unexpected changes"), |
73 | } | 74 | } |
74 | 75 | ||
76 | // in memory remove, restores data on disk | ||
75 | vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); | 77 | vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); |
76 | let change = vfs.commit_changes().pop().unwrap(); | 78 | match vfs.commit_changes().as_slice() { |
77 | match change { | 79 | [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), |
78 | VfsChange::ChangeFile { text, .. } => assert_eq!(&*text, "nested hello"), | 80 | _ => panic!("unexpected changes"), |
79 | _ => panic!("unexpected change"), | ||
80 | } | 81 | } |
81 | 82 | ||
82 | vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); | 83 | // in memory add |
83 | let change = vfs.commit_changes().pop().unwrap(); | 84 | vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), Some("spam".to_string())); |
84 | match change { | 85 | match vfs.commit_changes().as_slice() { |
85 | VfsChange::AddFile { text, path, .. } => { | 86 | [VfsChange::AddFile { text, path, .. }] => { |
86 | assert_eq!(&*text, "spam"); | 87 | assert_eq!(text.as_str(), "spam"); |
87 | assert_eq!(path, "spam.rs"); | 88 | assert_eq!(path, "spam.rs"); |
88 | } | 89 | } |
89 | _ => panic!("unexpected change"), | 90 | _ => panic!("unexpected changes"), |
90 | } | 91 | } |
91 | 92 | ||
93 | // in memory remove | ||
92 | vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); | 94 | vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); |
93 | let change = vfs.commit_changes().pop().unwrap(); | 95 | match vfs.commit_changes().as_slice() { |
94 | match change { | 96 | [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "spam.rs"), |
95 | VfsChange::RemoveFile { .. } => (), | 97 | _ => panic!("unexpected changes"), |
96 | _ => panic!("unexpected change"), | 98 | } |
99 | |||
100 | // on disk add | ||
101 | fs::write(&dir.path().join("a/new.rs"), "new hello").unwrap(); | ||
102 | let change = vfs.change_receiver().recv().unwrap(); | ||
103 | vfs.handle_change(change); | ||
104 | match vfs.commit_changes().as_slice() { | ||
105 | [VfsChange::AddFile { text, path, .. }] => { | ||
106 | assert_eq!(text.as_str(), "new hello"); | ||
107 | assert_eq!(path, "new.rs"); | ||
108 | } | ||
109 | _ => panic!("unexpected changes"), | ||
110 | } | ||
111 | |||
112 | // on disk rename | ||
113 | fs::rename(&dir.path().join("a/new.rs"), &dir.path().join("a/new1.rs")).unwrap(); | ||
114 | let change = vfs.change_receiver().recv().unwrap(); | ||
115 | vfs.handle_change(change); | ||
116 | match vfs.commit_changes().as_slice() { | ||
117 | [VfsChange::RemoveFile { | ||
118 | path: removed_path, .. | ||
119 | }, VfsChange::AddFile { | ||
120 | text, | ||
121 | path: added_path, | ||
122 | .. | ||
123 | }] => { | ||
124 | assert_eq!(removed_path, "new.rs"); | ||
125 | assert_eq!(added_path, "new1.rs"); | ||
126 | assert_eq!(text.as_str(), "new hello"); | ||
127 | } | ||
128 | _ => panic!("unexpected changes"), | ||
129 | } | ||
130 | |||
131 | // on disk remove | ||
132 | fs::remove_file(&dir.path().join("a/new1.rs")).unwrap(); | ||
133 | let change = vfs.change_receiver().recv().unwrap(); | ||
134 | vfs.handle_change(change); | ||
135 | match vfs.commit_changes().as_slice() { | ||
136 | [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "new1.rs"), | ||
137 | _ => panic!("unexpected changes"), | ||
138 | } | ||
139 | |||
140 | match vfs.change_receiver().try_recv() { | ||
141 | Err(crossbeam_channel::TryRecvError::Empty) => (), | ||
142 | res => panic!("unexpected {:?}", res), | ||
97 | } | 143 | } |
98 | 144 | ||
99 | vfs.shutdown().unwrap(); | 145 | vfs.shutdown().unwrap(); |