diff --git a/gitbutler-app/src/database.rs b/gitbutler-app/src/database.rs index b0e4fef87..f7eb35ded 100644 --- a/gitbutler-app/src/database.rs +++ b/gitbutler-app/src/database.rs @@ -33,6 +33,15 @@ impl TryFrom<&AppHandle> for Database { } } +#[cfg(test)] +impl TryFrom<&path::PathBuf> for Database { + type Error = anyhow::Error; + + fn try_from(value: &path::PathBuf) -> Result { + Self::open(value.join("database.sqlite3")) + } +} + impl Database { fn open>(path: P) -> Result { let path = path.as_ref(); diff --git a/gitbutler-app/src/deltas/database.rs b/gitbutler-app/src/deltas/database.rs index 174cc0ad3..92abc29af 100644 --- a/gitbutler-app/src/deltas/database.rs +++ b/gitbutler-app/src/deltas/database.rs @@ -145,7 +145,7 @@ mod tests { #[test] fn insert_query() -> Result<()> { let db = test_utils::test_database(); - let database = Database::from(db); + let database = Database::new(db); let project_id = ProjectId::generate(); let session_id = SessionId::generate(); @@ -171,7 +171,7 @@ mod tests { #[test] fn insert_update() -> Result<()> { let db = test_utils::test_database(); - let database = Database::from(db); + let database = Database::new(db); let project_id = ProjectId::generate(); let session_id = SessionId::generate(); @@ -204,7 +204,7 @@ mod tests { #[test] fn aggregate_deltas_by_file() -> Result<()> { let db = test_utils::test_database(); - let database = Database::from(db); + let database = Database::new(db); let project_id = ProjectId::generate(); let session_id = SessionId::generate(); diff --git a/gitbutler-app/src/git/credentials.rs b/gitbutler-app/src/git/credentials.rs index 2ba6ebd60..67d7b7a0f 100644 --- a/gitbutler-app/src/git/credentials.rs +++ b/gitbutler-app/src/git/credentials.rs @@ -93,6 +93,17 @@ impl TryFrom<&AppHandle> for Helper { } } +impl TryFrom<&std::path::PathBuf> for Helper { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + let keys = keys::Controller::try_from(value)?; + let users = users::Controller::try_from(value)?; + let home_dir = env::var_os("HOME").map(path::PathBuf::from); + Ok(Helper::new(keys, users, home_dir)) + } +} + #[derive(Debug, thiserror::Error)] pub enum HelpError { #[error("no url set for remote")] @@ -420,14 +431,14 @@ mod tests { fn run(&self) -> Vec<(String, Vec)> { let local_app_data = test_utils::temp_dir(); - let users = users::Controller::from(&local_app_data); + let users = users::Controller::try_from(&local_app_data).unwrap(); let user = users::User { github_access_token: self.github_access_token.map(ToString::to_string), ..Default::default() }; users.set_user(&user).unwrap(); - let keys = keys::Controller::from(&local_app_data); + let keys = keys::Controller::try_from(&local_app_data).unwrap(); let helper = Helper::new(keys, users, self.home_dir.clone()); let repo = test_repository(); diff --git a/gitbutler-app/src/keys/controller.rs b/gitbutler-app/src/keys/controller.rs index 912824988..b66e2da14 100644 --- a/gitbutler-app/src/keys/controller.rs +++ b/gitbutler-app/src/keys/controller.rs @@ -22,6 +22,14 @@ impl TryFrom<&AppHandle> for Controller { } } +impl TryFrom<&std::path::PathBuf> for Controller { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + Ok(Controller::new(Storage::try_from(value)?)) + } +} + impl Controller { fn new(storage: Storage) -> Self { Self { storage } @@ -58,7 +66,7 @@ mod tests { #[test] fn test_get_or_create() { let suite = Suite::default(); - let controller = Controller::from(&suite.local_app_data); + let controller = Controller::try_from(&suite.local_app_data).unwrap(); let once = controller.get_or_create().unwrap(); let twice = controller.get_or_create().unwrap(); diff --git a/gitbutler-app/src/keys/storage.rs b/gitbutler-app/src/keys/storage.rs index 8cc223ec4..48dbe7ab2 100644 --- a/gitbutler-app/src/keys/storage.rs +++ b/gitbutler-app/src/keys/storage.rs @@ -31,6 +31,14 @@ impl TryFrom<&AppHandle> for Storage { } } +impl TryFrom<&std::path::PathBuf> for Storage { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + Ok(Storage::new(storage::Storage::try_from(value)?)) + } +} + impl Storage { fn new(storage: storage::Storage) -> Storage { Storage { storage } diff --git a/gitbutler-app/src/projects/controller.rs b/gitbutler-app/src/projects/controller.rs index 9473e30fd..1fae9949a 100644 --- a/gitbutler-app/src/projects/controller.rs +++ b/gitbutler-app/src/projects/controller.rs @@ -34,6 +34,19 @@ impl TryFrom<&AppHandle> for Controller { } } +impl TryFrom<&std::path::PathBuf> for Controller { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + Ok(Self { + local_data_dir: value.clone(), + projects_storage: storage::Storage::try_from(value)?, + users: users::Controller::try_from(value)?, + watchers: None, + }) + } +} + impl Controller { pub fn add(&self, path: &path::Path) -> Result { let all_projects = self diff --git a/gitbutler-app/src/projects/storage.rs b/gitbutler-app/src/projects/storage.rs index a14f3e6d7..19b77a16c 100644 --- a/gitbutler-app/src/projects/storage.rs +++ b/gitbutler-app/src/projects/storage.rs @@ -27,6 +27,14 @@ impl TryFrom<&AppHandle> for Storage { } } +impl TryFrom<&std::path::PathBuf> for Storage { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + Ok(Storage::new(storage::Storage::try_from(value)?)) + } +} + #[derive(Debug, Serialize, Deserialize, Default)] pub struct UpdateRequest { pub id: ProjectId, diff --git a/gitbutler-app/src/sessions/database.rs b/gitbutler-app/src/sessions/database.rs index c2687e180..a9bed61b1 100644 --- a/gitbutler-app/src/sessions/database.rs +++ b/gitbutler-app/src/sessions/database.rs @@ -205,7 +205,9 @@ mod tests { #[test] fn test_insert_query() -> Result<()> { let db = test_utils::test_database(); - let database = Database::from(db); + println!("0"); + let database = Database::new(db); + println!("1"); let project_id = ProjectId::generate(); let session1 = session::Session { @@ -246,7 +248,7 @@ mod tests { #[test] fn test_update() -> Result<()> { let db = test_utils::test_database(); - let database = Database::from(db); + let database = Database::new(db); let project_id = ProjectId::generate(); let session = session::Session { diff --git a/gitbutler-app/src/storage.rs b/gitbutler-app/src/storage.rs index 4e8bfbaf0..1982cc5a0 100644 --- a/gitbutler-app/src/storage.rs +++ b/gitbutler-app/src/storage.rs @@ -36,6 +36,14 @@ impl TryFrom<&AppHandle> for Storage { } } +impl TryFrom<&PathBuf> for Storage { + type Error = anyhow::Error; + + fn try_from(value: &PathBuf) -> Result { + Ok(Storage::new(value)) + } +} + impl Storage { fn new>(local_data_dir: P) -> Storage { Storage { diff --git a/gitbutler-app/src/test_utils.rs b/gitbutler-app/src/test_utils.rs index 42e2c3f48..107d40191 100644 --- a/gitbutler-app/src/test_utils.rs +++ b/gitbutler-app/src/test_utils.rs @@ -15,10 +15,14 @@ pub struct Suite { impl Default for Suite { fn default() -> Self { let local_app_data = temp_dir(); - let storage = storage::Storage::from(&local_app_data); - let users = users::Controller::from(&storage); - let projects = projects::Controller::from(&local_app_data); - let keys = keys::Controller::from(&storage); + let storage = + storage::Storage::try_from(&local_app_data).expect("failed to create storage"); + let users = users::Controller::try_from(&local_app_data) + .expect("failed to create users controller"); + let projects = projects::Controller::try_from(&local_app_data) + .expect("failed to create projects controller"); + let keys = + keys::Controller::try_from(&local_app_data).expect("failed to create keys controller"); Self { storage, local_app_data, @@ -86,7 +90,8 @@ impl<'a> Case<'a> { let gb_repository = gb_repository::Repository::open(&suite.local_app_data, &project_repository, None) .expect("failed to open gb repository"); - let credentials = git::credentials::Helper::from(&suite.local_app_data); + let credentials = git::credentials::Helper::try_from(&suite.local_app_data) + .expect("failed to get credentials"); Case { suite, project, @@ -105,7 +110,8 @@ impl<'a> Case<'a> { let project_repository = project_repository::Repository::open(&project) .expect("failed to create project repository"); let user = self.suite.users.get_user().expect("failed to get user"); - let credentials = git::credentials::Helper::from(&self.suite.local_app_data); + let credentials = git::credentials::Helper::try_from(&self.suite.local_app_data) + .expect("failed to get credentials"); Self { suite: self.suite, gb_repository: gb_repository::Repository::open( @@ -122,8 +128,7 @@ impl<'a> Case<'a> { } pub fn test_database() -> database::Database { - let path = temp_dir().join("test.db"); - database::Database::try_from(&path).unwrap() + database::Database::try_from(&temp_dir()).unwrap() } pub fn temp_dir() -> path::PathBuf { diff --git a/gitbutler-app/src/users/controller.rs b/gitbutler-app/src/users/controller.rs index 67219888e..facc5832e 100644 --- a/gitbutler-app/src/users/controller.rs +++ b/gitbutler-app/src/users/controller.rs @@ -22,6 +22,14 @@ impl TryFrom<&AppHandle> for Controller { } } +impl TryFrom<&std::path::PathBuf> for Controller { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + Ok(Controller::new(Storage::try_from(value)?)) + } +} + impl Controller { fn new(storage: Storage) -> Controller { Controller { storage } diff --git a/gitbutler-app/src/users/storage.rs b/gitbutler-app/src/users/storage.rs index 6a25d0980..e02541f5b 100644 --- a/gitbutler-app/src/users/storage.rs +++ b/gitbutler-app/src/users/storage.rs @@ -32,6 +32,14 @@ impl TryFrom<&AppHandle> for Storage { } } +impl TryFrom<&std::path::PathBuf> for Storage { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + Ok(Storage::new(storage::Storage::try_from(value)?)) + } +} + impl Storage { fn new(storage: storage::Storage) -> Storage { Storage { storage } diff --git a/gitbutler-app/src/watcher/handlers.rs b/gitbutler-app/src/watcher/handlers.rs index a72cbae46..0adada829 100644 --- a/gitbutler-app/src/watcher/handlers.rs +++ b/gitbutler-app/src/watcher/handlers.rs @@ -65,6 +65,7 @@ impl TryFrom<&AppHandle> for Handler { } impl Handler { + #[allow(clippy::too_many_arguments)] fn new( git_file_change_handler: git_file_change::Handler, tick_handler: tick_handler::Handler, diff --git a/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs b/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs index 4e0e87586..b26da3a7b 100644 --- a/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs +++ b/gitbutler-app/src/watcher/handlers/calculate_deltas_handler.rs @@ -38,6 +38,21 @@ impl TryFrom<&AppHandle> for Handler { } } +#[cfg(test)] +impl TryFrom<&std::path::PathBuf> for Handler { + type Error = anyhow::Error; + + fn try_from(value: &std::path::PathBuf) -> Result { + let app_data_dir = value.clone(); + let handler = Self::new( + app_data_dir, + projects::Controller::try_from(value)?, + users::Controller::try_from(value)?, + ); + Ok(handler) + } +} + impl Handler { fn new( local_data_dir: path::PathBuf, @@ -262,7 +277,7 @@ mod test { project, .. } = suite.new_case_with_files(HashMap::from([(path::PathBuf::from("test.txt"), "test")])); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); std::fs::write(project.path.join("test.txt"), "test2")?; listener.handle("test.txt", &project.id)?; @@ -293,7 +308,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); std::fs::write(project.path.join("test.txt"), "test")?; listener.handle("test.txt", &project.id)?; @@ -311,7 +326,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); std::fs::write(project.path.join("test.txt"), "test")?; listener.handle("test.txt", &project.id)?; @@ -334,7 +349,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); std::fs::write( project.path.join("test.bin"), @@ -366,7 +381,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); std::fs::write(project.path.join("test.txt"), "")?; @@ -394,7 +409,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); std::fs::write(project.path.join("test.txt"), "test")?; @@ -427,7 +442,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); // file change, wd and deltas are written std::fs::write(project.path.join("test.txt"), "test")?; @@ -455,7 +470,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); std::fs::write(project.path.join("test.txt"), "test")?; listener.handle("test.txt", &project.id)?; @@ -507,7 +522,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); { // write file @@ -586,7 +601,7 @@ mod test { project_repository, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); let size = 10; let relative_file_path = std::path::Path::new("one/two/test.txt"); @@ -673,7 +688,7 @@ mod test { project_repository, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); let size = 10; let relative_file_path = std::path::Path::new("one/two/test.txt"); @@ -758,7 +773,7 @@ mod test { project, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); let size = 10_i32; let relative_file_path = std::path::Path::new("one/two/test.txt"); @@ -816,7 +831,7 @@ mod test { path::PathBuf::from("test.txt"), "hello world", )])); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); let branch_writer = virtual_branches::branch::Writer::new(&gb_repository)?; let target_writer = virtual_branches::target::Writer::new(&gb_repository)?; @@ -872,7 +887,7 @@ mod test { path::PathBuf::from("test.txt"), "hello world", )])); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); let branch_writer = virtual_branches::branch::Writer::new(&gb_repository)?; let target_writer = virtual_branches::target::Writer::new(&gb_repository)?; @@ -931,7 +946,7 @@ mod test { project_repository, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); // write a file into session std::fs::write(project.path.join("test.txt"), "hello world!").unwrap(); @@ -1007,7 +1022,7 @@ mod test { project_repository, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); // write a file into session std::fs::write(project.path.join("test.txt"), "hello world!").unwrap(); @@ -1083,7 +1098,7 @@ mod test { project_repository, .. } = suite.new_case(); - let listener = Handler::from(&suite.local_app_data); + let listener = Handler::try_from(&suite.local_app_data).unwrap(); // write a file into session std::fs::write(project.path.join("test.txt"), "hello world!").unwrap(); diff --git a/gitbutler-app/src/watcher/handlers/git_file_change.rs b/gitbutler-app/src/watcher/handlers/git_file_change.rs index 09e9a0538..2748eba94 100644 --- a/gitbutler-app/src/watcher/handlers/git_file_change.rs +++ b/gitbutler-app/src/watcher/handlers/git_file_change.rs @@ -198,8 +198,9 @@ mod test { fn create_new_session_via_new_file(project: &projects::Project, suite: &Suite) { fs::write(project.path.join("test.txt"), "test").unwrap(); + let file_change_listener = - handlers::calculate_deltas_handler::Handler::from(&suite.local_app_data); + handlers::calculate_deltas_handler::Handler::try_from(&suite.local_app_data).unwrap(); file_change_listener .handle("test.txt", &project.id) .unwrap(); diff --git a/gitbutler-app/src/zip.rs b/gitbutler-app/src/zip.rs index 49f616fa2..9be4c07f8 100644 --- a/gitbutler-app/src/zip.rs +++ b/gitbutler-app/src/zip.rs @@ -199,7 +199,7 @@ mod tests { file.write_all(b"test").unwrap(); let zipper_cache = tempdir().unwrap(); - let zipper = Zipper::from(&zipper_cache.path().to_path_buf()); + let zipper = Zipper::new(zipper_cache.path()).unwrap(); let zip_file_path = zipper.zip(tmp_dir).unwrap(); assert!(zip_file_path.exists()); } @@ -213,7 +213,7 @@ mod tests { file.write_all(b"test").unwrap(); let zipper_cache = tempdir().unwrap(); - let zipper = Zipper::from(&zipper_cache.path().to_path_buf()); + let zipper = Zipper::new(zipper_cache.path()).unwrap(); zipper.zip(file_path).unwrap_err(); } @@ -226,7 +226,7 @@ mod tests { file.write_all(b"test").unwrap(); let zipper_cache = tempdir().unwrap(); - let zipper = Zipper::from(&zipper_cache.path().to_path_buf()); + let zipper = Zipper::new(zipper_cache.path()).unwrap(); assert_eq!(zipper.zip(&tmp_dir).unwrap(), zipper.zip(&tmp_dir).unwrap()); assert_eq!(WalkDir::new(tmp_dir).into_iter().count(), 1); } diff --git a/gitbutler-app/tests/gb_repository.rs b/gitbutler-app/tests/gb_repository.rs index 6a6f0d9b5..67ec3a5a1 100644 --- a/gitbutler-app/tests/gb_repository.rs +++ b/gitbutler-app/tests/gb_repository.rs @@ -12,7 +12,7 @@ mod init { let test_project = TestProject::default(); let data_dir = paths::data_dir(); - let projects = projects::Controller::from(&data_dir); + let projects = projects::Controller::try_from(&data_dir).unwrap(); let project = projects .add(test_project.path()) @@ -31,7 +31,7 @@ mod init { let test_project = TestProject::default(); let data_dir = paths::data_dir(); - let projects = projects::Controller::from(&data_dir); + let projects = projects::Controller::try_from(&data_dir).unwrap(); let project = projects .add(test_project.path()) @@ -52,7 +52,7 @@ mod init { let test_project = TestProject::default(); let data_dir = paths::data_dir(); - let projects = projects::Controller::from(&data_dir); + let projects = projects::Controller::try_from(&data_dir).unwrap(); let project = projects .add(test_project.path()) @@ -82,7 +82,7 @@ mod flush { let test_project = TestProject::default(); let data_dir = paths::data_dir(); - let projects = projects::Controller::from(&data_dir); + let projects = projects::Controller::try_from(&data_dir).unwrap(); let project = projects .add(test_project.path()) @@ -104,7 +104,7 @@ mod flush { let test_project = TestProject::default(); let data_dir = paths::data_dir(); - let projects = projects::Controller::from(&data_dir); + let projects = projects::Controller::try_from(&data_dir).unwrap(); let project = projects .add(test_project.path()) @@ -128,7 +128,7 @@ mod flush { let test_project = TestProject::default(); let data_dir = paths::data_dir(); - let projects = projects::Controller::from(&data_dir); + let projects = projects::Controller::try_from(&data_dir).unwrap(); let project = projects .add(test_project.path()) diff --git a/gitbutler-app/tests/projects.rs b/gitbutler-app/tests/projects.rs index 697af51b6..c3255f6d2 100644 --- a/gitbutler-app/tests/projects.rs +++ b/gitbutler-app/tests/projects.rs @@ -5,7 +5,7 @@ use gblib::projects::Controller; pub fn new() -> Controller { let data_dir = paths::data_dir(); - Controller::from(&data_dir) + Controller::try_from(&data_dir).unwrap() } mod add { diff --git a/gitbutler-app/tests/virtual_branches.rs b/gitbutler-app/tests/virtual_branches.rs index ac8212dad..1ea303106 100644 --- a/gitbutler-app/tests/virtual_branches.rs +++ b/gitbutler-app/tests/virtual_branches.rs @@ -29,10 +29,10 @@ struct Test { impl Default for Test { fn default() -> Self { let data_dir = paths::data_dir(); - let keys = keys::Controller::from(&data_dir); - let projects = projects::Controller::from(&data_dir); - let users = users::Controller::from(&data_dir); - let helper = git::credentials::Helper::from(&data_dir); + let keys = keys::Controller::try_from(&data_dir).unwrap(); + let projects = projects::Controller::try_from(&data_dir).unwrap(); + let users = users::Controller::try_from(&data_dir).unwrap(); + let helper = git::credentials::Helper::try_from(&data_dir).unwrap(); let test_project = TestProject::default(); let project = projects @@ -42,7 +42,7 @@ impl Default for Test { Self { repository: test_project, project_id: project.id, - controller: Controller::new(&data_dir, &projects, &users, &keys, &helper), + controller: Controller::new(data_dir, projects.clone(), users, keys, helper), projects, } } @@ -4496,14 +4496,14 @@ mod init { #[tokio::test] async fn twice() { let data_dir = paths::data_dir(); - let keys = keys::Controller::from(&data_dir); - let projects = projects::Controller::from(&data_dir); - let users = users::Controller::from(&data_dir); - let helper = git::credentials::Helper::from(&data_dir); + let keys = keys::Controller::try_from(&data_dir).unwrap(); + let projects = projects::Controller::try_from(&data_dir).unwrap(); + let users = users::Controller::try_from(&data_dir).unwrap(); + let helper = git::credentials::Helper::try_from(&data_dir).unwrap(); let test_project = TestProject::default(); - let controller = Controller::new(&data_dir, &projects, &users, &keys, &helper); + let controller = Controller::new(data_dir, projects.clone(), users, keys, helper); { let project = projects