From d032a1a4e8c52f4ea38d9ca24070203a35436158 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sun, 6 Jan 2019 18:36:22 +0100 Subject: complete test --- crates/ra_vfs/tests/vfs.rs | 106 ++++++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 30 deletions(-) (limited to 'crates/ra_vfs/tests') 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 @@ -use std::{ - fs, - collections::HashSet, -}; - -use tempfile::tempdir; +use std::{collections::HashSet, fs}; +use flexi_logger::Logger; use ra_vfs::{Vfs, VfsChange}; +use tempfile::tempdir; #[test] fn test_vfs_works() -> std::io::Result<()> { + Logger::with_str("debug").start().unwrap(); + let files = [ ("a/foo.rs", "hello"), ("a/bar.rs", "world"), @@ -58,42 +57,89 @@ fn test_vfs_works() -> std::io::Result<()> { assert_eq!(files, expected_files); } - vfs.add_file_overlay(&dir.path().join("a/b/baz.rs"), "quux".to_string()); - let change = vfs.commit_changes().pop().unwrap(); - match change { - VfsChange::ChangeFile { text, .. } => assert_eq!(&*text, "quux"), - _ => panic!("unexpected change"), + // on disk change + fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); + let change = vfs.change_receiver().recv().unwrap(); + vfs.handle_change(change); + match vfs.commit_changes().as_slice() { + [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), + _ => panic!("unexpected changes"), } - vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); - let change = vfs.commit_changes().pop().unwrap(); - match change { - VfsChange::ChangeFile { text, .. } => assert_eq!(&*text, "m"), - _ => panic!("unexpected change"), + // in memory change + vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), Some("m".to_string())); + match vfs.commit_changes().as_slice() { + [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "m"), + _ => panic!("unexpected changes"), } + // in memory remove, restores data on disk vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); - let change = vfs.commit_changes().pop().unwrap(); - match change { - VfsChange::ChangeFile { text, .. } => assert_eq!(&*text, "nested hello"), - _ => panic!("unexpected change"), + match vfs.commit_changes().as_slice() { + [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), + _ => panic!("unexpected changes"), } - vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); - let change = vfs.commit_changes().pop().unwrap(); - match change { - VfsChange::AddFile { text, path, .. } => { - assert_eq!(&*text, "spam"); + // in memory add + vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), Some("spam".to_string())); + match vfs.commit_changes().as_slice() { + [VfsChange::AddFile { text, path, .. }] => { + assert_eq!(text.as_str(), "spam"); assert_eq!(path, "spam.rs"); } - _ => panic!("unexpected change"), + _ => panic!("unexpected changes"), } + // in memory remove vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); - let change = vfs.commit_changes().pop().unwrap(); - match change { - VfsChange::RemoveFile { .. } => (), - _ => panic!("unexpected change"), + match vfs.commit_changes().as_slice() { + [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "spam.rs"), + _ => panic!("unexpected changes"), + } + + // on disk add + fs::write(&dir.path().join("a/new.rs"), "new hello").unwrap(); + let change = vfs.change_receiver().recv().unwrap(); + vfs.handle_change(change); + match vfs.commit_changes().as_slice() { + [VfsChange::AddFile { text, path, .. }] => { + assert_eq!(text.as_str(), "new hello"); + assert_eq!(path, "new.rs"); + } + _ => panic!("unexpected changes"), + } + + // on disk rename + fs::rename(&dir.path().join("a/new.rs"), &dir.path().join("a/new1.rs")).unwrap(); + let change = vfs.change_receiver().recv().unwrap(); + vfs.handle_change(change); + match vfs.commit_changes().as_slice() { + [VfsChange::RemoveFile { + path: removed_path, .. + }, VfsChange::AddFile { + text, + path: added_path, + .. + }] => { + assert_eq!(removed_path, "new.rs"); + assert_eq!(added_path, "new1.rs"); + assert_eq!(text.as_str(), "new hello"); + } + _ => panic!("unexpected changes"), + } + + // on disk remove + fs::remove_file(&dir.path().join("a/new1.rs")).unwrap(); + let change = vfs.change_receiver().recv().unwrap(); + vfs.handle_change(change); + match vfs.commit_changes().as_slice() { + [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "new1.rs"), + _ => panic!("unexpected changes"), + } + + match vfs.change_receiver().try_recv() { + Err(crossbeam_channel::TryRecvError::Empty) => (), + res => panic!("unexpected {:?}", res), } vfs.shutdown().unwrap(); -- cgit v1.2.3 From 6b86f038d61752bbf306ed5dd9def74be3b5dcc1 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 7 Jan 2019 21:35:18 +0100 Subject: refator to move all io to io module use same channel for scanner and watcher some implementations pending --- crates/ra_vfs/tests/vfs.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index 8634be9c4..21d5633b1 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -59,15 +59,15 @@ fn test_vfs_works() -> std::io::Result<()> { // on disk change fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); - let change = vfs.change_receiver().recv().unwrap(); - vfs.handle_change(change); + let task = vfs.task_receiver().recv().unwrap(); + vfs.handle_task(task); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), _ => panic!("unexpected changes"), } // in memory change - vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), Some("m".to_string())); + vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "m"), _ => panic!("unexpected changes"), @@ -81,7 +81,7 @@ fn test_vfs_works() -> std::io::Result<()> { } // in memory add - vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), Some("spam".to_string())); + vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); match vfs.commit_changes().as_slice() { [VfsChange::AddFile { text, path, .. }] => { assert_eq!(text.as_str(), "spam"); @@ -99,8 +99,8 @@ fn test_vfs_works() -> std::io::Result<()> { // on disk add fs::write(&dir.path().join("a/new.rs"), "new hello").unwrap(); - let change = vfs.change_receiver().recv().unwrap(); - vfs.handle_change(change); + let task = vfs.task_receiver().recv().unwrap(); + vfs.handle_task(task); match vfs.commit_changes().as_slice() { [VfsChange::AddFile { text, path, .. }] => { assert_eq!(text.as_str(), "new hello"); @@ -111,8 +111,8 @@ fn test_vfs_works() -> std::io::Result<()> { // on disk rename fs::rename(&dir.path().join("a/new.rs"), &dir.path().join("a/new1.rs")).unwrap(); - let change = vfs.change_receiver().recv().unwrap(); - vfs.handle_change(change); + let task = vfs.task_receiver().recv().unwrap(); + vfs.handle_task(task); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path: removed_path, .. @@ -130,14 +130,14 @@ fn test_vfs_works() -> std::io::Result<()> { // on disk remove fs::remove_file(&dir.path().join("a/new1.rs")).unwrap(); - let change = vfs.change_receiver().recv().unwrap(); - vfs.handle_change(change); + let task = vfs.task_receiver().recv().unwrap(); + vfs.handle_task(task); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "new1.rs"), _ => panic!("unexpected changes"), } - match vfs.change_receiver().try_recv() { + match vfs.task_receiver().try_recv() { Err(crossbeam_channel::TryRecvError::Empty) => (), res => panic!("unexpected {:?}", res), } -- cgit v1.2.3 From 76bf7498aa88c4de4517f4eb1218807fdfc7071b Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 12 Jan 2019 18:17:52 +0100 Subject: handle watched events filtering in `Vfs`add `is_overlayed`load changed files contents in `io` --- crates/ra_vfs/tests/vfs.rs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index 21d5633b1..9cde2bed7 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -4,6 +4,13 @@ use flexi_logger::Logger; use ra_vfs::{Vfs, VfsChange}; use tempfile::tempdir; +fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { + for _ in 0..num_tasks { + let task = vfs.task_receiver().recv().unwrap(); + vfs.handle_task(task); + } +} + #[test] fn test_vfs_works() -> std::io::Result<()> { Logger::with_str("debug").start().unwrap(); @@ -25,10 +32,7 @@ fn test_vfs_works() -> std::io::Result<()> { let b_root = dir.path().join("a/b"); let (mut vfs, _) = Vfs::new(vec![a_root, b_root]); - for _ in 0..2 { - let task = vfs.task_receiver().recv().unwrap(); - vfs.handle_task(task); - } + process_tasks(&mut vfs, 2); { let files = vfs .commit_changes() @@ -57,30 +61,26 @@ fn test_vfs_works() -> std::io::Result<()> { assert_eq!(files, expected_files); } - // on disk change fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); - let task = vfs.task_receiver().recv().unwrap(); - vfs.handle_task(task); + process_tasks(&mut vfs, 1); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), _ => panic!("unexpected changes"), } - // in memory change vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "m"), _ => panic!("unexpected changes"), } - // in memory remove, restores data on disk + // removing overlay restores data on disk vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), _ => panic!("unexpected changes"), } - // in memory add vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); match vfs.commit_changes().as_slice() { [VfsChange::AddFile { text, path, .. }] => { @@ -90,17 +90,14 @@ fn test_vfs_works() -> std::io::Result<()> { _ => panic!("unexpected changes"), } - // in memory remove vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "spam.rs"), _ => panic!("unexpected changes"), } - // on disk add fs::write(&dir.path().join("a/new.rs"), "new hello").unwrap(); - let task = vfs.task_receiver().recv().unwrap(); - vfs.handle_task(task); + process_tasks(&mut vfs, 1); match vfs.commit_changes().as_slice() { [VfsChange::AddFile { text, path, .. }] => { assert_eq!(text.as_str(), "new hello"); @@ -109,10 +106,8 @@ fn test_vfs_works() -> std::io::Result<()> { _ => panic!("unexpected changes"), } - // on disk rename fs::rename(&dir.path().join("a/new.rs"), &dir.path().join("a/new1.rs")).unwrap(); - let task = vfs.task_receiver().recv().unwrap(); - vfs.handle_task(task); + process_tasks(&mut vfs, 2); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path: removed_path, .. @@ -125,13 +120,11 @@ fn test_vfs_works() -> std::io::Result<()> { assert_eq!(added_path, "new1.rs"); assert_eq!(text.as_str(), "new hello"); } - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } - // on disk remove fs::remove_file(&dir.path().join("a/new1.rs")).unwrap(); - let task = vfs.task_receiver().recv().unwrap(); - vfs.handle_task(task); + process_tasks(&mut vfs, 1); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "new1.rs"), _ => panic!("unexpected changes"), -- cgit v1.2.3 From b0f7e72c49141df0ad95e3fcb561ee6a4d86b537 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 15 Jan 2019 17:45:56 +0100 Subject: use notify with fix --- crates/ra_vfs/tests/vfs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index 9cde2bed7..87fb5a092 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -1,6 +1,6 @@ use std::{collections::HashSet, fs}; -use flexi_logger::Logger; +// use flexi_logger::Logger; use ra_vfs::{Vfs, VfsChange}; use tempfile::tempdir; @@ -13,7 +13,7 @@ fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { #[test] fn test_vfs_works() -> std::io::Result<()> { - Logger::with_str("debug").start().unwrap(); + // Logger::with_str("debug").start().unwrap(); let files = [ ("a/foo.rs", "hello"), -- cgit v1.2.3 From e69b620f0d1e90afcc14dc7cf07ed0b828d8ec96 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Wed, 16 Jan 2019 19:30:20 +0100 Subject: add missing Task::HandleChange --- crates/ra_vfs/tests/vfs.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index 87fb5a092..8266a0bd5 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -62,23 +62,24 @@ fn test_vfs_works() -> std::io::Result<()> { } fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); - process_tasks(&mut vfs, 1); + // 2 tasks per watcher change, first for HandleChange then for LoadChange + process_tasks(&mut vfs, 2); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "m"), - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } // removing overlay restores data on disk vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); match vfs.commit_changes().as_slice() { [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); @@ -87,27 +88,27 @@ fn test_vfs_works() -> std::io::Result<()> { assert_eq!(text.as_str(), "spam"); assert_eq!(path, "spam.rs"); } - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "spam.rs"), - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } fs::write(&dir.path().join("a/new.rs"), "new hello").unwrap(); - process_tasks(&mut vfs, 1); + process_tasks(&mut vfs, 2); match vfs.commit_changes().as_slice() { [VfsChange::AddFile { text, path, .. }] => { assert_eq!(text.as_str(), "new hello"); assert_eq!(path, "new.rs"); } - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } fs::rename(&dir.path().join("a/new.rs"), &dir.path().join("a/new1.rs")).unwrap(); - process_tasks(&mut vfs, 2); + process_tasks(&mut vfs, 4); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path: removed_path, .. @@ -124,10 +125,10 @@ fn test_vfs_works() -> std::io::Result<()> { } fs::remove_file(&dir.path().join("a/new1.rs")).unwrap(); - process_tasks(&mut vfs, 1); + process_tasks(&mut vfs, 2); match vfs.commit_changes().as_slice() { [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "new1.rs"), - _ => panic!("unexpected changes"), + xs => panic!("unexpected changes {:?}", xs), } match vfs.task_receiver().try_recv() { -- cgit v1.2.3 From eacf7aeb42d7ba54c305664773e77eb592b51b99 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 19 Jan 2019 22:28:51 +0100 Subject: ignore check event dir for ignore, cleanup tests --- crates/ra_vfs/tests/vfs.rs | 131 ++++++++++++++++++++++++++++----------------- 1 file changed, 82 insertions(+), 49 deletions(-) (limited to 'crates/ra_vfs/tests') 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) { } } +macro_rules! assert_match { + ($x:expr, $pat:pat) => { + assert_match!($x, $pat, assert!(true)) + }; + ($x:expr, $pat:pat, $assert:expr) => { + match $x { + $pat => $assert, + x => assert!(false, "Expected {}, got {:?}", stringify!($pat), x), + }; + }; +} + #[test] fn test_vfs_works() -> std::io::Result<()> { - // Logger::with_str("debug").start().unwrap(); + // Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap(); let files = [ ("a/foo.rs", "hello"), @@ -21,13 +33,16 @@ fn test_vfs_works() -> std::io::Result<()> { ("a/b/baz.rs", "nested hello"), ]; - let dir = tempdir()?; + let dir = tempdir().unwrap(); for (path, text) in files.iter() { let file_path = dir.path().join(path); - fs::create_dir_all(file_path.parent().unwrap())?; + fs::create_dir_all(file_path.parent().unwrap()).unwrap(); fs::write(file_path, text)? } + let gitignore = dir.path().join("a/.gitignore"); + fs::write(gitignore, "/target").unwrap(); + let a_root = dir.path().join("a"); let b_root = dir.path().join("a/b"); @@ -62,79 +77,97 @@ fn test_vfs_works() -> std::io::Result<()> { } fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); - // 2 tasks per watcher change, first for HandleChange then for LoadChange + // 2 tasks per change, HandleChange and then LoadChange process_tasks(&mut vfs, 2); - match vfs.commit_changes().as_slice() { - [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), - xs => panic!("unexpected changes {:?}", xs), - } + assert_match!( + vfs.commit_changes().as_slice(), + [VfsChange::ChangeFile { text, .. }], + assert_eq!(text.as_str(), "quux") + ); vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); - match vfs.commit_changes().as_slice() { - [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "m"), - xs => panic!("unexpected changes {:?}", xs), - } + assert_match!( + vfs.commit_changes().as_slice(), + [VfsChange::ChangeFile { text, .. }], + assert_eq!(text.as_str(), "m") + ); // removing overlay restores data on disk vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); - match vfs.commit_changes().as_slice() { - [VfsChange::ChangeFile { text, .. }] => assert_eq!(text.as_str(), "quux"), - xs => panic!("unexpected changes {:?}", xs), - } + assert_match!( + vfs.commit_changes().as_slice(), + [VfsChange::ChangeFile { text, .. }], + assert_eq!(text.as_str(), "quux") + ); vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); - match vfs.commit_changes().as_slice() { - [VfsChange::AddFile { text, path, .. }] => { + assert_match!( + vfs.commit_changes().as_slice(), + [VfsChange::AddFile { text, path, .. }], + { assert_eq!(text.as_str(), "spam"); assert_eq!(path, "spam.rs"); } - xs => panic!("unexpected changes {:?}", xs), - } + ); vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs")); - match vfs.commit_changes().as_slice() { - [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "spam.rs"), - xs => panic!("unexpected changes {:?}", xs), - } - - fs::write(&dir.path().join("a/new.rs"), "new hello").unwrap(); - process_tasks(&mut vfs, 2); - match vfs.commit_changes().as_slice() { - [VfsChange::AddFile { text, path, .. }] => { + assert_match!( + vfs.commit_changes().as_slice(), + [VfsChange::RemoveFile { path, .. }], + assert_eq!(path, "spam.rs") + ); + + fs::create_dir_all(dir.path().join("a/c")).unwrap(); + fs::write(dir.path().join("a/c/new.rs"), "new hello").unwrap(); + process_tasks(&mut vfs, 4); + assert_match!( + vfs.commit_changes().as_slice(), + [VfsChange::AddFile { text, path, .. }], + { assert_eq!(text.as_str(), "new hello"); - assert_eq!(path, "new.rs"); + assert_eq!(path, "c/new.rs"); } - xs => panic!("unexpected changes {:?}", xs), - } + ); - fs::rename(&dir.path().join("a/new.rs"), &dir.path().join("a/new1.rs")).unwrap(); + fs::rename( + &dir.path().join("a/c/new.rs"), + &dir.path().join("a/c/new1.rs"), + ) + .unwrap(); process_tasks(&mut vfs, 4); - match vfs.commit_changes().as_slice() { + assert_match!( + vfs.commit_changes().as_slice(), [VfsChange::RemoveFile { path: removed_path, .. }, VfsChange::AddFile { text, path: added_path, .. - }] => { - assert_eq!(removed_path, "new.rs"); - assert_eq!(added_path, "new1.rs"); + }], + { + assert_eq!(removed_path, "c/new.rs"); + assert_eq!(added_path, "c/new1.rs"); assert_eq!(text.as_str(), "new hello"); } - xs => panic!("unexpected changes {:?}", xs), - } + ); - fs::remove_file(&dir.path().join("a/new1.rs")).unwrap(); + fs::remove_file(&dir.path().join("a/c/new1.rs")).unwrap(); process_tasks(&mut vfs, 2); - match vfs.commit_changes().as_slice() { - [VfsChange::RemoveFile { path, .. }] => assert_eq!(path, "new1.rs"), - xs => panic!("unexpected changes {:?}", xs), - } - - match vfs.task_receiver().try_recv() { - Err(crossbeam_channel::TryRecvError::Empty) => (), - res => panic!("unexpected {:?}", res), - } + assert_match!( + vfs.commit_changes().as_slice(), + [VfsChange::RemoveFile { path, .. }], + assert_eq!(path, "c/new1.rs") + ); + + fs::create_dir_all(dir.path().join("a/target")).unwrap(); + // should be ignored + fs::write(&dir.path().join("a/target/new.rs"), "ignore me").unwrap(); + process_tasks(&mut vfs, 1); // 1 task because no LoadChange will happen, just HandleChange for dir creation + + assert_match!( + vfs.task_receiver().try_recv(), + Err(crossbeam_channel::TryRecvError::Empty) + ); vfs.shutdown().unwrap(); Ok(()) -- cgit v1.2.3 From 7f7c4e7465f58cdbfdaaf232d571960f1b754b7c Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 21 Jan 2019 18:37:46 +0100 Subject: do not emit create for directory again --- crates/ra_vfs/tests/vfs.rs | 3 --- 1 file changed, 3 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index 71b25a5c9..b18ea74a3 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -40,9 +40,6 @@ fn test_vfs_works() -> std::io::Result<()> { fs::write(file_path, text)? } - let gitignore = dir.path().join("a/.gitignore"); - fs::write(gitignore, "/target").unwrap(); - let a_root = dir.path().join("a"); let b_root = dir.path().join("a/b"); -- cgit v1.2.3 From be14ab217ce29542a8b2c84282e822adcc69646c Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 22 Jan 2019 18:38:34 +0100 Subject: better test, avoid duplicated events --- crates/ra_vfs/tests/vfs.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index b18ea74a3..d3271570a 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -1,12 +1,13 @@ use std::{collections::HashSet, fs}; -// use flexi_logger::Logger; +use flexi_logger::Logger; use ra_vfs::{Vfs, VfsChange}; use tempfile::tempdir; fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { for _ in 0..num_tasks { let task = vfs.task_receiver().recv().unwrap(); + log::debug!("{:?}", task); vfs.handle_task(task); } } @@ -25,7 +26,7 @@ macro_rules! assert_match { #[test] fn test_vfs_works() -> std::io::Result<()> { - // Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap(); + Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap(); let files = [ ("a/foo.rs", "hello"), @@ -114,21 +115,21 @@ fn test_vfs_works() -> std::io::Result<()> { assert_eq!(path, "spam.rs") ); - fs::create_dir_all(dir.path().join("a/c")).unwrap(); - fs::write(dir.path().join("a/c/new.rs"), "new hello").unwrap(); + fs::create_dir_all(dir.path().join("a/sub1/sub2")).unwrap(); + fs::write(dir.path().join("a/sub1/sub2/new.rs"), "new hello").unwrap(); process_tasks(&mut vfs, 4); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::AddFile { text, path, .. }], { assert_eq!(text.as_str(), "new hello"); - assert_eq!(path, "c/new.rs"); + assert_eq!(path, "sub1/sub2/new.rs"); } ); fs::rename( - &dir.path().join("a/c/new.rs"), - &dir.path().join("a/c/new1.rs"), + &dir.path().join("a/sub1/sub2/new.rs"), + &dir.path().join("a/sub1/sub2/new1.rs"), ) .unwrap(); process_tasks(&mut vfs, 4); @@ -142,18 +143,18 @@ fn test_vfs_works() -> std::io::Result<()> { .. }], { - assert_eq!(removed_path, "c/new.rs"); - assert_eq!(added_path, "c/new1.rs"); + assert_eq!(removed_path, "sub1/sub2/new.rs"); + assert_eq!(added_path, "sub1/sub2/new1.rs"); assert_eq!(text.as_str(), "new hello"); } ); - fs::remove_file(&dir.path().join("a/c/new1.rs")).unwrap(); + fs::remove_file(&dir.path().join("a/sub1/sub2/new1.rs")).unwrap(); process_tasks(&mut vfs, 2); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::RemoveFile { path, .. }], - assert_eq!(path, "c/new1.rs") + assert_eq!(path, "sub1/sub2/new1.rs") ); fs::create_dir_all(dir.path().join("a/target")).unwrap(); -- cgit v1.2.3 From cfbf47b0023585a30d825b5c5da8e2fb9d6fc337 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Wed, 23 Jan 2019 20:43:35 +0100 Subject: review fixes --- crates/ra_vfs/tests/vfs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index d3271570a..bf44e97c5 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -117,7 +117,7 @@ fn test_vfs_works() -> std::io::Result<()> { fs::create_dir_all(dir.path().join("a/sub1/sub2")).unwrap(); fs::write(dir.path().join("a/sub1/sub2/new.rs"), "new hello").unwrap(); - process_tasks(&mut vfs, 4); + process_tasks(&mut vfs, 3); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::AddFile { text, path, .. }], -- cgit v1.2.3 From d63e1cebff771621b90bdce25ac013eecb415e1e Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 25 Jan 2019 18:39:35 +0100 Subject: use `Roots` in watcher --- crates/ra_vfs/tests/vfs.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index bf44e97c5..8562c56b9 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -75,27 +75,31 @@ fn test_vfs_works() -> std::io::Result<()> { } fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap(); - // 2 tasks per change, HandleChange and then LoadChange - process_tasks(&mut vfs, 2); + process_tasks(&mut vfs, 1); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::ChangeFile { text, .. }], assert_eq!(text.as_str(), "quux") ); - vfs.change_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); + vfs.add_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string()); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::ChangeFile { text, .. }], assert_eq!(text.as_str(), "m") ); + // 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(), []); + // removing overlay restores data on disk vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs")); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::ChangeFile { text, .. }], - assert_eq!(text.as_str(), "quux") + assert_eq!(text.as_str(), "corge") ); vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string()); @@ -117,7 +121,7 @@ fn test_vfs_works() -> std::io::Result<()> { fs::create_dir_all(dir.path().join("a/sub1/sub2")).unwrap(); fs::write(dir.path().join("a/sub1/sub2/new.rs"), "new hello").unwrap(); - process_tasks(&mut vfs, 3); + process_tasks(&mut vfs, 1); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::AddFile { text, path, .. }], @@ -132,7 +136,7 @@ fn test_vfs_works() -> std::io::Result<()> { &dir.path().join("a/sub1/sub2/new1.rs"), ) .unwrap(); - process_tasks(&mut vfs, 4); + process_tasks(&mut vfs, 2); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::RemoveFile { @@ -150,17 +154,16 @@ fn test_vfs_works() -> std::io::Result<()> { ); fs::remove_file(&dir.path().join("a/sub1/sub2/new1.rs")).unwrap(); - process_tasks(&mut vfs, 2); + process_tasks(&mut vfs, 1); assert_match!( vfs.commit_changes().as_slice(), [VfsChange::RemoveFile { path, .. }], assert_eq!(path, "sub1/sub2/new1.rs") ); - fs::create_dir_all(dir.path().join("a/target")).unwrap(); // should be ignored + fs::create_dir_all(dir.path().join("a/target")).unwrap(); fs::write(&dir.path().join("a/target/new.rs"), "ignore me").unwrap(); - process_tasks(&mut vfs, 1); // 1 task because no LoadChange will happen, just HandleChange for dir creation assert_match!( vfs.task_receiver().try_recv(), -- cgit v1.2.3 From 410a3ae6e847b59f9930ce4d6bf9f3c5f1d72167 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 25 Jan 2019 22:13:55 +0100 Subject: use entry file_type, improve test --- crates/ra_vfs/tests/vfs.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'crates/ra_vfs/tests') diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs index 8562c56b9..357e1c775 100644 --- a/crates/ra_vfs/tests/vfs.rs +++ b/crates/ra_vfs/tests/vfs.rs @@ -1,12 +1,16 @@ -use std::{collections::HashSet, fs}; +use std::{collections::HashSet, fs, time::Duration}; -use flexi_logger::Logger; +// use flexi_logger::Logger; +use crossbeam_channel::RecvTimeoutError; use ra_vfs::{Vfs, VfsChange}; use tempfile::tempdir; fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { for _ in 0..num_tasks { - let task = vfs.task_receiver().recv().unwrap(); + let task = vfs + .task_receiver() + .recv_timeout(Duration::from_secs(3)) + .unwrap(); log::debug!("{:?}", task); vfs.handle_task(task); } @@ -14,7 +18,7 @@ fn process_tasks(vfs: &mut Vfs, num_tasks: u32) { macro_rules! assert_match { ($x:expr, $pat:pat) => { - assert_match!($x, $pat, assert!(true)) + assert_match!($x, $pat, ()) }; ($x:expr, $pat:pat, $assert:expr) => { match $x { @@ -26,7 +30,7 @@ macro_rules! assert_match { #[test] fn test_vfs_works() -> std::io::Result<()> { - Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap(); + // Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap(); let files = [ ("a/foo.rs", "hello"), @@ -166,8 +170,8 @@ fn test_vfs_works() -> std::io::Result<()> { fs::write(&dir.path().join("a/target/new.rs"), "ignore me").unwrap(); assert_match!( - vfs.task_receiver().try_recv(), - Err(crossbeam_channel::TryRecvError::Empty) + vfs.task_receiver().recv_timeout(Duration::from_millis(300)), // slightly more than watcher debounce delay + Err(RecvTimeoutError::Timeout) ); vfs.shutdown().unwrap(); -- cgit v1.2.3