test repository open

This commit is contained in:
Nikita Galaiko 2023-02-20 13:37:44 +01:00
parent f21fc69170
commit ec06b264d2
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
5 changed files with 79 additions and 19 deletions

View File

@ -40,7 +40,7 @@ fn list_sessions(
project_id: &str,
) -> Result<Vec<sessions::Session>, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let projects_storage = projects::Storage::new(storage.clone());
let users_storage = users::Storage::new(storage);
@ -65,7 +65,7 @@ fn list_sessions(
#[tauri::command]
fn get_user(handle: tauri::AppHandle) -> Result<Option<users::User>, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let users_storage = users::Storage::new(storage);
users_storage.get().map_err(|e| {
@ -79,7 +79,7 @@ fn get_user(handle: tauri::AppHandle) -> Result<Option<users::User>, Error> {
#[tauri::command]
fn set_user(handle: tauri::AppHandle, user: users::User) -> Result<(), Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let users_storage = users::Storage::new(storage);
users_storage.set(&user).map_err(|e| {
@ -94,7 +94,7 @@ fn set_user(handle: tauri::AppHandle, user: users::User) -> Result<(), Error> {
#[tauri::command]
fn delete_user(handle: tauri::AppHandle) -> Result<(), Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let users_storage = users::Storage::new(storage);
users_storage.delete().map_err(|e| {
@ -112,7 +112,7 @@ fn update_project(
project: projects::UpdateRequest,
) -> Result<projects::Project, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let projects_storage = projects::Storage::new(storage);
projects_storage.update_project(&project).map_err(|e| {
@ -130,7 +130,7 @@ fn add_project(
path: &str,
) -> Result<projects::Project, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let projects_storage = projects::Storage::new(storage.clone());
let users_storage = users::Storage::new(storage);
let watchers_collection = handle.state::<watchers::WatcherCollection>();
@ -179,7 +179,7 @@ fn add_project(
#[tauri::command]
fn list_projects(handle: tauri::AppHandle) -> Result<Vec<projects::Project>, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let projects_storage = projects::Storage::new(storage);
projects_storage.list_projects().map_err(|e| {
@ -193,7 +193,7 @@ fn list_projects(handle: tauri::AppHandle) -> Result<Vec<projects::Project>, Err
#[tauri::command]
fn delete_project(handle: tauri::AppHandle, id: &str) -> Result<(), Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let projects_storage = projects::Storage::new(storage.clone());
let watchers_collection = handle.state::<watchers::WatcherCollection>();
let users_storage = users::Storage::new(storage);
@ -238,7 +238,7 @@ fn list_session_files(
session_id: &str,
) -> Result<HashMap<String, String>, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let projects_storage = projects::Storage::new(storage.clone());
let users_storage = users::Storage::new(storage);
@ -267,7 +267,7 @@ fn list_deltas(
session_id: &str,
) -> Result<HashMap<String, Vec<Delta>>, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::new(&path_resolver);
let storage = storage::Storage::from_path_resolver(&path_resolver);
let projects_storage = projects::Storage::new(storage.clone());
let users_storage = users::Storage::new(storage);
@ -366,7 +366,7 @@ fn main() {
resolver.app_local_data_dir().unwrap()
);
let storage = Storage::new(&resolver);
let storage = Storage::from_path_resolver(&resolver);
let projects_storage = projects::Storage::new(storage.clone());
let users_storage = users::Storage::new(storage);
let watcher_collection = watchers::WatcherCollection::default();

View File

@ -0,0 +1,6 @@
mod repositories;
pub use repositories::Repository;
#[cfg(test)]
mod tests;

View File

@ -11,16 +11,18 @@ impl Repository {
pub fn open(
projects_storage: &projects::Storage,
users_storage: &users::Storage,
repository_id: &str,
project_id: &str,
) -> Result<Self> {
let project = projects_storage
.get_project(repository_id)
.context("failed to get project")?
.ok_or_else(|| anyhow::anyhow!("project {} not found", repository_id))?;
let user = users_storage.get().context("failed to get user")?;
.get_project(project_id)
.with_context(|| "failed to get project")?
.ok_or_else(|| anyhow::anyhow!("project {} not found", project_id))?;
let user = users_storage
.get()
.with_context(|| "failed to get user for project")?;
let git_repository =
git2::Repository::open(&project.path).context("failed to open repository")?;
init(&git_repository, &project, &user)?;
git2::Repository::open(&project.path).with_context(|| "failed to open repository")?;
init(&git_repository, &project, &user).with_context(|| "failed to init repository")?;
Ok(Repository {
project,
git_repository,

View File

@ -0,0 +1,45 @@
use super::Repository;
use crate::{projects, storage, users};
use anyhow::Result;
use tempfile::tempdir;
fn test_project() -> Result<projects::Project> {
let path = tempdir()?.path().to_str().unwrap().to_string();
std::fs::create_dir_all(&path)?;
let repo = git2::Repository::init(&path)?;
let mut index = repo.index()?;
let oid = index.write_tree()?;
let sig = repo.signature()?;
let _commit = repo.commit(
Some("HEAD"),
&sig,
&sig,
"initial commit",
&repo.find_tree(oid)?,
&[],
)?;
let project = projects::Project::from_path(path)?;
Ok(project)
}
#[test]
fn test_open_always_with_session() {
let storage_path = tempdir().unwrap();
let storage = storage::Storage::from_path(storage_path.path().to_path_buf());
let project = test_project().unwrap();
let projects_storage = projects::Storage::new(storage.clone());
projects_storage.add_project(&project).unwrap();
let users_storage = users::Storage::new(storage.clone());
let repository = Repository::open(&projects_storage, &users_storage, &project.id);
assert!(repository.is_ok());
let repository = repository.unwrap();
let sessions = repository.sessions().unwrap();
assert_eq!(sessions.len(), 1);
assert!(sessions[0].hash.is_some());
}

View File

@ -8,7 +8,14 @@ pub struct Storage {
}
impl Storage {
pub fn new(resolver: &PathResolver) -> Self {
#[cfg(test)]
pub fn from_path(path: PathBuf) -> Self {
Storage {
local_data_dir: path,
}
}
pub fn from_path_resolver(resolver: &PathResolver) -> Self {
Self {
local_data_dir: resolver.app_local_data_dir().unwrap(),
}