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

Antonio Scandurra created

Change summary

crates/collab/src/tests/randomized_integration_tests.rs | 99 ++++++++--
1 file changed, 77 insertions(+), 22 deletions(-)

Detailed changes

crates/collab/src/tests/randomized_integration_tests.rs 🔗

@@ -10,7 +10,7 @@ use collections::BTreeMap;
 use fs::{FakeFs, Fs as _};
 use futures::StreamExt as _;
 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 parking_lot::Mutex;
 use project::{search::SearchQuery, Project};
@@ -19,7 +19,12 @@ use rand::{
     prelude::*,
 };
 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)]
 async fn test_random_collaboration(
@@ -398,6 +403,14 @@ async fn test_random_collaboration(
                 let guest_diff_base = guest_buffer
                     .read_with(client_cx, |b, _| b.diff_base().map(ToString::to_string));
                 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();
     }
 
-    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);
-        }
-    }
+    let mut child_file_paths = child_file_paths(client, &dir_path).await;
     let count = rng.lock().gen_range(0..=child_file_paths.len());
     child_file_paths.shuffle(&mut *rng.lock());
     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>) {
-    let is_dir = rng.lock().gen::<bool>();
-    let mut new_path = client
+    let parent_dir_path = client
         .fs
         .directories()
         .await
         .choose(&mut *rng.lock())
         .unwrap()
         .clone();
-    new_path.push(gen_file_name(rng));
+
+    let is_dir = rng.lock().gen::<bool>();
     if is_dir {
-        log::info!("{}: creating local dir at {:?}", client.username, new_path);
-        client.fs.create_dir(&new_path).await.unwrap();
+        let mut dir_path = parent_dir_path.clone();
+        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 {
-        new_path.set_extension("rs");
-        log::info!("{}: creating local file at {:?}", client.username, new_path);
-        client
-            .fs
-            .create_file(&new_path, Default::default())
-            .await
-            .unwrap();
+        let child_file_paths = child_file_paths(client, &parent_dir_path).await;
+        let create_new_file = child_file_paths.is_empty() || rng.lock().gen();
+        let text = Alphanumeric.sample_string(&mut *rng.lock(), 16);
+        if create_new_file {
+            let mut file_path = parent_dir_path.clone();
+            file_path.push(gen_file_name(rng));
+            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
 }
+
+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
+}