Pick files directly from the Fs in simulate_host

Previously, the list of all existing files was maintained separately, but
it was not updated when a guest created a file.
This commit is contained in:
Max Brunsfeld 2022-05-09 15:39:45 -07:00
parent 3d6db9083d
commit 2aec4ff234
2 changed files with 22 additions and 22 deletions

View File

@ -5910,7 +5910,6 @@ mod tests {
let mut clients = Vec::new(); let mut clients = Vec::new();
let mut user_ids = Vec::new(); let mut user_ids = Vec::new();
let mut op_start_signals = Vec::new(); let mut op_start_signals = Vec::new();
let files = Arc::new(Mutex::new(Vec::new()));
let mut next_entity_id = 100000; let mut next_entity_id = 100000;
let mut host_cx = TestAppContext::new( let mut host_cx = TestAppContext::new(
@ -5964,7 +5963,7 @@ mod tests {
capabilities: lsp::LanguageServer::full_capabilities(), capabilities: lsp::LanguageServer::full_capabilities(),
initializer: Some(Box::new({ initializer: Some(Box::new({
let rng = rng.clone(); let rng = rng.clone();
let files = files.clone(); let fs = fs.clone();
let project = host_project.downgrade(); let project = host_project.downgrade();
move |fake_server: &mut FakeLanguageServer| { move |fake_server: &mut FakeLanguageServer| {
fake_server.handle_request::<lsp::request::Completion, _, _>( fake_server.handle_request::<lsp::request::Completion, _, _>(
@ -6005,13 +6004,13 @@ mod tests {
); );
fake_server.handle_request::<lsp::request::GotoDefinition, _, _>({ fake_server.handle_request::<lsp::request::GotoDefinition, _, _>({
let files = files.clone(); let fs = fs.clone();
let rng = rng.clone(); let rng = rng.clone();
move |_, _| { move |_, _| {
let files = files.clone(); let fs = fs.clone();
let rng = rng.clone(); let rng = rng.clone();
async move { async move {
let files = files.lock(); let files = fs.files().await;
let mut rng = rng.lock(); let mut rng = rng.lock();
let count = rng.gen_range::<usize, _>(1..3); let count = rng.gen_range::<usize, _>(1..3);
let files = (0..count) let files = (0..count)
@ -6082,7 +6081,6 @@ mod tests {
op_start_signals.push(op_start_signal.0); op_start_signals.push(op_start_signal.0);
clients.push(host_cx.foreground().spawn(host.simulate_host( clients.push(host_cx.foreground().spawn(host.simulate_host(
host_project, host_project,
files,
op_start_signal.1, op_start_signal.1,
rng.clone(), rng.clone(),
host_cx, host_cx,
@ -6696,7 +6694,6 @@ mod tests {
async fn simulate_host( async fn simulate_host(
mut self, mut self,
project: ModelHandle<Project>, project: ModelHandle<Project>,
files: Arc<Mutex<Vec<PathBuf>>>,
op_start_signal: futures::channel::mpsc::UnboundedReceiver<()>, op_start_signal: futures::channel::mpsc::UnboundedReceiver<()>,
rng: Arc<Mutex<StdRng>>, rng: Arc<Mutex<StdRng>>,
mut cx: TestAppContext, mut cx: TestAppContext,
@ -6704,7 +6701,6 @@ mod tests {
async fn simulate_host_internal( async fn simulate_host_internal(
client: &mut TestClient, client: &mut TestClient,
project: ModelHandle<Project>, project: ModelHandle<Project>,
files: Arc<Mutex<Vec<PathBuf>>>,
mut op_start_signal: futures::channel::mpsc::UnboundedReceiver<()>, mut op_start_signal: futures::channel::mpsc::UnboundedReceiver<()>,
rng: Arc<Mutex<StdRng>>, rng: Arc<Mutex<StdRng>>,
cx: &mut TestAppContext, cx: &mut TestAppContext,
@ -6713,9 +6709,10 @@ mod tests {
while op_start_signal.next().await.is_some() { while op_start_signal.next().await.is_some() {
let distribution = rng.lock().gen_range::<usize, _>(0..100); let distribution = rng.lock().gen_range::<usize, _>(0..100);
let files = fs.as_fake().files().await;
match distribution { match distribution {
0..=20 if !files.lock().is_empty() => { 0..=20 if !files.is_empty() => {
let path = files.lock().choose(&mut *rng.lock()).unwrap().clone(); let path = files.choose(&mut *rng.lock()).unwrap();
let mut path = path.as_path(); let mut path = path.as_path();
while let Some(parent_path) = path.parent() { while let Some(parent_path) = path.parent() {
path = parent_path; path = parent_path;
@ -6734,9 +6731,9 @@ mod tests {
find_or_create_worktree.await?; find_or_create_worktree.await?;
} }
} }
10..=80 if !files.lock().is_empty() => { 10..=80 if !files.is_empty() => {
let buffer = if client.buffers.is_empty() || rng.lock().gen() { let buffer = if client.buffers.is_empty() || rng.lock().gen() {
let file = files.lock().choose(&mut *rng.lock()).unwrap().clone(); let file = files.choose(&mut *rng.lock()).unwrap();
let (worktree, path) = project let (worktree, path) = project
.update(cx, |project, cx| { .update(cx, |project, cx| {
project.find_or_create_local_worktree( project.find_or_create_local_worktree(
@ -6810,7 +6807,6 @@ mod tests {
if fs.create_dir(&parent_path).await.is_ok() if fs.create_dir(&parent_path).await.is_ok()
&& fs.create_file(&path, Default::default()).await.is_ok() && fs.create_file(&path, Default::default()).await.is_ok()
{ {
files.lock().push(path);
break; break;
} else { } else {
log::info!("Host: cannot create file"); log::info!("Host: cannot create file");
@ -6824,15 +6820,9 @@ mod tests {
Ok(()) Ok(())
} }
let result = simulate_host_internal( let result =
&mut self, simulate_host_internal(&mut self, project.clone(), op_start_signal, rng, &mut cx)
project.clone(), .await;
files,
op_start_signal,
rng,
&mut cx,
)
.await;
log::info!("Host done"); log::info!("Host done");
self.project = Some(project); self.project = Some(project);
(self, cx, result.err()) (self, cx, result.err())

View File

@ -376,6 +376,16 @@ impl FakeFs {
.boxed() .boxed()
} }
pub async fn files(&self) -> Vec<PathBuf> {
self.state
.lock()
.await
.entries
.iter()
.filter_map(|(path, entry)| entry.content.as_ref().map(|_| path.clone()))
.collect()
}
async fn simulate_random_delay(&self) { async fn simulate_random_delay(&self) {
self.executor self.executor
.upgrade() .upgrade()