2024-07-08 16:33:26 +03:00
|
|
|
use gitbutler_project::Controller;
|
2024-04-06 11:08:50 +03:00
|
|
|
use gitbutler_testsupport::{self, paths};
|
2024-07-28 21:48:13 +03:00
|
|
|
use tempfile::TempDir;
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
pub fn new() -> (Controller, TempDir) {
|
|
|
|
let data_dir = paths::data_dir();
|
2024-04-24 10:20:55 +03:00
|
|
|
let controller = Controller::from_path(data_dir.path());
|
2024-03-29 12:04:26 +03:00
|
|
|
(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 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing() {
|
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
2024-05-31 17:42:52 +03:00
|
|
|
assert_eq!(
|
|
|
|
controller
|
|
|
|
.add(tmp.path().join("missing"))
|
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"path not found"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-05-31 17:42:52 +03:00
|
|
|
fn directory_without_git() {
|
2024-03-29 12:04:26 +03:00
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
let path = tmp.path();
|
|
|
|
std::fs::write(path.join("file.txt"), "hello world").unwrap();
|
2024-05-31 17:42:52 +03:00
|
|
|
assert_eq!(
|
|
|
|
controller.add(path).unwrap_err().to_string(),
|
|
|
|
"must be a Git repository"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
2024-05-31 17:42:52 +03:00
|
|
|
let err = controller.add(tmp.path()).unwrap_err();
|
|
|
|
assert_eq!(err.to_string(), "must be a Git repository");
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
2024-05-31 17:42:52 +03:00
|
|
|
assert_eq!(
|
|
|
|
controller.add(path).unwrap_err().to_string(),
|
|
|
|
"project already exists"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
2024-04-17 16:28:14 +03:00
|
|
|
|
2024-04-21 18:33:50 +03:00
|
|
|
#[test]
|
|
|
|
fn bare() {
|
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
let repo_dir = tmp.path().join("bare");
|
|
|
|
|
|
|
|
let repo = git2::Repository::init_bare(&repo_dir).unwrap();
|
|
|
|
create_initial_commit(&repo);
|
|
|
|
|
|
|
|
let err = controller.add(repo_dir).unwrap_err();
|
2024-05-31 17:42:52 +03:00
|
|
|
assert_eq!(err.to_string(), "bare repositories are unsupported");
|
2024-04-21 18:33:50 +03:00
|
|
|
}
|
|
|
|
|
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-05-30 11:44:42 +03:00
|
|
|
assert_eq!(err.to_string(), "can only work in main worktrees");
|
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
|
|
|
}
|
|
|
|
}
|
2024-04-24 18:38:07 +03:00
|
|
|
|
|
|
|
mod delete {
|
|
|
|
use super::*;
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn success() {
|
2024-04-24 18:38:07 +03:00
|
|
|
let (controller, _tmp) = new();
|
|
|
|
let repository = gitbutler_testsupport::TestProject::default();
|
|
|
|
let path = repository.path();
|
|
|
|
let project = controller.add(path).unwrap();
|
2024-07-28 21:36:55 +03:00
|
|
|
assert!(controller.delete(project.id).is_ok());
|
|
|
|
assert!(controller.delete(project.id).is_ok()); // idempotent
|
2024-05-29 11:47:11 +03:00
|
|
|
assert!(controller.get(project.id).is_err());
|
2024-04-24 18:38:07 +03:00
|
|
|
assert!(!project.gb_dir().exists());
|
|
|
|
assert!(!project.path.join(".gitbutler.json").exists());
|
|
|
|
}
|
|
|
|
}
|