2019-01-25 15:13:55 -06:00
|
|
|
use std::{collections::HashSet, fs, time::Duration};
|
2018-12-18 07:38:05 -06:00
|
|
|
|
2019-01-25 15:13:55 -06:00
|
|
|
// use flexi_logger::Logger;
|
|
|
|
use crossbeam_channel::RecvTimeoutError;
|
2018-12-18 07:38:05 -06:00
|
|
|
use ra_vfs::{Vfs, VfsChange};
|
2019-01-06 11:36:22 -06:00
|
|
|
use tempfile::tempdir;
|
2018-12-18 07:38:05 -06:00
|
|
|
|
2019-01-12 11:17:52 -06:00
|
|
|
fn process_tasks(vfs: &mut Vfs, num_tasks: u32) {
|
|
|
|
for _ in 0..num_tasks {
|
2019-01-25 15:13:55 -06:00
|
|
|
let task = vfs
|
|
|
|
.task_receiver()
|
|
|
|
.recv_timeout(Duration::from_secs(3))
|
|
|
|
.unwrap();
|
2019-01-22 11:38:34 -06:00
|
|
|
log::debug!("{:?}", task);
|
2019-01-12 11:17:52 -06:00
|
|
|
vfs.handle_task(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:28:51 -06:00
|
|
|
macro_rules! assert_match {
|
|
|
|
($x:expr, $pat:pat) => {
|
2019-01-25 15:13:55 -06:00
|
|
|
assert_match!($x, $pat, ())
|
2019-01-19 15:28:51 -06:00
|
|
|
};
|
|
|
|
($x:expr, $pat:pat, $assert:expr) => {
|
|
|
|
match $x {
|
|
|
|
$pat => $assert,
|
|
|
|
x => assert!(false, "Expected {}, got {:?}", stringify!($pat), x),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-18 07:38:05 -06:00
|
|
|
#[test]
|
|
|
|
fn test_vfs_works() -> std::io::Result<()> {
|
2019-01-25 15:13:55 -06:00
|
|
|
// Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap();
|
2019-01-06 11:36:22 -06:00
|
|
|
|
2018-12-18 07:38:05 -06:00
|
|
|
let files = [
|
|
|
|
("a/foo.rs", "hello"),
|
|
|
|
("a/bar.rs", "world"),
|
|
|
|
("a/b/baz.rs", "nested hello"),
|
|
|
|
];
|
|
|
|
|
2019-01-19 15:28:51 -06:00
|
|
|
let dir = tempdir().unwrap();
|
2018-12-18 07:38:05 -06:00
|
|
|
for (path, text) in files.iter() {
|
|
|
|
let file_path = dir.path().join(path);
|
2019-01-19 15:28:51 -06:00
|
|
|
fs::create_dir_all(file_path.parent().unwrap()).unwrap();
|
2018-12-18 07:38:05 -06:00
|
|
|
fs::write(file_path, text)?
|
|
|
|
}
|
|
|
|
|
|
|
|
let a_root = dir.path().join("a");
|
|
|
|
let b_root = dir.path().join("a/b");
|
|
|
|
|
2018-12-19 06:58:34 -06:00
|
|
|
let (mut vfs, _) = Vfs::new(vec![a_root, b_root]);
|
2019-01-12 11:17:52 -06:00
|
|
|
process_tasks(&mut vfs, 2);
|
2018-12-18 07:38:05 -06:00
|
|
|
{
|
|
|
|
let files = vfs
|
|
|
|
.commit_changes()
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|change| {
|
|
|
|
let files = match change {
|
|
|
|
VfsChange::AddRoot { files, .. } => files,
|
|
|
|
_ => panic!("unexpected change"),
|
|
|
|
};
|
|
|
|
files.into_iter().map(|(_id, path, text)| {
|
|
|
|
let text: String = (&*text).clone();
|
|
|
|
(format!("{}", path.display()), text)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
let expected_files = [
|
|
|
|
("foo.rs", "hello"),
|
|
|
|
("bar.rs", "world"),
|
|
|
|
("baz.rs", "nested hello"),
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.map(|(path, text)| (path.to_string(), text.to_string()))
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
assert_eq!(files, expected_files);
|
|
|
|
}
|
|
|
|
|
2019-01-06 11:36:22 -06:00
|
|
|
fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap();
|
2019-01-25 11:39:35 -06:00
|
|
|
process_tasks(&mut vfs, 1);
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::ChangeFile { text, .. }],
|
|
|
|
assert_eq!(text.as_str(), "quux")
|
|
|
|
);
|
2018-12-18 07:38:05 -06:00
|
|
|
|
2019-01-25 11:39:35 -06:00
|
|
|
vfs.add_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string());
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::ChangeFile { text, .. }],
|
|
|
|
assert_eq!(text.as_str(), "m")
|
|
|
|
);
|
2018-12-18 07:38:05 -06:00
|
|
|
|
2019-01-25 11:39:35 -06:00
|
|
|
// changing file on disk while overlayed doesn't generate a VfsChange
|
|
|
|
fs::write(&dir.path().join("a/b/baz.rs"), "corge").unwrap();
|
|
|
|
process_tasks(&mut vfs, 1);
|
|
|
|
assert_match!(vfs.commit_changes().as_slice(), []);
|
|
|
|
|
2019-01-12 11:17:52 -06:00
|
|
|
// removing overlay restores data on disk
|
2018-12-18 07:38:05 -06:00
|
|
|
vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs"));
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::ChangeFile { text, .. }],
|
2019-01-25 11:39:35 -06:00
|
|
|
assert_eq!(text.as_str(), "corge")
|
2019-01-19 15:28:51 -06:00
|
|
|
);
|
2018-12-18 07:38:05 -06:00
|
|
|
|
2019-01-07 14:35:18 -06:00
|
|
|
vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string());
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::AddFile { text, path, .. }],
|
|
|
|
{
|
2019-01-06 11:36:22 -06:00
|
|
|
assert_eq!(text.as_str(), "spam");
|
2018-12-18 07:38:05 -06:00
|
|
|
assert_eq!(path, "spam.rs");
|
|
|
|
}
|
2019-01-19 15:28:51 -06:00
|
|
|
);
|
2018-12-18 07:38:05 -06:00
|
|
|
|
|
|
|
vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs"));
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::RemoveFile { path, .. }],
|
|
|
|
assert_eq!(path, "spam.rs")
|
|
|
|
);
|
|
|
|
|
2019-01-22 11:38:34 -06:00
|
|
|
fs::create_dir_all(dir.path().join("a/sub1/sub2")).unwrap();
|
|
|
|
fs::write(dir.path().join("a/sub1/sub2/new.rs"), "new hello").unwrap();
|
2019-01-25 11:39:35 -06:00
|
|
|
process_tasks(&mut vfs, 1);
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::AddFile { text, path, .. }],
|
|
|
|
{
|
2019-01-06 11:36:22 -06:00
|
|
|
assert_eq!(text.as_str(), "new hello");
|
2019-01-22 11:38:34 -06:00
|
|
|
assert_eq!(path, "sub1/sub2/new.rs");
|
2019-01-06 11:36:22 -06:00
|
|
|
}
|
2019-01-19 15:28:51 -06:00
|
|
|
);
|
2019-01-06 11:36:22 -06:00
|
|
|
|
2019-01-19 15:28:51 -06:00
|
|
|
fs::rename(
|
2019-01-22 11:38:34 -06:00
|
|
|
&dir.path().join("a/sub1/sub2/new.rs"),
|
|
|
|
&dir.path().join("a/sub1/sub2/new1.rs"),
|
2019-01-19 15:28:51 -06:00
|
|
|
)
|
|
|
|
.unwrap();
|
2019-01-25 11:39:35 -06:00
|
|
|
process_tasks(&mut vfs, 2);
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
2019-01-06 11:36:22 -06:00
|
|
|
[VfsChange::RemoveFile {
|
|
|
|
path: removed_path, ..
|
|
|
|
}, VfsChange::AddFile {
|
|
|
|
text,
|
|
|
|
path: added_path,
|
|
|
|
..
|
2019-01-19 15:28:51 -06:00
|
|
|
}],
|
|
|
|
{
|
2019-01-22 11:38:34 -06:00
|
|
|
assert_eq!(removed_path, "sub1/sub2/new.rs");
|
|
|
|
assert_eq!(added_path, "sub1/sub2/new1.rs");
|
2019-01-06 11:36:22 -06:00
|
|
|
assert_eq!(text.as_str(), "new hello");
|
|
|
|
}
|
2019-01-19 15:28:51 -06:00
|
|
|
);
|
2019-01-06 11:36:22 -06:00
|
|
|
|
2019-01-22 11:38:34 -06:00
|
|
|
fs::remove_file(&dir.path().join("a/sub1/sub2/new1.rs")).unwrap();
|
2019-01-25 11:39:35 -06:00
|
|
|
process_tasks(&mut vfs, 1);
|
2019-01-19 15:28:51 -06:00
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::RemoveFile { path, .. }],
|
2019-01-22 11:38:34 -06:00
|
|
|
assert_eq!(path, "sub1/sub2/new1.rs")
|
2019-01-19 15:28:51 -06:00
|
|
|
);
|
|
|
|
|
2019-01-26 06:41:52 -06:00
|
|
|
{
|
|
|
|
vfs.add_file_overlay(&dir.path().join("a/memfile.rs"), "memfile".to_string());
|
|
|
|
assert_match!(
|
|
|
|
vfs.commit_changes().as_slice(),
|
|
|
|
[VfsChange::AddFile { text, .. }],
|
|
|
|
assert_eq!(text.as_str(), "memfile")
|
|
|
|
);
|
|
|
|
fs::write(&dir.path().join("a/memfile.rs"), "ignore me").unwrap();
|
|
|
|
process_tasks(&mut vfs, 1);
|
|
|
|
assert_match!(vfs.commit_changes().as_slice(), []);
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:28:51 -06:00
|
|
|
// should be ignored
|
2019-01-25 11:39:35 -06:00
|
|
|
fs::create_dir_all(dir.path().join("a/target")).unwrap();
|
2019-01-19 15:28:51 -06:00
|
|
|
fs::write(&dir.path().join("a/target/new.rs"), "ignore me").unwrap();
|
|
|
|
|
|
|
|
assert_match!(
|
2019-01-25 15:13:55 -06:00
|
|
|
vfs.task_receiver().recv_timeout(Duration::from_millis(300)), // slightly more than watcher debounce delay
|
|
|
|
Err(RecvTimeoutError::Timeout)
|
2019-01-19 15:28:51 -06:00
|
|
|
);
|
2018-12-18 07:38:05 -06:00
|
|
|
|
|
|
|
vfs.shutdown().unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|