Ensure Buffer::{is_dirty,has_conflict} converge in randomized test

This commit is contained in:
Antonio Scandurra 2023-01-17 16:20:09 +01:00
parent 467e5691b9
commit 2cd9db1cfe

View file

@ -10,7 +10,7 @@ use collections::BTreeMap;
use fs::{FakeFs, Fs as _}; use fs::{FakeFs, Fs as _};
use futures::StreamExt as _; use futures::StreamExt as _;
use gpui::{executor::Deterministic, ModelHandle, TestAppContext}; use gpui::{executor::Deterministic, ModelHandle, TestAppContext};
use language::{range_to_lsp, FakeLspAdapter, Language, LanguageConfig, PointUtf16}; use language::{range_to_lsp, FakeLspAdapter, Language, LanguageConfig, PointUtf16, Rope};
use lsp::FakeLanguageServer; use lsp::FakeLanguageServer;
use parking_lot::Mutex; use parking_lot::Mutex;
use project::{search::SearchQuery, Project}; use project::{search::SearchQuery, Project};
@ -19,7 +19,12 @@ use rand::{
prelude::*, prelude::*,
}; };
use settings::Settings; use settings::Settings;
use std::{env, ffi::OsStr, path::PathBuf, sync::Arc}; use std::{
env,
ffi::OsStr,
path::{Path, PathBuf},
sync::Arc,
};
#[gpui::test(iterations = 100)] #[gpui::test(iterations = 100)]
async fn test_random_collaboration( async fn test_random_collaboration(
@ -398,6 +403,14 @@ async fn test_random_collaboration(
let guest_diff_base = guest_buffer let guest_diff_base = guest_buffer
.read_with(client_cx, |b, _| b.diff_base().map(ToString::to_string)); .read_with(client_cx, |b, _| b.diff_base().map(ToString::to_string));
assert_eq!(guest_diff_base, host_diff_base); assert_eq!(guest_diff_base, host_diff_base);
let host_is_dirty = host_buffer.read_with(host_cx, |b, _| b.is_dirty());
let guest_is_dirty = host_buffer.read_with(host_cx, |b, _| b.is_dirty());
assert_eq!(guest_is_dirty, host_is_dirty);
let host_has_conflict = host_buffer.read_with(host_cx, |b, _| b.has_conflict());
let guest_has_conflict = host_buffer.read_with(host_cx, |b, _| b.has_conflict());
assert_eq!(guest_has_conflict, host_has_conflict);
} }
} }
} }
@ -638,14 +651,7 @@ async fn randomly_mutate_git(client: &mut TestClient, rng: &Mutex<StdRng>) {
client.fs.create_dir(&git_dir_path).await.unwrap(); client.fs.create_dir(&git_dir_path).await.unwrap();
} }
let mut child_paths = client.fs.read_dir(&dir_path).await.unwrap(); let mut child_file_paths = child_file_paths(client, &dir_path).await;
let mut child_file_paths = Vec::new();
while let Some(child_path) = child_paths.next().await {
let child_path = child_path.unwrap();
if client.fs.is_file(&child_path).await {
child_file_paths.push(child_path);
}
}
let count = rng.lock().gen_range(0..=child_file_paths.len()); let count = rng.lock().gen_range(0..=child_file_paths.len());
child_file_paths.shuffle(&mut *rng.lock()); child_file_paths.shuffle(&mut *rng.lock());
child_file_paths.truncate(count); child_file_paths.truncate(count);
@ -669,26 +675,63 @@ async fn randomly_mutate_git(client: &mut TestClient, rng: &Mutex<StdRng>) {
} }
async fn randomly_mutate_fs(client: &mut TestClient, rng: &Mutex<StdRng>) { async fn randomly_mutate_fs(client: &mut TestClient, rng: &Mutex<StdRng>) {
let is_dir = rng.lock().gen::<bool>(); let parent_dir_path = client
let mut new_path = client
.fs .fs
.directories() .directories()
.await .await
.choose(&mut *rng.lock()) .choose(&mut *rng.lock())
.unwrap() .unwrap()
.clone(); .clone();
new_path.push(gen_file_name(rng));
let is_dir = rng.lock().gen::<bool>();
if is_dir { if is_dir {
log::info!("{}: creating local dir at {:?}", client.username, new_path); let mut dir_path = parent_dir_path.clone();
client.fs.create_dir(&new_path).await.unwrap(); dir_path.push(gen_file_name(rng));
log::info!("{}: creating local dir at {:?}", client.username, dir_path);
client.fs.create_dir(&dir_path).await.unwrap();
} else { } else {
new_path.set_extension("rs"); let child_file_paths = child_file_paths(client, &parent_dir_path).await;
log::info!("{}: creating local file at {:?}", client.username, new_path); let create_new_file = child_file_paths.is_empty() || rng.lock().gen();
client let text = Alphanumeric.sample_string(&mut *rng.lock(), 16);
.fs if create_new_file {
.create_file(&new_path, Default::default()) let mut file_path = parent_dir_path.clone();
.await file_path.push(gen_file_name(rng));
.unwrap(); file_path.set_extension("rs");
log::info!(
"{}: creating local file at {:?}",
client.username,
file_path
);
client
.fs
.create_file(&file_path, Default::default())
.await
.unwrap();
log::info!(
"{}: setting local file {:?} text to {:?}",
client.username,
file_path,
text
);
client
.fs
.save(&file_path, &Rope::from(text.as_str()), fs::LineEnding::Unix)
.await
.unwrap();
} else {
let file_path = child_file_paths.choose(&mut *rng.lock()).unwrap();
log::info!(
"{}: setting local file {:?} text to {:?}",
client.username,
file_path,
text
);
client
.fs
.save(file_path, &Rope::from(text.as_str()), fs::LineEnding::Unix)
.await
.unwrap();
}
} }
} }
@ -1154,3 +1197,15 @@ fn gen_file_name(rng: &Mutex<StdRng>) -> String {
} }
name name
} }
async fn child_file_paths(client: &TestClient, dir_path: &Path) -> Vec<PathBuf> {
let mut child_paths = client.fs.read_dir(dir_path).await.unwrap();
let mut child_file_paths = Vec::new();
while let Some(child_path) = child_paths.next().await {
let child_path = child_path.unwrap();
if client.fs.is_file(&child_path).await {
child_file_paths.push(child_path);
}
}
child_file_paths
}