Start a test for remote git data updating

Co-Authored-By: Mikayla Maki <mikayla@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Julia 2022-09-30 15:50:55 -04:00
parent 6540936970
commit ce7f6dd082
10 changed files with 272 additions and 57 deletions

View file

@ -13,12 +13,15 @@ git2 = { version = "0.15", default-features = false }
lazy_static = "1.4.0"
sum_tree = { path = "../sum_tree" }
text = { path = "../text" }
collections = { path = "../collections" }
util = { path = "../util" }
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
smol = "1.2"
parking_lot = "0.11.1"
async-trait = "0.1"
[dev-dependencies]
unindent = "0.1.7"
[features]
test-support = []

View file

@ -222,6 +222,40 @@ impl BufferDiff {
}
}
/// Range (crossing new lines), old, new
#[cfg(any(test, feature = "test-support"))]
#[track_caller]
pub fn assert_hunks<Iter>(
diff_hunks: Iter,
buffer: &BufferSnapshot,
head_text: &str,
expected_hunks: &[(Range<u32>, &str, &str)],
) where
Iter: Iterator<Item = DiffHunk<u32>>,
{
let actual_hunks = diff_hunks
.map(|hunk| {
(
hunk.buffer_range.clone(),
&head_text[hunk.head_byte_range],
buffer
.text_for_range(
Point::new(hunk.buffer_range.start, 0)
..Point::new(hunk.buffer_range.end, 0),
)
.collect::<String>(),
)
})
.collect::<Vec<_>>();
let expected_hunks: Vec<_> = expected_hunks
.iter()
.map(|(r, s, h)| (r.clone(), *s, h.to_string()))
.collect();
assert_eq!(actual_hunks, expected_hunks);
}
#[cfg(test)]
mod tests {
use super::*;
@ -248,21 +282,19 @@ mod tests {
let mut diff = BufferDiff::new();
smol::block_on(diff.update(&head_text, &buffer));
assert_hunks(
&diff,
diff.hunks(&buffer),
&buffer,
&head_text,
&[(1..2, "two\n", "HELLO\n")],
None,
);
buffer.edit([(0..0, "point five\n")]);
smol::block_on(diff.update(&head_text, &buffer));
assert_hunks(
&diff,
diff.hunks(&buffer),
&buffer,
&head_text,
&[(0..1, "", "point five\n"), (2..3, "two\n", "HELLO\n")],
None,
);
}
@ -309,7 +341,7 @@ mod tests {
assert_eq!(diff.hunks(&buffer).count(), 8);
assert_hunks(
&diff,
diff.hunks_in_range(7..12, &buffer),
&buffer,
&head_text,
&[
@ -317,39 +349,6 @@ mod tests {
(9..10, "six\n", "SIXTEEN\n"),
(12..13, "", "WORLD\n"),
],
Some(7..12),
);
}
#[track_caller]
fn assert_hunks(
diff: &BufferDiff,
buffer: &BufferSnapshot,
head_text: &str,
expected_hunks: &[(Range<u32>, &str, &str)],
range: Option<Range<u32>>,
) {
let actual_hunks = diff
.hunks_in_range(range.unwrap_or(0..u32::MAX), buffer)
.map(|hunk| {
(
hunk.buffer_range.clone(),
&head_text[hunk.head_byte_range],
buffer
.text_for_range(
Point::new(hunk.buffer_range.start, 0)
..Point::new(hunk.buffer_range.end, 0),
)
.collect::<String>(),
)
})
.collect::<Vec<_>>();
let expected_hunks: Vec<_> = expected_hunks
.iter()
.map(|(r, s, h)| (r.clone(), *s, h.to_string()))
.collect();
assert_eq!(actual_hunks, expected_hunks);
}
}

View file

@ -1,7 +1,11 @@
use anyhow::Result;
use collections::HashMap;
use git2::Repository as LibGitRepository;
use parking_lot::Mutex;
use std::{path::Path, sync::Arc};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use util::ResultExt;
#[async_trait::async_trait]
@ -140,14 +144,25 @@ pub struct FakeGitRepository {
content_path: Arc<Path>,
git_dir_path: Arc<Path>,
scan_id: usize,
state: Arc<Mutex<FakeGitRepositoryState>>,
}
#[derive(Debug, Clone, Default)]
pub struct FakeGitRepositoryState {
pub index_contents: HashMap<PathBuf, String>,
}
impl FakeGitRepository {
pub fn open(dotgit_path: &Path, scan_id: usize) -> Box<dyn GitRepository> {
pub fn open(
dotgit_path: &Path,
scan_id: usize,
state: Arc<Mutex<FakeGitRepositoryState>>,
) -> Box<dyn GitRepository> {
Box::new(FakeGitRepository {
content_path: dotgit_path.parent().unwrap().into(),
git_dir_path: dotgit_path.into(),
scan_id,
state,
})
}
}
@ -174,12 +189,13 @@ impl GitRepository for FakeGitRepository {
self.scan_id
}
async fn load_head_text(&self, _: &Path) -> Option<String> {
None
async fn load_head_text(&self, path: &Path) -> Option<String> {
let state = self.state.lock();
state.index_contents.get(path).cloned()
}
fn reopen_git_repo(&mut self) -> bool {
false
true
}
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>> {