2024-03-31 00:29:45 +03:00
|
|
|
use gitbutler_core::projects::Controller;
|
2024-03-29 12:04:26 +03:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2024-04-06 11:08:50 +03:00
|
|
|
use gitbutler_testsupport::{self, paths};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
pub fn new() -> (Controller, TempDir) {
|
|
|
|
let data_dir = paths::data_dir();
|
|
|
|
let controller = Controller::from_path(&data_dir);
|
|
|
|
(controller, data_dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
mod add {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn success() {
|
|
|
|
let (controller, _tmp) = new();
|
2024-04-06 11:08:50 +03:00
|
|
|
let repository = gitbutler_testsupport::TestProject::default();
|
2024-03-29 12:04:26 +03:00
|
|
|
let path = repository.path();
|
|
|
|
let project = controller.add(path).unwrap();
|
|
|
|
assert_eq!(project.path, path);
|
|
|
|
assert_eq!(project.title, path.iter().last().unwrap().to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
mod error {
|
2024-03-31 00:29:45 +03:00
|
|
|
use gitbutler_core::projects::AddError;
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing() {
|
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
assert!(matches!(
|
|
|
|
controller.add(tmp.path().join("missing")),
|
|
|
|
Err(AddError::PathNotFound)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_git() {
|
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
let path = tmp.path();
|
|
|
|
std::fs::write(path.join("file.txt"), "hello world").unwrap();
|
|
|
|
assert!(matches!(
|
|
|
|
controller.add(path),
|
|
|
|
Err(AddError::NotAGitRepository)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
assert!(matches!(
|
|
|
|
controller.add(tmp.path()),
|
|
|
|
Err(AddError::NotAGitRepository)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn twice() {
|
|
|
|
let (controller, _tmp) = new();
|
2024-04-06 11:08:50 +03:00
|
|
|
let repository = gitbutler_testsupport::TestProject::default();
|
2024-03-29 12:04:26 +03:00
|
|
|
let path = repository.path();
|
|
|
|
controller.add(path).unwrap();
|
|
|
|
assert!(matches!(controller.add(path), Err(AddError::AlreadyExists)));
|
|
|
|
}
|
2024-04-17 16:28:14 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn worktree() {
|
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
let main_worktree_dir = tmp.path().join("main");
|
|
|
|
let worktree_dir = tmp.path().join("worktree");
|
|
|
|
|
2024-04-18 16:43:58 +03:00
|
|
|
let repo = git2::Repository::init(main_worktree_dir).unwrap();
|
2024-04-17 16:28:14 +03:00
|
|
|
create_initial_commit(&repo);
|
|
|
|
|
|
|
|
let worktree = repo.worktree("feature", &worktree_dir, None).unwrap();
|
|
|
|
let err = controller.add(worktree.path()).unwrap_err();
|
2024-04-17 19:34:56 +03:00
|
|
|
assert_eq!(err.to_string(), "worktrees unsupported");
|
2024-04-17 16:28:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_initial_commit(repo: &git2::Repository) -> git2::Oid {
|
|
|
|
let signature = git2::Signature::now("test", "test@email.com").unwrap();
|
|
|
|
|
|
|
|
let mut index = repo.index().unwrap();
|
|
|
|
let oid = index.write_tree().unwrap();
|
|
|
|
|
|
|
|
repo.commit(
|
|
|
|
Some("HEAD"),
|
|
|
|
&signature,
|
|
|
|
&signature,
|
|
|
|
"initial commit",
|
|
|
|
&repo.find_tree(oid).unwrap(),
|
|
|
|
&[],
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
}
|