Flatten worktree tests module structure

This commit is contained in:
Max Brunsfeld 2023-06-13 10:31:29 -07:00
parent c17dbab6f1
commit 5b6d1a27ff

View file

@ -1,10 +1,10 @@
use crate::{
worktree::{Event, WorktreeHandle},
worktree::{Event, Snapshot, WorktreeHandle},
EntryKind, PathChange, Worktree,
};
use anyhow::Result;
use client::Client;
use fs::{FakeFs, Fs, RealFs, RemoveOptions};
use fs::{repository::GitFileStatus, FakeFs, Fs, RealFs, RemoveOptions};
use git::GITIGNORE;
use gpui::{executor::Deterministic, ModelContext, Task, TestAppContext};
use parking_lot::Mutex;
@ -728,9 +728,9 @@ fn randomly_mutate_worktree(
} else {
other_entry.path.parent().unwrap().into()
};
let mut new_path = new_parent_path.join(gen_name(rng));
let mut new_path = new_parent_path.join(random_filename(rng));
if new_path.starts_with(&entry.path) {
new_path = gen_name(rng).into();
new_path = random_filename(rng).into();
}
log::info!(
@ -747,7 +747,7 @@ fn randomly_mutate_worktree(
}
_ => {
let task = if entry.is_dir() {
let child_path = entry.path.join(gen_name(rng));
let child_path = entry.path.join(random_filename(rng));
let is_dir = rng.gen_bool(0.3);
log::info!(
"creating {} at {:?}",
@ -788,7 +788,7 @@ async fn randomly_mutate_fs(
if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
let path = dirs.choose(rng).unwrap();
let new_path = path.join(gen_name(rng));
let new_path = path.join(random_filename(rng));
if rng.gen() {
log::info!(
@ -880,7 +880,7 @@ async fn randomly_mutate_fs(
.unwrap();
new_path_parent.to_path_buf()
} else {
new_path_parent.join(gen_name(rng))
new_path_parent.join(random_filename(rng))
};
log::info!(
@ -927,23 +927,15 @@ async fn randomly_mutate_fs(
}
}
fn gen_name(rng: &mut impl Rng) -> String {
fn random_filename(rng: &mut impl Rng) -> String {
(0..6)
.map(|_| rng.sample(rand::distributions::Alphanumeric))
.map(char::from)
.collect()
}
mod git_tests {
use crate::{worktree::WorktreeHandle, Snapshot};
use super::*;
use collections::HashMap;
use fs::repository::GitFileStatus;
use pretty_assertions::assert_eq;
#[gpui::test]
async fn test_rename_work_directory(cx: &mut TestAppContext) {
#[gpui::test]
async fn test_rename_work_directory(cx: &mut TestAppContext) {
let root = temp_tree(json!({
"projects": {
"project1": {
@ -1012,10 +1004,10 @@ mod git_tests {
Some(GitFileStatus::Added)
);
});
}
}
#[gpui::test]
async fn test_git_repository_for_path(cx: &mut TestAppContext) {
#[gpui::test]
async fn test_git_repository_for_path(cx: &mut TestAppContext) {
let root = temp_tree(json!({
"c.txt": "",
"dir1": {
@ -1134,10 +1126,10 @@ mod git_tests {
.repository_for_path("dir1/src/b.txt".as_ref())
.is_none());
});
}
}
#[gpui::test]
async fn test_git_status(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
#[gpui::test]
async fn test_git_status(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
const IGNORE_RULE: &'static str = "**/target";
let root = temp_tree(json!({
@ -1324,10 +1316,10 @@ mod git_tests {
Some(GitFileStatus::Added)
);
});
}
}
#[gpui::test]
async fn test_propagate_git_statuses(cx: &mut TestAppContext) {
#[gpui::test]
async fn test_propagate_git_statuses(cx: &mut TestAppContext) {
let fs = FakeFs::new(cx.background());
fs.insert_tree(
"/root",
@ -1445,30 +1437,30 @@ mod git_tests {
expected_statuses
);
}
}
}
#[track_caller]
fn git_init(path: &Path) -> git2::Repository {
#[track_caller]
fn git_init(path: &Path) -> git2::Repository {
git2::Repository::init(path).expect("Failed to initialize git repository")
}
}
#[track_caller]
fn git_add<P: AsRef<Path>>(path: P, repo: &git2::Repository) {
#[track_caller]
fn git_add<P: AsRef<Path>>(path: P, repo: &git2::Repository) {
let path = path.as_ref();
let mut index = repo.index().expect("Failed to get index");
index.add_path(path).expect("Failed to add a.txt");
index.write().expect("Failed to write index");
}
}
#[track_caller]
fn git_remove_index(path: &Path, repo: &git2::Repository) {
#[track_caller]
fn git_remove_index(path: &Path, repo: &git2::Repository) {
let mut index = repo.index().expect("Failed to get index");
index.remove_path(path).expect("Failed to add a.txt");
index.write().expect("Failed to write index");
}
}
#[track_caller]
fn git_commit(msg: &'static str, repo: &git2::Repository) {
#[track_caller]
fn git_commit(msg: &'static str, repo: &git2::Repository) {
use git2::Signature;
let signature = Signature::now("test", "test@zed.dev").unwrap();
@ -1492,19 +1484,19 @@ mod git_tests {
repo.commit(Some("HEAD"), &signature, &signature, msg, &tree, &[])
.expect("Failed to commit");
}
}
}
#[track_caller]
fn git_stash(repo: &mut git2::Repository) {
#[track_caller]
fn git_stash(repo: &mut git2::Repository) {
use git2::Signature;
let signature = Signature::now("test", "test@zed.dev").unwrap();
repo.stash_save(&signature, "N/A", None)
.expect("Failed to stash");
}
}
#[track_caller]
fn git_reset(offset: usize, repo: &git2::Repository) {
#[track_caller]
fn git_reset(offset: usize, repo: &git2::Repository) {
let head = repo.head().expect("Couldn't get repo head");
let object = head.peel(git2::ObjectType::Commit).unwrap();
let commit = object.as_commit().unwrap();
@ -1518,15 +1510,14 @@ mod git_tests {
.expect("Not enough history");
repo.reset(&new_head.as_object(), git2::ResetType::Soft, None)
.expect("Could not reset");
}
}
#[allow(dead_code)]
#[track_caller]
fn git_status(repo: &git2::Repository) -> HashMap<String, git2::Status> {
#[allow(dead_code)]
#[track_caller]
fn git_status(repo: &git2::Repository) -> collections::HashMap<String, git2::Status> {
repo.statuses(None)
.unwrap()
.iter()
.map(|status| (status.path().unwrap().to_string(), status.status()))
.collect()
}
}