From 40d101a0757868203c5a098ec3fe8d03a77a10e9 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 28 Aug 2023 09:34:30 +0200 Subject: [PATCH] init test_utils --- src-tauri/src/deltas/writer.rs | 27 +------ .../src/gb_repository/repository_tests.rs | 47 ++++------- src-tauri/src/lib.rs | 3 + src-tauri/src/reader.rs | 71 +++++++---------- src-tauri/src/search/searcher_test.rs | 78 +++++++------------ src-tauri/src/sessions/tests.rs | 36 +++------ src-tauri/src/test_utils.rs | 28 +++++++ .../src/virtual_branches/branch/reader.rs | 34 +++----- .../src/virtual_branches/branch/writer.rs | 39 +++------- src-tauri/src/virtual_branches/iterator.rs | 32 ++------ .../src/virtual_branches/target/reader.rs | 38 +++------ .../src/virtual_branches/target/writer.rs | 33 ++------ src-tauri/src/virtual_branches/tests.rs | 32 ++------ .../watcher/handlers/project_file_change.rs | 63 ++++++--------- 14 files changed, 190 insertions(+), 371 deletions(-) create mode 100644 src-tauri/src/test_utils.rs diff --git a/src-tauri/src/deltas/writer.rs b/src-tauri/src/deltas/writer.rs index 4bf9f184d..df5aaa313 100644 --- a/src-tauri/src/deltas/writer.rs +++ b/src-tauri/src/deltas/writer.rs @@ -65,35 +65,16 @@ impl<'writer> DeltasWriter<'writer> { mod tests { use std::vec; - use tempfile::tempdir; - - use crate::{deltas, projects, sessions, users}; + use crate::{deltas, projects, sessions, users, test_utils}; use super::*; - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } - #[test] fn write_no_vbranches() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_app_data = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_app_data = test_utils::temp_dir(); let user_store = users::Storage::from(&local_app_data); let project_store = projects::Storage::from(&local_app_data); project_store.add_project(&project)?; diff --git a/src-tauri/src/gb_repository/repository_tests.rs b/src-tauri/src/gb_repository/repository_tests.rs index b83e6ee3f..e1ee93869 100644 --- a/src-tauri/src/gb_repository/repository_tests.rs +++ b/src-tauri/src/gb_repository/repository_tests.rs @@ -1,6 +1,6 @@ use std::{thread, time}; -use crate::{deltas, gb_repository, projects, sessions, users}; +use crate::{deltas, gb_repository, projects, sessions, test_utils, users}; use anyhow::Result; use tempfile::tempdir; @@ -10,23 +10,6 @@ fn remote_repository() -> Result { Ok(repository) } -fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) -} - fn commit_all(repository: &git2::Repository) -> Result { let mut index = repository.index()?; index.add_all(["."], git2::IndexAddOption::DEFAULT, None)?; @@ -46,7 +29,7 @@ fn commit_all(repository: &git2::Repository) -> Result { #[test] fn test_get_current_session_writer_should_use_existing_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -65,7 +48,7 @@ fn test_get_current_session_writer_should_use_existing_session() -> Result<()> { #[test] fn test_must_not_return_init_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -85,7 +68,7 @@ fn test_must_not_return_init_session() -> Result<()> { #[test] fn test_must_not_flush_without_current_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -106,7 +89,7 @@ fn test_must_not_flush_without_current_session() -> Result<()> { #[test] fn test_init_on_non_empty_repository() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -124,7 +107,7 @@ fn test_init_on_non_empty_repository() -> Result<()> { #[test] fn test_flush_on_existing_repository() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -153,7 +136,7 @@ fn test_flush_on_existing_repository() -> Result<()> { #[test] fn test_must_flush_current_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -175,7 +158,7 @@ fn test_must_flush_current_session() -> Result<()> { #[test] fn test_list_deltas_from_current_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -211,7 +194,7 @@ fn test_list_deltas_from_current_session() -> Result<()> { #[test] fn test_list_deltas_from_flushed_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -247,7 +230,7 @@ fn test_list_deltas_from_flushed_session() -> Result<()> { #[test] fn test_list_files_from_current_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -277,7 +260,7 @@ fn test_list_files_from_current_session() -> Result<()> { #[test] fn test_list_files_from_flushed_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); let local_app_data = tempdir()?.path().to_path_buf(); @@ -331,7 +314,7 @@ fn test_remote_syncronization() -> Result<()> { })?; // create first local project, add files, deltas and flush a session - let repository_one = test_repository()?; + let repository_one = test_utils::test_repository(); let project_one = projects::Project::try_from(&repository_one)?; project_store.add_project(&projects::Project { api: Some(api_project.clone()), @@ -359,7 +342,7 @@ fn test_remote_syncronization() -> Result<()> { gb_repo_one.push().unwrap(); // create second local project, fetch it and make sure session is there - let repository_two = test_repository()?; + let repository_two = test_utils::test_repository(); let project_two = projects::Project::try_from(&repository_two)?; project_store.add_project(&projects::Project { api: Some(api_project), @@ -419,7 +402,7 @@ fn test_remote_sync_order() -> Result<()> { })?; // create first project and repo - let repository_one = test_repository()?; + let repository_one = test_utils::test_repository(); let project_one = projects::Project::try_from(&repository_one)?; project_store.add_project(&projects::Project { api: Some(api_project.clone()), @@ -433,7 +416,7 @@ fn test_remote_sync_order() -> Result<()> { )?; // create second project and repo - let repository_two = test_repository()?; + let repository_two = test_utils::test_repository(); let project_two = projects::Project::try_from(&repository_two)?; project_store.add_project(&projects::Project { api: Some(api_project), diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 9eaf5f8b3..39b811411 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -25,3 +25,6 @@ pub mod virtual_branches; pub mod watcher; pub mod writer; pub mod zip; + +#[cfg(test)] +pub mod test_utils; diff --git a/src-tauri/src/reader.rs b/src-tauri/src/reader.rs index b640c6877..f391710d3 100644 --- a/src-tauri/src/reader.rs +++ b/src-tauri/src/reader.rs @@ -274,8 +274,10 @@ impl Reader for SubReader<'_> { #[cfg(test)] mod tests { use super::*; + use anyhow::Result; - use tempfile::tempdir; + + use crate::test_utils; fn commit(repository: &git2::Repository) -> Result { let mut index = repository.index()?; @@ -294,29 +296,12 @@ mod tests { Ok(commit_oid) } - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } - #[test] fn test_directory_reader_is_dir() -> Result<()> { - let dir = tempdir()?; - let reader = DirReader::open(dir.path().to_path_buf()); - std::fs::create_dir(dir.path().join("dir"))?; - std::fs::write(dir.path().join("dir/test.txt"), "test")?; + let dir = test_utils::temp_dir(); + let reader = DirReader::open(dir.clone()); + std::fs::create_dir(dir.join("dir"))?; + std::fs::write(dir.join("dir/test.txt"), "test")?; assert!(reader.is_dir(".")); assert!(reader.is_dir("dir")); assert!(!reader.is_dir("dir/test.txt")); @@ -326,12 +311,12 @@ mod tests { #[test] fn test_directory_reader_read_file() -> Result<()> { - let dir = tempdir()?; + let dir = test_utils::temp_dir(); let file_path = "test.txt"; - std::fs::write(dir.path().join(file_path), "test")?; + std::fs::write(dir.join(file_path), "test")?; - let reader = DirReader::open(dir.path().to_path_buf()); + let reader = DirReader::open(dir.to_path_buf()); assert_eq!(reader.read(file_path)?, Content::UTF8("test".to_string())); Ok(()) @@ -339,7 +324,7 @@ mod tests { #[test] fn test_commit_reader_is_dir() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); std::fs::create_dir(repository.path().parent().unwrap().join("dir"))?; std::fs::write( @@ -357,7 +342,7 @@ mod tests { #[test] fn test_commit_reader_read_file() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let file_path = "test.txt"; std::fs::write(repository.path().parent().unwrap().join(file_path), "test")?; @@ -374,13 +359,13 @@ mod tests { #[test] fn test_reader_list_files_should_return_relative() -> Result<()> { - let dir = tempdir()?; + let dir = test_utils::temp_dir(); - std::fs::write(dir.path().join("test1.txt"), "test")?; - std::fs::create_dir(dir.path().join("dir"))?; - std::fs::write(dir.path().join("dir").join("test.txt"), "test")?; + std::fs::write(dir.join("test1.txt"), "test")?; + std::fs::create_dir(dir.join("dir"))?; + std::fs::write(dir.join("dir").join("test.txt"), "test")?; - let reader = DirReader::open(dir.path().to_path_buf()); + let reader = DirReader::open(dir.to_path_buf()); let files = reader.list_files("dir")?; assert_eq!(files.len(), 1); assert!(files.contains(&"test.txt".to_string())); @@ -390,13 +375,13 @@ mod tests { #[test] fn test_reader_list_files() -> Result<()> { - let dir = tempdir()?; + let dir = test_utils::temp_dir(); - std::fs::write(dir.path().join("test.txt"), "test")?; - std::fs::create_dir(dir.path().join("dir"))?; - std::fs::write(dir.path().join("dir").join("test.txt"), "test")?; + std::fs::write(dir.join("test.txt"), "test")?; + std::fs::create_dir(dir.join("dir"))?; + std::fs::write(dir.join("dir").join("test.txt"), "test")?; - let reader = DirReader::open(dir.path().to_path_buf()); + let reader = DirReader::open(dir.to_path_buf()); let files = reader.list_files("")?; assert_eq!(files.len(), 2); assert!(files.contains(&"test.txt".to_string())); @@ -407,7 +392,7 @@ mod tests { #[test] fn test_commit_reader_list_files_should_return_relative() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); std::fs::write( repository.path().parent().unwrap().join("test1.txt"), @@ -438,7 +423,7 @@ mod tests { #[test] fn test_commit_reader_list_files() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); std::fs::write(repository.path().parent().unwrap().join("test.txt"), "test")?; std::fs::create_dir(repository.path().parent().unwrap().join("dir"))?; @@ -467,11 +452,11 @@ mod tests { #[test] fn test_directory_reader_exists() -> Result<()> { - let dir = tempdir()?; + let dir = test_utils::temp_dir(); - std::fs::write(dir.path().join("test.txt"), "test")?; + std::fs::write(dir.join("test.txt"), "test")?; - let reader = DirReader::open(dir.path().to_path_buf()); + let reader = DirReader::open(dir.to_path_buf()); assert!(reader.exists("test.txt")); assert!(!reader.exists("test2.txt")); @@ -480,7 +465,7 @@ mod tests { #[test] fn test_commit_reader_exists() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); std::fs::write(repository.path().parent().unwrap().join("test.txt"), "test")?; diff --git a/src-tauri/src/search/searcher_test.rs b/src-tauri/src/search/searcher_test.rs index fe4ed40b7..b6906c6b2 100644 --- a/src-tauri/src/search/searcher_test.rs +++ b/src-tauri/src/search/searcher_test.rs @@ -1,40 +1,22 @@ use std::{path::Path, time}; use anyhow::Result; -use tempfile::tempdir; -use crate::{bookmarks, deltas, gb_repository, projects, users}; - -fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) -} +use crate::{bookmarks, deltas, gb_repository, projects, test_utils, users}; #[test] fn test_sorted_by_timestamp() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; let user_store = users::Storage::from(&local_data_dir); let gb_repo = gb_repository::Repository::open(gb_repo_path, &project.id, project_store, user_store)?; - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let writer = deltas::Writer::new(&gb_repo); writer.write( @@ -74,17 +56,17 @@ fn test_sorted_by_timestamp() -> Result<()> { #[test] fn search_by_bookmark_note() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; let user_store = users::Storage::from(&local_data_dir); let gb_repo = gb_repository::Repository::open(gb_repo_path, &project.id, project_store, user_store)?; - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let writer = deltas::Writer::new(&gb_repo); writer.write( @@ -170,17 +152,17 @@ fn search_by_bookmark_note() -> Result<()> { #[test] fn search_by_full_match() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; let user_store = users::Storage::from(&local_data_dir); let gb_repo = gb_repository::Repository::open(gb_repo_path, &project.id, project_store, user_store)?; - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let writer = deltas::Writer::new(&gb_repo); writer.write( @@ -211,17 +193,17 @@ fn search_by_full_match() -> Result<()> { #[test] fn search_by_diff() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; let user_store = users::Storage::from(&local_data_dir); let gb_repo = gb_repository::Repository::open(gb_repo_path, &project.id, project_store, user_store)?; - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let writer = deltas::Writer::new(&gb_repo); writer.write( @@ -262,7 +244,7 @@ fn search_by_diff() -> Result<()> { #[test] fn should_index_bookmark_once() -> Result<()> { - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let searcher = super::Searcher::try_from(&index_path).unwrap(); // should not index deleted non-existing bookmark @@ -330,17 +312,17 @@ fn should_index_bookmark_once() -> Result<()> { #[test] fn test_delete_all() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; let user_store = users::Storage::from(&local_data_dir); let gb_repo = gb_repository::Repository::open(gb_repo_path, &project.id, project_store, user_store)?; - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let writer = deltas::Writer::new(&gb_repo); writer.write( @@ -379,17 +361,17 @@ fn test_delete_all() -> Result<()> { #[test] fn search_bookmark_by_phrase() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; let user_store = users::Storage::from(&local_data_dir); let gb_repo = gb_repository::Repository::open(gb_repo_path, &project.id, project_store, user_store)?; - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let writer = deltas::Writer::new(&gb_repo); writer.write( @@ -435,17 +417,17 @@ fn search_bookmark_by_phrase() -> Result<()> { #[test] fn search_by_filename() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; let user_store = users::Storage::from(&local_data_dir); let gb_repo = gb_repository::Repository::open(gb_repo_path, &project.id, project_store, user_store)?; - let index_path = tempdir()?.path().to_path_buf(); + let index_path = test_utils::temp_dir(); let writer = deltas::Writer::new(&gb_repo); writer.write( diff --git a/src-tauri/src/sessions/tests.rs b/src-tauri/src/sessions/tests.rs index 6d34261fe..390c0c4d9 100644 --- a/src-tauri/src/sessions/tests.rs +++ b/src-tauri/src/sessions/tests.rs @@ -1,37 +1,21 @@ use anyhow::Result; -use tempfile::tempdir; use crate::{ gb_repository, projects::{self, Project}, sessions, users, + test_utils, }; use super::Writer; -fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) -} #[test] fn test_should_not_write_session_with_hash() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_app_data = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_app_data = test_utils::temp_dir(); let user_store = users::Storage::from(&local_app_data); let project_store = projects::Storage::from(&local_app_data); project_store.add_project(&project)?; @@ -56,10 +40,10 @@ fn test_should_not_write_session_with_hash() -> Result<()> { #[test] fn test_should_write_full_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_app_data = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_app_data = test_utils::temp_dir(); let user_store = users::Storage::from(&local_app_data); let project_store = projects::Storage::from(&local_app_data); project_store.add_project(&project)?; @@ -105,10 +89,10 @@ fn test_should_write_full_session() -> Result<()> { #[test] fn test_should_write_partial_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_app_data = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_app_data = test_utils::temp_dir(); let user_store = users::Storage::from(&local_app_data); let project_store = projects::Storage::from(&local_app_data); project_store.add_project(&project)?; diff --git a/src-tauri/src/test_utils.rs b/src-tauri/src/test_utils.rs new file mode 100644 index 000000000..1ed1c4eb3 --- /dev/null +++ b/src-tauri/src/test_utils.rs @@ -0,0 +1,28 @@ +use std::{path, fs}; + +use tempfile::tempdir; + +pub fn temp_dir() -> path::PathBuf { + let path = tempdir().unwrap().path().to_path_buf(); + fs::create_dir_all(&path).unwrap(); + path +} + +pub fn test_repository() -> git2::Repository { + let path = temp_dir(); + let repository = git2::Repository::init(path).expect("failed to init repository"); + let mut index = repository.index().expect("failed to get index"); + let oid = index.write_tree().expect("failed to write tree"); + let signature = git2::Signature::now("test", "test@email.com").unwrap(); + repository + .commit( + Some("HEAD"), + &signature, + &signature, + "Initial commit", + &repository.find_tree(oid).expect("failed to find tree"), + &[], + ) + .expect("failed to commit"); + repository +} diff --git a/src-tauri/src/virtual_branches/branch/reader.rs b/src-tauri/src/virtual_branches/branch/reader.rs index 605678b08..424b2e585 100644 --- a/src-tauri/src/virtual_branches/branch/reader.rs +++ b/src-tauri/src/virtual_branches/branch/reader.rs @@ -29,9 +29,10 @@ impl<'reader> BranchReader<'reader> { #[cfg(test)] mod tests { use anyhow::Result; - use tempfile::tempdir; - use crate::{gb_repository, projects, sessions, users, virtual_branches::branch::Ownership}; + use crate::{ + gb_repository, projects, sessions, test_utils, users, virtual_branches::branch::Ownership, + }; use super::{super::Writer, *}; @@ -72,29 +73,12 @@ mod tests { } } - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } - #[test] fn test_read_not_found() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -114,10 +98,10 @@ mod tests { #[test] fn test_read_override() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; diff --git a/src-tauri/src/virtual_branches/branch/writer.rs b/src-tauri/src/virtual_branches/branch/writer.rs index b3506ac0a..1391227a6 100644 --- a/src-tauri/src/virtual_branches/branch/writer.rs +++ b/src-tauri/src/virtual_branches/branch/writer.rs @@ -106,9 +106,7 @@ impl<'writer> BranchWriter<'writer> { mod tests { use std::fs; - use tempfile::tempdir; - - use crate::{projects, users, virtual_branches::branch}; + use crate::{projects, test_utils, users, virtual_branches::branch}; use super::*; @@ -150,29 +148,12 @@ mod tests { } } - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } - #[test] fn test_write_branch() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -235,10 +216,10 @@ mod tests { #[test] fn test_should_create_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -257,10 +238,10 @@ mod tests { #[test] fn test_should_update() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; diff --git a/src-tauri/src/virtual_branches/iterator.rs b/src-tauri/src/virtual_branches/iterator.rs index 9ca9e88e7..46137092e 100644 --- a/src-tauri/src/virtual_branches/iterator.rs +++ b/src-tauri/src/virtual_branches/iterator.rs @@ -46,29 +46,11 @@ impl<'iterator> Iterator for BranchIterator<'iterator> { #[cfg(test)] mod tests { use anyhow::Result; - use tempfile::tempdir; - use crate::{gb_repository, projects, sessions, users, virtual_branches::target}; + use crate::{gb_repository, projects, sessions, test_utils, users, virtual_branches::target}; use super::*; - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } - static mut TEST_INDEX: usize = 0; fn test_branch() -> branch::Branch { @@ -119,10 +101,10 @@ mod tests { #[test] fn test_empty_iterator() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_app_data = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_app_data = test_utils::temp_dir(); let user_store = users::Storage::from(&local_app_data); let project_store = projects::Storage::from(&local_app_data); project_store.add_project(&project)?; @@ -141,10 +123,10 @@ mod tests { #[test] fn test_iterate_all() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_app_data = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_app_data = test_utils::temp_dir(); let user_store = users::Storage::from(&local_app_data); let project_store = projects::Storage::from(&local_app_data); project_store.add_project(&project)?; diff --git a/src-tauri/src/virtual_branches/target/reader.rs b/src-tauri/src/virtual_branches/target/reader.rs index d3e54c2b0..77167a28b 100644 --- a/src-tauri/src/virtual_branches/target/reader.rs +++ b/src-tauri/src/virtual_branches/target/reader.rs @@ -35,10 +35,9 @@ impl<'reader> TargetReader<'reader> { mod tests { use crate::writer::Writer; use anyhow::Result; - use tempfile::tempdir; use crate::{ - gb_repository, projects, sessions, users, + gb_repository, projects, sessions, test_utils, users, virtual_branches::{branch, target::writer::TargetWriter}, }; @@ -82,29 +81,12 @@ mod tests { } } - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } - #[test] fn test_read_not_found() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_repo_path = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_repo_path = test_utils::temp_dir(); let user_store = users::Storage::from(&local_repo_path); let project_store = projects::Storage::from(&local_repo_path); project_store.add_project(&project)?; @@ -124,10 +106,10 @@ mod tests { #[test] fn test_read_deprecated_format() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_path = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_path = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_path); let project_store = projects::Storage::from(&local_data_path); project_store.add_project(&project)?; @@ -172,10 +154,10 @@ mod tests { #[test] fn test_read_override_target() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_app_data = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_app_data = test_utils::temp_dir(); let user_store = users::Storage::from(&local_app_data); let project_store = projects::Storage::from(&local_app_data); project_store.add_project(&project)?; diff --git a/src-tauri/src/virtual_branches/target/writer.rs b/src-tauri/src/virtual_branches/target/writer.rs index a9e3aa8eb..d3a0c803b 100644 --- a/src-tauri/src/virtual_branches/target/writer.rs +++ b/src-tauri/src/virtual_branches/target/writer.rs @@ -82,9 +82,7 @@ impl<'writer> TargetWriter<'writer> { mod tests { use std::fs; - use tempfile::tempdir; - - use crate::{projects, users, virtual_branches::branch}; + use crate::{projects, users, virtual_branches::branch, test_utils}; use super::{super::Target, *}; @@ -126,29 +124,12 @@ mod tests { } } - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } - #[test] fn test_write() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_repo_path = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_repo_path = test_utils::temp_dir(); let user_store = users::Storage::from(&local_repo_path); let project_store = projects::Storage::from(&local_repo_path); project_store.add_project(&project)?; @@ -237,10 +218,10 @@ mod tests { #[test] fn test_should_update() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_repo_path = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_repo_path = test_utils::temp_dir(); let user_store = users::Storage::from(&local_repo_path); let project_store = projects::Storage::from(&local_repo_path); project_store.add_project(&project)?; diff --git a/src-tauri/src/virtual_branches/tests.rs b/src-tauri/src/virtual_branches/tests.rs index be9e35766..c6ad3085b 100644 --- a/src-tauri/src/virtual_branches/tests.rs +++ b/src-tauri/src/virtual_branches/tests.rs @@ -4,13 +4,12 @@ use std::{ io::Write, os::unix::fs::{symlink, PermissionsExt}, thread, - time::Duration, + time::Duration, path, }; use anyhow::{Context, Result}; -use tempfile::tempdir; -use crate::{gb_repository, project_repository, projects, reader, sessions, users}; +use crate::{gb_repository, project_repository, projects, reader, sessions, test_utils, users}; use super::branch::{Branch, BranchCreateRequest, Ownership}; use super::*; @@ -19,16 +18,16 @@ pub struct TestDeps { repository: git2::Repository, project: projects::Project, gb_repo: gb_repository::Repository, - gb_repo_path: String, + gb_repo_path: path::PathBuf, user_store: users::Storage, project_store: projects::Storage, } fn new_test_deps() -> Result { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string(); - let local_data_dir = tempdir()?.path().to_path_buf(); + let gb_repo_path = test_utils::temp_dir(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -87,25 +86,6 @@ fn set_test_target( Ok(()) } -fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - //dbg!(&path); - let repository = git2::Repository::init(path)?; - repository.remote_add_fetch("origin/master", "master")?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) -} - #[test] fn test_commit_on_branch_then_change_file_then_get_status() -> Result<()> { let TestDeps { diff --git a/src-tauri/src/watcher/handlers/project_file_change.rs b/src-tauri/src/watcher/handlers/project_file_change.rs index 62755b87c..2ae619fe7 100644 --- a/src-tauri/src/watcher/handlers/project_file_change.rs +++ b/src-tauri/src/watcher/handlers/project_file_change.rs @@ -209,11 +209,10 @@ impl Handler { #[cfg(test)] mod test { - use tempfile::tempdir; - use crate::{ deltas, gb_repository, project_repository, projects, sessions, users, virtual_branches::{self, branch}, + test_utils, }; use super::*; @@ -283,29 +282,13 @@ mod test { Ok(commit_oid) } - fn test_repository() -> Result { - let path = tempdir()?.path().to_str().unwrap().to_string(); - let repository = git2::Repository::init(path)?; - let mut index = repository.index()?; - let oid = index.write_tree()?; - let signature = git2::Signature::now("test", "test@email.com").unwrap(); - repository.commit( - Some("HEAD"), - &signature, - &signature, - "Initial commit", - &repository.find_tree(oid)?, - &[], - )?; - Ok(repository) - } #[test] fn test_register_existing_commited_file() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -345,10 +328,10 @@ mod test { #[test] fn test_register_must_init_current_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -372,10 +355,10 @@ mod test { #[test] fn test_register_must_not_override_current_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -404,10 +387,10 @@ mod test { #[test] fn test_register_new_file() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -444,10 +427,10 @@ mod test { #[test] fn test_register_new_file_twice() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -503,10 +486,10 @@ mod test { #[test] fn test_register_file_delted() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -555,9 +538,9 @@ mod test { #[test] fn test_flow_with_commits() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -646,9 +629,9 @@ mod test { #[test] fn test_flow_no_commits() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -736,9 +719,9 @@ mod test { #[test] fn test_flow_signle_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -796,10 +779,10 @@ mod test { #[test] fn should_persist_branches_targets_state_between_sessions() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?; @@ -859,10 +842,10 @@ mod test { #[test] fn should_restore_branches_targets_state_from_head_session() -> Result<()> { - let repository = test_repository()?; + let repository = test_utils::test_repository(); let project = projects::Project::try_from(&repository)?; let project_repo = project_repository::Repository::open(&project)?; - let local_data_dir = tempdir()?.path().to_path_buf(); + let local_data_dir = test_utils::temp_dir(); let user_store = users::Storage::from(&local_data_dir); let project_store = projects::Storage::from(&local_data_dir); project_store.add_project(&project)?;