From 113d3b88d0e1aeb31d8488fe1296bc563c4d842e Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Wed, 28 Sep 2022 14:16:21 -0700 Subject: [PATCH] Added test, and fix, for changed_repos method on LocalWorktree --- crates/project/src/worktree.rs | 52 +++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index ae55659f98..81eec4987f 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -524,7 +524,7 @@ impl LocalWorktree { match self.scan_state() { ScanState::Idle => { let new_snapshot = self.background_snapshot.lock().clone(); - let updated_repos = Self::list_updated_repos( + let updated_repos = Self::changed_repos( &self.snapshot.git_repositories, &new_snapshot.git_repositories, ); @@ -545,7 +545,7 @@ impl LocalWorktree { let is_fake_fs = self.fs.is_fake(); let new_snapshot = self.background_snapshot.lock().clone(); - let updated_repos = Self::list_updated_repos( + let updated_repos = Self::changed_repos( &self.snapshot.git_repositories, &new_snapshot.git_repositories, ); @@ -580,7 +580,7 @@ impl LocalWorktree { cx.notify(); } - fn list_updated_repos( + fn changed_repos( old_repos: &[Box], new_repos: &[Box], ) -> Vec> { @@ -595,7 +595,7 @@ impl LocalWorktree { && a_repo.scan_id() == b_repo.scan_id() }); - if matched.is_some() { + if matched.is_none() { updated.insert(a_repo.git_dir_path(), a_repo.boxed_clone()); } } @@ -2955,6 +2955,7 @@ mod tests { use anyhow::Result; use client::test::FakeHttpClient; use fs::RealFs; + use git::repository::FakeGitRepository; use gpui::{executor::Deterministic, TestAppContext}; use rand::prelude::*; use serde_json::json; @@ -3278,6 +3279,49 @@ mod tests { }); } + #[test] + fn test_changed_repos() { + let prev_repos: Vec> = vec![ + FakeGitRepository::open(Path::new("/.git"), 0), + FakeGitRepository::open(Path::new("/a/.git"), 0), + FakeGitRepository::open(Path::new("/a/b/.git"), 0), + ]; + + let new_repos: Vec> = vec![ + FakeGitRepository::open(Path::new("/a/.git"), 1), + FakeGitRepository::open(Path::new("/a/b/.git"), 0), + FakeGitRepository::open(Path::new("/a/c/.git"), 0), + ]; + + let res = LocalWorktree::changed_repos(&prev_repos, &new_repos); + + dbg!(&res); + + // Deletion retained + assert!(res + .iter() + .find(|repo| repo.git_dir_path() == Path::new("/.git") && repo.scan_id() == 0) + .is_some()); + + // Update retained + assert!(res + .iter() + .find(|repo| repo.git_dir_path() == Path::new("/a/.git") && repo.scan_id() == 1) + .is_some()); + + // Addition retained + assert!(res + .iter() + .find(|repo| repo.git_dir_path() == Path::new("/a/c/.git") && repo.scan_id() == 0) + .is_some()); + + // Nochange, not retained + assert!(res + .iter() + .find(|repo| repo.git_dir_path() == Path::new("/a/b/.git") && repo.scan_id() == 0) + .is_none()); + } + #[gpui::test] async fn test_write_file(cx: &mut TestAppContext) { let dir = temp_tree(json!({