From b8835941f9dfca1b4a915c05e9c8bdcc660d8712 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 6 Jun 2024 18:31:48 +0100 Subject: [PATCH] WIP: test: add more tests to git crate --- crates/config/src/branch_name.rs | 3 ++ crates/config/src/git_dir.rs | 4 ++ crates/config/src/server_repo_config.rs | 2 + crates/config/test/src/given.rs | 40 +++++++++++++++-- crates/git/Cargo.toml | 2 + crates/git/src/commit.rs | 1 + crates/git/src/file.rs | 34 ++++++++------- crates/git/src/tests.rs | 52 +++++++++++++++++++--- crates/git/test/Cargo.toml | 1 + crates/git/test/src/given.rs | 57 ++++++++++++++++++++++--- 10 files changed, 166 insertions(+), 30 deletions(-) diff --git a/crates/config/src/branch_name.rs b/crates/config/src/branch_name.rs index 70df7c6..5086151 100644 --- a/crates/config/src/branch_name.rs +++ b/crates/config/src/branch_name.rs @@ -5,4 +5,7 @@ impl BranchName { pub fn new(str: impl Into) -> Self { Self(str.into()) } + pub fn into_string(self) -> String { + self.0 + } } diff --git a/crates/config/src/git_dir.rs b/crates/config/src/git_dir.rs index f27596d..57c0cc4 100644 --- a/crates/config/src/git_dir.rs +++ b/crates/config/src/git_dir.rs @@ -20,6 +20,10 @@ impl GitDir { pub const fn pathbuf(&self) -> &PathBuf { &self.0 } + + pub fn into_string(self) -> String { + self.to_string() + } } impl std::fmt::Display for GitDir { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/crates/config/src/server_repo_config.rs b/crates/config/src/server_repo_config.rs index 7e6e37f..c973f2e 100644 --- a/crates/config/src/server_repo_config.rs +++ b/crates/config/src/server_repo_config.rs @@ -9,7 +9,9 @@ use crate::{BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource, Repo )] #[display("{}@{}", repo, branch)] pub struct ServerRepoConfig { + /// repo path: owner/repo repo: String, + /// branch name branch: String, gitdir: Option, main: Option, diff --git a/crates/config/test/src/given.rs b/crates/config/test/src/given.rs index aeec1ae..5110567 100644 --- a/crates/config/test/src/given.rs +++ b/crates/config/test/src/given.rs @@ -1,9 +1,9 @@ // use config::{ - server::Webhook, webhook::message::Body, ForgeAlias, RepoAlias, RepoBranches, WebhookAuth, - WebhookId, + server::Webhook, webhook::message::Body, BranchName, ForgeAlias, RepoAlias, RepoBranches, + WebhookAuth, WebhookId, }; -use git_next_config as config; +use git_next_config::{self as config, ForgeConfig, ForgeType, GitDir, ServerRepoConfig}; use rand::RngCore; @@ -68,3 +68,37 @@ pub fn a_webhook_id() -> WebhookId { pub fn a_github_webhook_id() -> i64 { rand::thread_rng().next_u32().into() } + +pub fn a_branch_name() -> BranchName { + BranchName::new(a_name()) +} + +pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir { + let dir_name = a_name(); + let dir = fs.base().join(dir_name); + GitDir::new(&dir) +} + +pub fn a_forge_config() -> ForgeConfig { + ForgeConfig::new( + ForgeType::MockForge, + a_name(), + a_name(), + a_name(), + Default::default(), // no repos + ) +} + +pub fn a_server_repo_config() -> ServerRepoConfig { + let main = a_branch_name().into_string(); + let next = a_branch_name().into_string(); + let dev = a_branch_name().into_string(); + ServerRepoConfig::new( + format!("{}/{}", a_name(), a_name()), + main.clone(), + None, + Some(main), + Some(next), + Some(dev), + ) +} diff --git a/crates/git/Cargo.toml b/crates/git/Cargo.toml index c34de0b..f3ed2b8 100644 --- a/crates/git/Cargo.toml +++ b/crates/git/Cargo.toml @@ -53,6 +53,8 @@ actix = { workspace = true } # [dev-dependencies] # Testing +git-next-config-test = { workspace = true } +git-next-git-test = { workspace = true } assert2 = { workspace = true } [lints.clippy] diff --git a/crates/git/src/commit.rs b/crates/git/src/commit.rs index 8948d7f..fcbb3f4 100644 --- a/crates/git/src/commit.rs +++ b/crates/git/src/commit.rs @@ -52,6 +52,7 @@ pub mod log { } impl From for Error { + #[cfg(not(tarpaulin_include))] fn from(e: String) -> Self { Self::Gix(e) } diff --git a/crates/git/src/file.rs b/crates/git/src/file.rs index c545d7e..34be9d0 100644 --- a/crates/git/src/file.rs +++ b/crates/git/src/file.rs @@ -52,26 +52,30 @@ pub enum Error { TryId, } -impl From for Error { - fn from(value: gix::reference::find::existing::Error) -> Self { - Self::FindReference(value.to_string()) +mod gix_errors { + #![cfg(not(tarpaulin_include))] // third-party library errors + use super::Error; + impl From for Error { + fn from(value: gix::reference::find::existing::Error) -> Self { + Self::FindReference(value.to_string()) + } } -} -impl From for Error { - fn from(value: gix::object::commit::Error) -> Self { - Self::NoTreeInCommit(value.to_string()) + impl From for Error { + fn from(value: gix::object::commit::Error) -> Self { + Self::NoTreeInCommit(value.to_string()) + } } -} -impl From for Error { - fn from(value: gix::object::find::existing::Error) -> Self { - Self::FindObject(value.to_string()) + impl From for Error { + fn from(value: gix::object::find::existing::Error) -> Self { + Self::FindObject(value.to_string()) + } } -} -impl From for Error { - fn from(value: std::string::FromUtf8Error) -> Self { - Self::NonUtf8Blob(value.to_string()) + impl From for Error { + fn from(value: std::string::FromUtf8Error) -> Self { + Self::NonUtf8Blob(value.to_string()) + } } } diff --git a/crates/git/src/tests.rs b/crates/git/src/tests.rs index 12674f7..d067503 100644 --- a/crates/git/src/tests.rs +++ b/crates/git/src/tests.rs @@ -1,22 +1,36 @@ +use crate as git; + mod commit { - use crate::{commit, Commit}; + use super::*; #[test] fn should_return_sha() { - let sha = commit::Sha::new("sha".to_string()); - let message = commit::Message::new("message".to_string()); - let commit = Commit::new(sha.clone(), message); + let sha = given::a_commit_sha(); + let commit = given::a_commit_with_sha(&sha); assert_eq!(commit.sha(), &sha); } #[test] fn should_return_message() { - let sha = commit::Sha::new("sha".to_string()); - let message = commit::Message::new("message".to_string()); - let commit = Commit::new(sha, message.clone()); + let message = given::a_commit_message(); + let commit = given::a_commit_with_message(&message); assert_eq!(commit.message(), &message); } + #[test] + fn should_convert_from_push() { + let sha = given::a_commit_sha(); + let message = given::a_commit_message(); + let push = given::a_webhook_push(&sha, &message); + let commit = git::Commit::from(push); + + let expected = git::Commit::new( + git::commit::Sha::new(sha.to_string()), + git::commit::Message::new(message.to_string()), + ); + + assert_eq!(commit, expected); + } } mod generation { use crate::Generation; @@ -160,3 +174,27 @@ mod repo_details { ); } } +// mod branch { +// use super::*; +// use assert2::let_assert; +// #[test] +// fn reset_should_fetch_then_push() { +// // let repository = given::a_mock_open_repository(); +// let fs = given::a_filesystem(); +// let repo_detauls = given::repo_details(&fs); +// let repository = given::a_mock_open_repository(); +// let_assert!( +// Ok(result) = git::branch::reset( +// repository, +// &repo_details, +// &repo_details.branch, +// git_ref, +// force +// ) +// ); +// } +// } +mod given { + // pub use git_next_config_test::given::*; + pub use git_next_git_test::given::*; +} diff --git a/crates/git/test/Cargo.toml b/crates/git/test/Cargo.toml index 4f294d1..2810a24 100644 --- a/crates/git/test/Cargo.toml +++ b/crates/git/test/Cargo.toml @@ -5,6 +5,7 @@ edition = { workspace = true } [dependencies] git-next-git = { workspace = true } +git-next-config = { workspace = true } git-next-config-test = { workspace = true } rand = { workspace = true } diff --git a/crates/git/test/src/given.rs b/crates/git/test/src/given.rs index 0b538c6..96dcbad 100644 --- a/crates/git/test/src/given.rs +++ b/crates/git/test/src/given.rs @@ -1,9 +1,56 @@ -use git_next_config_test::given::a_name; -use git_next_git as git; +use git_next_config as config; +use git_next_config_test::given::*; +use git_next_git::{self as git, RepoDetails}; pub fn a_commit() -> git::Commit { - git::Commit::new( - git::commit::Sha::new(a_name()), - git::commit::Message::new(a_name()), + git::Commit::new(a_commit_sha(), a_commit_message()) +} + +pub fn a_commit_with_message(message: &git::commit::Message) -> git::Commit { + git::Commit::new(a_commit_sha(), message.to_owned()) +} + +pub fn a_commit_with_sha(sha: &git::commit::Sha) -> git::Commit { + git::Commit::new(sha.to_owned(), a_commit_message()) +} + +pub fn a_commit_with(sha: &git::commit::Sha, message: &git::commit::Message) -> git::Commit { + git::Commit::new(sha.to_owned(), message.to_owned()) +} + +pub fn a_commit_message() -> git::commit::Message { + git::commit::Message::new(a_name()) +} + +pub fn a_commit_sha() -> git::commit::Sha { + git::commit::Sha::new(a_name()) +} + +pub fn a_webhook_push( + sha: &git::commit::Sha, + message: &git::commit::Message, +) -> config::webhook::Push { + let branch = a_branch_name(); + config::webhook::Push::new(branch, sha.to_string(), message.to_string()) +} + +pub fn a_filesystem() -> kxio::fs::FileSystem { + kxio::fs::temp().unwrap_or_else(|e| panic!("{}", e)) +} + +pub fn repo_details(fs: &kxio::fs::FileSystem) -> git::RepoDetails { + let generation = git::Generation::new(); + let repo_alias = a_repo_alias(); + let server_repo_config = a_server_repo_config(); + let forge_alias = a_forge_alias(); + let forge_config = a_forge_config(); + let gitdir = a_git_dir(fs); + RepoDetails::new( + generation, + &repo_alias, + &server_repo_config, + &forge_alias, + &forge_config, + gitdir, ) }