From f4b8401bb113c3d645ba81a8765577829b2f2cb6 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 19 May 2024 09:44:59 +0100 Subject: [PATCH] test(git): add more tests --- crates/git/src/push.rs | 2 +- crates/git/src/repository.rs | 1 + crates/git/src/tests.rs | 142 +++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) diff --git a/crates/git/src/push.rs b/crates/git/src/push.rs index 5c0e6dd..e735455 100644 --- a/crates/git/src/push.rs +++ b/crates/git/src/push.rs @@ -8,7 +8,7 @@ pub enum Force { impl std::fmt::Display for Force { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::No => write!(f, "fast-foward"), + Self::No => write!(f, "fast-forward"), Self::From(from) => write!(f, "force-if-from:{}", from), } } diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rs index fe351ba..cd94931 100644 --- a/crates/git/src/repository.rs +++ b/crates/git/src/repository.rs @@ -1,4 +1,5 @@ // +#![cfg(not(tarpaulin_include))] use std::{ ops::Deref as _, sync::{atomic::AtomicBool, Arc, Mutex}, diff --git a/crates/git/src/tests.rs b/crates/git/src/tests.rs index 9f2ebe9..c75fb47 100644 --- a/crates/git/src/tests.rs +++ b/crates/git/src/tests.rs @@ -18,3 +18,145 @@ mod commit { assert_eq!(commit.message(), &message); } } +mod generation { + use crate::Generation; + + #[test] + fn should_increment() { + let mut g = Generation::new(); + assert_eq!(g.to_string(), "0"); + + g.inc(); + + assert_eq!(g.to_string(), "1"); + } +} +mod gitref { + use crate::{commit, Commit, GitRef}; + #[test] + fn should_convert_from_commit() { + let commit = Commit::new( + commit::Sha::new("sha".to_string()), + commit::Message::new("message".to_string()), + ); + let gitref = GitRef::from(commit); + + assert_eq!(gitref.to_string(), "sha"); + } +} +mod gitremote { + use git_next_config::{Hostname, RepoPath}; + + use crate::GitRemote; + + #[test] + fn should_return_hostname() { + let host = Hostname::new("localhost".to_string()); + let repo_path = RepoPath::new("kemitix/git-next".to_string()); + let gr = GitRemote::new(host.clone(), repo_path); + + assert_eq!(gr.host(), &host); + } + #[test] + fn should_return_repo_path() { + let host = Hostname::new("localhost".to_string()); + let repo_path = RepoPath::new("kemitix/git-next".to_string()); + let gr = GitRemote::new(host, repo_path.clone()); + + assert_eq!(gr.repo_path(), &repo_path); + } +} +mod push { + use crate::{commit, Commit, GitRef}; + + use crate::push::Force; + + #[test] + fn force_no_should_display() { + assert_eq!(Force::No.to_string(), "fast-forward") + } + #[test] + fn force_from_should_display() { + let commit = Commit::new( + commit::Sha::new("sha".to_string()), + commit::Message::new("message".to_string()), + ); + assert_eq!( + Force::From(GitRef::from(commit)).to_string(), + "force-if-from:sha" + ) + } +} +mod repo_details { + + use std::{collections::BTreeMap, path::PathBuf}; + + use git_next_config::{ + ForgeConfig, ForgeName, ForgeType, GitDir, Hostname, RepoAlias, RepoPath, ServerRepoConfig, + }; + use secrecy::ExposeSecret; + + use crate::{Generation, GitRemote, RepoDetails}; + + #[test] + fn should_return_origin() { + let rd = RepoDetails::new( + Generation::new(), + &RepoAlias::new("foo"), + &ServerRepoConfig::new( + "repo".to_string(), + "branch".to_string(), + None, + None, + None, + None, + ), + &ForgeName::new("default".to_string()), + &ForgeConfig::new( + ForgeType::MockForge, + "host".to_string(), + "user".to_string(), + "token".to_string(), + BTreeMap::new(), + ), + GitDir::from(PathBuf::default().join("foo")), + ); + + assert_eq!( + rd.origin().expose_secret(), + "https://user:token@host/repo.git" + ); + } + #[test] + fn should_return_git_remote() { + let rd = RepoDetails::new( + Generation::new(), + &RepoAlias::new("foo"), + &ServerRepoConfig::new( + "user/repo".to_string(), + "branch".to_string(), + None, + None, + None, + None, + ), + &ForgeName::new("default".to_string()), + &ForgeConfig::new( + ForgeType::MockForge, + "host".to_string(), + "user".to_string(), + "token".to_string(), + BTreeMap::new(), + ), + GitDir::from(PathBuf::default().join("foo")), + ); + + assert_eq!( + rd.git_remote(), + GitRemote::new( + Hostname::new("host".to_string()), + RepoPath::new("user/repo".to_string()) + ) + ); + } +}