2024-03-01 17:14:30 +03:00
|
|
|
pub(crate) mod common;
|
|
|
|
mod suite {
|
|
|
|
mod gb_repository;
|
|
|
|
mod projects;
|
|
|
|
mod virtual_branches;
|
|
|
|
}
|
|
|
|
|
2023-10-04 13:50:16 +03:00
|
|
|
use std::{collections::HashMap, fs, path};
|
2023-08-28 10:34:30 +03:00
|
|
|
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
2024-01-31 16:29:02 +03:00
|
|
|
use crate::{database, gb_repository, git, keys, project_repository, projects, storage, users};
|
2023-10-04 13:50:16 +03:00
|
|
|
|
|
|
|
pub struct Suite {
|
2024-01-31 16:29:02 +03:00
|
|
|
pub local_app_data: path::PathBuf,
|
2023-10-06 16:41:45 +03:00
|
|
|
pub storage: storage::Storage,
|
|
|
|
pub users: users::Controller,
|
2023-10-06 16:56:43 +03:00
|
|
|
pub projects: projects::Controller,
|
2023-10-06 16:41:45 +03:00
|
|
|
pub keys: keys::Controller,
|
2023-10-04 13:50:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Suite {
|
|
|
|
fn default() -> Self {
|
2024-01-31 16:29:02 +03:00
|
|
|
let local_app_data = temp_dir();
|
2024-03-06 16:44:08 +03:00
|
|
|
let storage = storage::Storage::new(&local_app_data);
|
|
|
|
let users = users::Controller::from_path(&local_app_data);
|
|
|
|
let projects = projects::Controller::from_path(&local_app_data);
|
|
|
|
let keys = keys::Controller::from_path(&local_app_data);
|
2023-10-04 13:50:16 +03:00
|
|
|
Self {
|
2023-10-06 16:41:45 +03:00
|
|
|
storage,
|
2023-10-04 13:50:16 +03:00
|
|
|
local_app_data,
|
2023-10-06 16:41:45 +03:00
|
|
|
users,
|
|
|
|
projects,
|
|
|
|
keys,
|
2023-10-04 13:50:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Suite {
|
|
|
|
pub fn sign_in(&self) -> users::User {
|
|
|
|
let user = users::User {
|
2023-11-30 15:04:36 +03:00
|
|
|
name: Some("test".to_string()),
|
2023-10-04 13:50:16 +03:00
|
|
|
email: "test@email.com".to_string(),
|
2023-11-27 20:25:50 +03:00
|
|
|
access_token: "token".to_string(),
|
2023-10-04 13:50:16 +03:00
|
|
|
..Default::default()
|
|
|
|
};
|
2023-10-06 16:41:45 +03:00
|
|
|
self.users.set_user(&user).expect("failed to add user");
|
2023-10-04 13:50:16 +03:00
|
|
|
user
|
|
|
|
}
|
|
|
|
|
2023-10-06 13:43:32 +03:00
|
|
|
fn project(&self, fs: HashMap<path::PathBuf, &str>) -> projects::Project {
|
2023-10-04 13:50:16 +03:00
|
|
|
let repository = test_repository();
|
|
|
|
for (path, contents) in fs {
|
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
fs::create_dir_all(repository.path().parent().unwrap().join(parent))
|
|
|
|
.expect("failed to create dir");
|
|
|
|
}
|
|
|
|
fs::write(
|
|
|
|
repository.path().parent().unwrap().join(&path),
|
|
|
|
contents.as_bytes(),
|
|
|
|
)
|
|
|
|
.expect("failed to write file");
|
|
|
|
}
|
|
|
|
commit_all(&repository);
|
|
|
|
|
2023-10-06 16:56:43 +03:00
|
|
|
self.projects
|
|
|
|
.add(repository.path().parent().unwrap())
|
|
|
|
.expect("failed to add project")
|
2023-10-06 13:43:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_case_with_files(&self, fs: HashMap<path::PathBuf, &str>) -> Case {
|
|
|
|
let project = self.project(fs);
|
|
|
|
Case::new(self, project)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_case(&self) -> Case {
|
|
|
|
self.new_case_with_files(HashMap::new())
|
2023-10-04 13:50:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 16:53:00 +03:00
|
|
|
pub struct Case<'a> {
|
|
|
|
suite: &'a Suite,
|
2023-10-06 13:43:32 +03:00
|
|
|
pub project: projects::Project,
|
2023-10-04 13:50:16 +03:00
|
|
|
pub project_repository: project_repository::Repository,
|
|
|
|
pub gb_repository: gb_repository::Repository,
|
2023-12-06 19:24:48 +03:00
|
|
|
pub credentials: git::credentials::Helper,
|
2023-10-04 13:50:16 +03:00
|
|
|
}
|
2023-09-14 13:23:25 +03:00
|
|
|
|
2023-10-06 13:43:32 +03:00
|
|
|
impl<'a> Case<'a> {
|
|
|
|
fn new(suite: &'a Suite, project: projects::Project) -> Case<'a> {
|
2023-11-14 17:19:22 +03:00
|
|
|
let project_repository = project_repository::Repository::open(&project)
|
2023-10-06 13:43:32 +03:00
|
|
|
.expect("failed to create project repository");
|
|
|
|
let gb_repository =
|
|
|
|
gb_repository::Repository::open(&suite.local_app_data, &project_repository, None)
|
|
|
|
.expect("failed to open gb repository");
|
2024-03-06 17:47:08 +03:00
|
|
|
let credentials = git::credentials::Helper::from_path(&suite.local_app_data);
|
2023-10-06 13:43:32 +03:00
|
|
|
Case {
|
|
|
|
suite,
|
|
|
|
project,
|
|
|
|
gb_repository,
|
|
|
|
project_repository,
|
2023-10-17 15:19:47 +03:00
|
|
|
credentials,
|
2023-10-06 13:43:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn refresh(&self) -> Self {
|
|
|
|
let project = self
|
2023-10-04 16:53:00 +03:00
|
|
|
.suite
|
2023-10-06 16:41:45 +03:00
|
|
|
.projects
|
2023-10-05 11:04:36 +03:00
|
|
|
.get(&self.project.id)
|
|
|
|
.expect("failed to get project");
|
2023-11-14 17:19:22 +03:00
|
|
|
let project_repository = project_repository::Repository::open(&project)
|
2023-10-04 16:53:00 +03:00
|
|
|
.expect("failed to create project repository");
|
2023-10-17 15:19:47 +03:00
|
|
|
let user = self.suite.users.get_user().expect("failed to get user");
|
2024-03-06 17:47:08 +03:00
|
|
|
let credentials = git::credentials::Helper::from_path(&self.suite.local_app_data);
|
2023-10-06 13:43:32 +03:00
|
|
|
Self {
|
|
|
|
suite: self.suite,
|
|
|
|
gb_repository: gb_repository::Repository::open(
|
|
|
|
&self.suite.local_app_data,
|
|
|
|
&project_repository,
|
2023-10-17 15:19:47 +03:00
|
|
|
user.as_ref(),
|
2023-10-06 13:43:32 +03:00
|
|
|
)
|
|
|
|
.expect("failed to open gb repository"),
|
2023-10-17 15:19:47 +03:00
|
|
|
credentials,
|
2023-10-06 13:43:32 +03:00
|
|
|
project_repository,
|
|
|
|
project,
|
|
|
|
}
|
2023-10-04 16:53:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 13:23:25 +03:00
|
|
|
pub fn test_database() -> database::Database {
|
2024-03-06 17:19:51 +03:00
|
|
|
database::Database::open_in_directory(temp_dir()).unwrap()
|
2023-09-14 13:23:25 +03:00
|
|
|
}
|
2023-08-30 10:46:52 +03:00
|
|
|
|
2023-08-28 10:34:30 +03:00
|
|
|
pub fn temp_dir() -> path::PathBuf {
|
|
|
|
let path = tempdir().unwrap().path().to_path_buf();
|
|
|
|
fs::create_dir_all(&path).unwrap();
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2023-10-11 14:56:46 +03:00
|
|
|
pub fn empty_bare_repository() -> git::Repository {
|
|
|
|
let path = temp_dir();
|
2024-03-26 16:29:21 +03:00
|
|
|
git::Repository::init_opts(path, &init_opts_bare()).expect("failed to init repository")
|
2023-10-11 14:56:46 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 10:46:52 +03:00
|
|
|
pub fn test_repository() -> git::Repository {
|
2023-08-28 10:34:30 +03:00
|
|
|
let path = temp_dir();
|
2024-03-26 16:29:21 +03:00
|
|
|
let repository =
|
|
|
|
git::Repository::init_opts(path, &init_opts()).expect("failed to init repository");
|
2023-08-28 10:34:30 +03:00
|
|
|
let mut index = repository.index().expect("failed to get index");
|
|
|
|
let oid = index.write_tree().expect("failed to write tree");
|
2023-09-20 12:53:59 +03:00
|
|
|
let signature = git::Signature::now("test", "test@email.com").unwrap();
|
2023-08-28 10:34:30 +03:00
|
|
|
repository
|
|
|
|
.commit(
|
2023-11-22 14:20:07 +03:00
|
|
|
Some(&"refs/heads/master".parse().unwrap()),
|
2023-08-28 10:34:30 +03:00
|
|
|
&signature,
|
|
|
|
&signature,
|
|
|
|
"Initial commit",
|
|
|
|
&repository.find_tree(oid).expect("failed to find tree"),
|
|
|
|
&[],
|
|
|
|
)
|
|
|
|
.expect("failed to commit");
|
|
|
|
repository
|
|
|
|
}
|
2023-08-28 11:28:26 +03:00
|
|
|
|
2023-08-30 18:03:00 +03:00
|
|
|
pub fn commit_all(repository: &git::Repository) -> git::Oid {
|
2023-08-28 11:28:26 +03:00
|
|
|
let mut index = repository.index().expect("failed to get index");
|
|
|
|
index
|
|
|
|
.add_all(["."], git2::IndexAddOption::DEFAULT, None)
|
|
|
|
.expect("failed to add all");
|
|
|
|
index.write().expect("failed to write index");
|
|
|
|
let oid = index.write_tree().expect("failed to write tree");
|
2023-09-20 12:53:59 +03:00
|
|
|
let signature = git::Signature::now("test", "test@email.com").unwrap();
|
2023-11-22 14:20:07 +03:00
|
|
|
let head = repository.head().expect("failed to get head");
|
2023-08-28 11:28:26 +03:00
|
|
|
let commit_oid = repository
|
|
|
|
.commit(
|
2023-11-22 14:20:07 +03:00
|
|
|
Some(&head.name().unwrap()),
|
2023-08-28 11:28:26 +03:00
|
|
|
&signature,
|
|
|
|
&signature,
|
|
|
|
"some commit",
|
|
|
|
&repository.find_tree(oid).expect("failed to find tree"),
|
|
|
|
&[&repository
|
|
|
|
.find_commit(
|
|
|
|
repository
|
|
|
|
.refname_to_id("HEAD")
|
|
|
|
.expect("failed to get head"),
|
|
|
|
)
|
|
|
|
.expect("failed to find commit")],
|
|
|
|
)
|
|
|
|
.expect("failed to commit");
|
|
|
|
commit_oid
|
|
|
|
}
|
2024-03-26 16:29:21 +03:00
|
|
|
|
|
|
|
fn init_opts() -> git2::RepositoryInitOptions {
|
|
|
|
let mut opts = git2::RepositoryInitOptions::new();
|
|
|
|
opts.initial_head("master");
|
|
|
|
opts
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_opts_bare() -> git2::RepositoryInitOptions {
|
|
|
|
let mut opts = init_opts();
|
|
|
|
opts.bare(true);
|
|
|
|
opts
|
|
|
|
}
|