mod common;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use common::Fake;
use vvv_core::Query;
use vvv_engine::{Apply, Engine, Languages, MemoryVfs, Retention, Stamp, Vfs, VfsError, Workspace};
#[derive(Debug)]
struct Counting {
inner: MemoryVfs,
reads: AtomicUsize,
}
impl Counting {
fn reads(&self) -> usize {
self.reads.load(Ordering::SeqCst)
}
}
impl Vfs for Counting {
fn read(&self, path: &Path) -> Result<String, VfsError> {
self.reads.fetch_add(1, Ordering::SeqCst);
self.inner.read(path)
}
fn stamp(&self, path: &Path) -> Result<Stamp, VfsError> {
self.inner.stamp(path)
}
fn write(&self, path: &Path, contents: &str) -> Result<(), VfsError> {
self.inner.write(path, contents)
}
fn exists(&self, path: &Path) -> bool {
self.inner.exists(path)
}
fn entry_kind(
&self,
path: &Path,
) -> Result<Option<vvv_engine::EntryKind>, vvv_engine::VfsError> {
self.inner.entry_kind(path)
}
fn entry_path(&self, path: &Path) -> Result<Option<PathBuf>, vvv_engine::VfsError> {
self.inner.entry_path(path)
}
fn same_entry(&self, from: &Path, to: &Path) -> Result<bool, vvv_engine::VfsError> {
self.inner.same_entry(from, to)
}
fn names_alias(&self, from: &Path, to: &Path) -> Result<bool, vvv_engine::VfsError> {
self.inner.names_alias(from, to)
}
fn prepare_parent(&self, path: &Path) -> vvv_engine::ParentCreation {
self.inner.prepare_parent(path)
}
fn remove_file(&self, path: &Path) -> Result<(), vvv_engine::VfsError> {
self.inner.remove_file(path)
}
fn create_dir(&self, path: &Path) -> Result<(), vvv_engine::VfsError> {
self.inner.create_dir(path)
}
fn remove_empty_dir(&self, path: &Path) -> Result<(), vvv_engine::VfsError> {
self.inner.remove_empty_dir(path)
}
fn move_if_absent(&self, from: &Path, to: &Path) -> Result<(), vvv_engine::MoveError> {
self.inner.move_if_absent(from, to)
}
fn walk(&self, root: &Path) -> Result<Vec<PathBuf>, VfsError> {
self.inner.walk(root)
}
}
fn setup() -> (Arc<Counting>, Engine) {
let vfs = Arc::new(Counting {
inner: MemoryVfs::new()
.with_file("/ws/a.p", "def foo\nfoo")
.with_file("/ws/b.p", "def bar\nbar")
.with_file("/ws/notes.txt", "foo"),
reads: AtomicUsize::new(0),
});
let engine = Engine::new(
Workspace::new("/ws", vfs.clone()),
Languages::new().with(Fake::default()),
)
.with_retention(Retention::session());
(vfs, engine)
}
fn paths(engine: &Engine, query: &str) -> Vec<String> {
let mut out: Vec<String> = vvv_engine::SearchQuery::from(Query::pattern(query))
.execute(engine)
.unwrap()
.matches
.into_iter()
.map(|m| m.path.display().to_string())
.collect();
out.dedup();
out
}
#[test]
fn per_call_and_session_agree() {
let (_, session) = setup();
let per_call = session.clone().with_retention(Retention::PerCall);
assert_eq!(paths(&session, "foo"), paths(&per_call, "foo"));
assert_eq!(paths(&session, "foo"), ["a.p"], "notes.txt has no language");
}
#[test]
fn session_reads_each_file_once_while_unchanged() {
let (vfs, engine) = setup();
assert_eq!(paths(&engine, "foo"), ["a.p"]);
let after_first = vfs.reads();
assert_eq!(after_first, 2, "both .p files loaded");
assert_eq!(paths(&engine, "bar"), ["b.p"]);
assert_eq!(paths(&engine, "foo"), ["a.p"]);
assert_eq!(vfs.reads(), after_first, "nothing changed, nothing re-read");
}
#[test]
fn session_sees_edits_new_files_deletions_and_renames() {
let (vfs, engine) = setup();
assert_eq!(paths(&engine, "foo"), ["a.p"]);
vfs.write(Path::new("/ws/b.p"), "def foo\nfoo").unwrap();
let before = vfs.reads();
assert_eq!(paths(&engine, "foo"), ["a.p", "b.p"]);
assert_eq!(vfs.reads(), before + 1);
vfs.write(Path::new("/ws/c.p"), "foo").unwrap();
assert_eq!(paths(&engine, "foo"), ["a.p", "b.p", "c.p"]);
vfs.move_if_absent(Path::new("/ws/c.p"), Path::new("/ws/d.p"))
.unwrap();
assert_eq!(paths(&engine, "foo"), ["a.p", "b.p", "d.p"]);
vfs.move_if_absent(Path::new("/ws/d.p"), Path::new("/ws/d.txt"))
.unwrap();
assert_eq!(paths(&engine, "foo"), ["a.p", "b.p"]);
}
#[test]
fn session_sees_what_the_engine_itself_applies() {
let (_, engine) = setup();
assert_eq!(paths(&engine, "foo"), ["a.p"]);
let intent = vvv_engine::RenameIntent::new("foo", "qux");
let rename = intent.clone().plan(&engine).unwrap();
Apply(rename).apply(&engine).unwrap();
assert_eq!(paths(&engine, "foo"), Vec::<String>::new());
assert_eq!(paths(&engine, "qux"), ["a.p"]);
}
#[test]
fn a_trusting_session_walks_once_per_window_unless_touched() {
let (vfs, session) = setup();
let engine =
session.with_retention(Retention::session().trusting(std::time::Duration::from_secs(60)));
assert_eq!(paths(&engine, "foo"), ["a.p"]);
let after_first = vfs.reads();
vfs.write(Path::new("/ws/b.p"), "def foo\nfoo").unwrap();
assert_eq!(
paths(&engine, "foo"),
["a.p"],
"within the window: not looked at"
);
assert_eq!(vfs.reads(), after_first);
engine.touched();
assert_eq!(paths(&engine, "foo"), ["a.p", "b.p"], "told: looked at");
let rename = vvv_engine::RenameIntent::new("foo", "qux")
.declared_in("a.p")
.plan(&engine)
.unwrap();
Apply(rename).apply(&engine).unwrap();
assert_eq!(paths(&engine, "qux"), ["a.p"]);
}
#[test]
fn a_file_is_parsed_once_per_stamp() {
use std::sync::atomic::AtomicUsize;
let parses = Arc::new(AtomicUsize::new(0));
let vfs = Arc::new(
MemoryVfs::new()
.with_file("/ws/a.p", "def foo\nfoo")
.with_file("/ws/b.p", "use a.p/*\nfoo foo"),
);
let engine = Engine::new(
Workspace::new("/ws", vfs.clone()),
Languages::new().with(common::Counting::new(parses.clone())),
)
.with_retention(Retention::session());
let intent = vvv_engine::RenameIntent::new("foo", "bar");
intent.clone().plan(&engine).unwrap();
let first = parses.load(Ordering::SeqCst);
assert_eq!(first, 2, "each of the two files parsed exactly once");
intent.clone().plan(&engine).unwrap();
assert_eq!(
parses.load(Ordering::SeqCst),
first,
"nothing changed, nothing re-parsed"
);
vfs.write(Path::new("/ws/b.p"), "use a.p/*\nfoo").unwrap();
intent.clone().plan(&engine).unwrap();
assert_eq!(
parses.load(Ordering::SeqCst),
first + 1,
"only the changed file"
);
}