WIP: test: add more tests to git crate
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-06-06 18:31:48 +01:00
parent 1ca13bb0ae
commit 3fd75c43f2
7 changed files with 107 additions and 29 deletions

View file

@ -1,7 +1,7 @@
// //
use config::{ use config::{
server::Webhook, webhook::message::Body, ForgeAlias, RepoAlias, RepoBranches, WebhookAuth, server::Webhook, webhook::message::Body, BranchName, ForgeAlias, RepoAlias, RepoBranches,
WebhookId, WebhookAuth, WebhookId,
}; };
use git_next_config as config; use git_next_config as config;
@ -68,3 +68,7 @@ pub fn a_webhook_id() -> WebhookId {
pub fn a_github_webhook_id() -> i64 { pub fn a_github_webhook_id() -> i64 {
rand::thread_rng().next_u32().into() rand::thread_rng().next_u32().into()
} }
pub fn a_branch_name() -> BranchName {
BranchName::new(a_name())
}

View file

@ -53,6 +53,8 @@ actix = { workspace = true }
# #
[dev-dependencies] [dev-dependencies]
# Testing # Testing
git-next-config-test = { workspace = true }
git-next-git-test = { workspace = true }
assert2 = { workspace = true } assert2 = { workspace = true }
[lints.clippy] [lints.clippy]

View file

@ -52,6 +52,7 @@ pub mod log {
} }
impl From<String> for Error { impl From<String> for Error {
#[cfg(not(tarpaulin_include))]
fn from(e: String) -> Self { fn from(e: String) -> Self {
Self::Gix(e) Self::Gix(e)
} }

View file

@ -52,26 +52,30 @@ pub enum Error {
TryId, TryId,
} }
impl From<gix::reference::find::existing::Error> for Error { mod gix_errors {
fn from(value: gix::reference::find::existing::Error) -> Self { #![cfg(not(tarpaulin_include))] // third-party library errors
Self::FindReference(value.to_string()) use super::Error;
impl From<gix::reference::find::existing::Error> for Error {
fn from(value: gix::reference::find::existing::Error) -> Self {
Self::FindReference(value.to_string())
}
} }
}
impl From<gix::object::commit::Error> for Error { impl From<gix::object::commit::Error> for Error {
fn from(value: gix::object::commit::Error) -> Self { fn from(value: gix::object::commit::Error) -> Self {
Self::NoTreeInCommit(value.to_string()) Self::NoTreeInCommit(value.to_string())
}
} }
}
impl From<gix::object::find::existing::Error> for Error { impl From<gix::object::find::existing::Error> for Error {
fn from(value: gix::object::find::existing::Error) -> Self { fn from(value: gix::object::find::existing::Error) -> Self {
Self::FindObject(value.to_string()) Self::FindObject(value.to_string())
}
} }
}
impl From<std::string::FromUtf8Error> for Error { impl From<std::string::FromUtf8Error> for Error {
fn from(value: std::string::FromUtf8Error) -> Self { fn from(value: std::string::FromUtf8Error) -> Self {
Self::NonUtf8Blob(value.to_string()) Self::NonUtf8Blob(value.to_string())
}
} }
} }

View file

@ -1,22 +1,36 @@
use crate as git;
mod commit { mod commit {
use crate::{commit, Commit}; use super::*;
#[test] #[test]
fn should_return_sha() { fn should_return_sha() {
let sha = commit::Sha::new("sha".to_string()); let sha = given::a_commit_sha();
let message = commit::Message::new("message".to_string()); let commit = given::a_commit_with_sha(&sha);
let commit = Commit::new(sha.clone(), message);
assert_eq!(commit.sha(), &sha); assert_eq!(commit.sha(), &sha);
} }
#[test] #[test]
fn should_return_message() { fn should_return_message() {
let sha = commit::Sha::new("sha".to_string()); let message = given::a_commit_message();
let message = commit::Message::new("message".to_string()); let commit = given::a_commit_with_message(&message);
let commit = Commit::new(sha, message.clone());
assert_eq!(commit.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 { mod generation {
use crate::Generation; use crate::Generation;
@ -160,3 +174,19 @@ 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 repo_detauls = given::repo_details();
let_assert!(
Ok(result) = git::branch::reset(repository, repo_details, branch_name, git_ref, force)
);
}
}
mod given {
pub use git_next_config_test::given::*;
pub use git_next_git_test::given::*;
}

View file

@ -5,6 +5,7 @@ edition = { workspace = true }
[dependencies] [dependencies]
git-next-git = { workspace = true } git-next-git = { workspace = true }
git-next-config = { workspace = true }
git-next-config-test = { workspace = true } git-next-config-test = { workspace = true }
rand = { workspace = true } rand = { workspace = true }

View file

@ -1,9 +1,45 @@
use git_next_config_test::given::a_name; use git_next_config as config;
use git_next_config_test::given::*;
use git_next_git as git; use git_next_git as git;
pub fn a_commit() -> git::Commit { pub fn a_commit() -> git::Commit {
git::Commit::new( git::Commit::new(a_commit_sha(), a_commit_message())
git::commit::Sha::new(a_name()), }
git::commit::Message::new(a_name()),
) 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 repo_details() -> git::RepoDetails {
// generation: Generation,
// repo_alias: &RepoAlias,
// server_repo_config: &ServerRepoConfig,
// forge_alias: &ForgeAlias,
// forge_config: &ForgeConfig,
// gitdir: GitDir,
// git::RepoDetails::new()
} }