Compare commits

..

2 commits

Author SHA1 Message Date
b1e5bf3788 WIP: add more tests to repo-actor 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
2024-06-19 06:59:18 +01:00
5e9f9eb80f refactor: start to use newtype macro
All checks were successful
Rust / build (push) Successful in 1m21s
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
2024-06-19 06:45:45 +01:00
10 changed files with 61 additions and 120 deletions

View file

@ -1,13 +1,2 @@
use derive_more::Display; // The name of a Branch
crate::newtype!(BranchName is a String, derive_more::Display, Default);
/// The name of a Branch
#[derive(Clone, Default, Debug, Hash, PartialEq, Eq, Display, PartialOrd, Ord)]
pub struct BranchName(String);
impl BranchName {
pub fn new(str: impl Into<String>) -> Self {
Self(str.into())
}
pub fn into_string(self) -> String {
self.0
}
}

View file

@ -0,0 +1,7 @@
// The name of a Forge to connect to
crate::newtype!(ForgeAlias is a String, derive_more::Display, Default);
impl From<&ForgeAlias> for std::path::PathBuf {
fn from(value: &ForgeAlias) -> Self {
Self::from(&value.0)
}
}

View file

@ -1,12 +0,0 @@
use std::path::PathBuf;
use derive_more::{AsRef, Constructor, Display};
/// The name of a Forge to connect to
#[derive(Clone, Default, Debug, Hash, PartialEq, Eq, Constructor, Display, AsRef)]
pub struct ForgeAlias(String);
impl From<&ForgeAlias> for PathBuf {
fn from(value: &ForgeAlias) -> Self {
Self::from(&value.0)
}
}

View file

@ -1,22 +1,8 @@
//
use std::path::PathBuf; use std::path::PathBuf;
#[derive( crate::newtype!(GitDir is a PathBuf, Default);
Clone,
Default,
Debug,
Hash,
PartialEq,
Eq,
serde::Deserialize,
derive_more::Deref,
derive_more::From,
)]
pub struct GitDir(PathBuf);
impl GitDir { impl GitDir {
pub fn new(path: &std::path::Path) -> Self {
Self(path.to_path_buf())
}
pub const fn pathbuf(&self) -> &PathBuf { pub const fn pathbuf(&self) -> &PathBuf {
&self.0 &self.0
} }
@ -36,8 +22,3 @@ impl From<&GitDir> for PathBuf {
value.to_path_buf() value.to_path_buf()
} }
} }
impl From<GitDir> for PathBuf {
fn from(value: GitDir) -> Self {
value.0
}
}

View file

@ -1,8 +1,2 @@
/// The hostname of a forge // The hostname of a forge
#[derive(Clone, Default, Debug, PartialEq, Eq, derive_more::Display)] crate::newtype!(Hostname is a String, derive_more::Display, Default);
pub struct Hostname(String);
impl Hostname {
pub fn new(str: impl Into<String>) -> Self {
Self(str.into())
}
}

View file

@ -1,10 +1,11 @@
// //
mod api_token; mod api_token;
mod branch_name; mod branch_name;
pub mod common; pub mod common;
mod forge_alias;
mod forge_config; mod forge_config;
mod forge_details; mod forge_details;
mod forge_name;
mod forge_type; mod forge_type;
pub mod git_dir; pub mod git_dir;
mod host_name; mod host_name;
@ -25,9 +26,9 @@ mod tests;
pub use api_token::ApiToken; pub use api_token::ApiToken;
pub use branch_name::BranchName; pub use branch_name::BranchName;
pub use forge_alias::ForgeAlias;
pub use forge_config::ForgeConfig; pub use forge_config::ForgeConfig;
pub use forge_details::ForgeDetails; pub use forge_details::ForgeDetails;
pub use forge_name::ForgeAlias;
pub use forge_type::ForgeType; pub use forge_type::ForgeType;
pub use git_dir::GitDir; pub use git_dir::GitDir;
pub use host_name::Hostname; pub use host_name::Hostname;

View file

@ -1,18 +1,22 @@
// //
use super::*;
use assert2::let_assert;
use secrecy::ExposeSecret;
use std::collections::BTreeMap;
use std::path::PathBuf;
use crate::server::Http;
use crate::server::ServerConfig;
use crate::server::ServerStorage;
use crate::server::Webhook;
use crate::webhook::push::Branch;
type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>; type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
type TestResult = Result<()>; type TestResult = Result<()>;
mod server_repo_config { mod server_repo_config {
use std::path::PathBuf; use super::*;
use assert2::let_assert;
use crate::{
tests::given, BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource, RepoPath,
};
use super::super::server_repo_config::*;
#[test] #[test]
fn should_not_return_repo_config_when_no_branches() { fn should_not_return_repo_config_when_no_branches() {
@ -86,14 +90,11 @@ mod server_repo_config {
assert_eq!( assert_eq!(
src.gitdir(), src.gitdir(),
Some(GitDir::new(&PathBuf::default().join(gitdir))) Some(GitDir::new(PathBuf::default().join(gitdir)))
); );
} }
} }
mod repo_config { mod repo_config {
use crate::{RepoBranches, RepoConfigSource};
use super::super::repo_config::*;
use super::*; use super::*;
#[test] #[test]
@ -144,13 +145,7 @@ mod repo_config {
} }
} }
mod forge_config { mod forge_config {
use std::collections::BTreeMap; use super::*;
use secrecy::ExposeSecret;
use crate::{
tests::given, ForgeConfig, ForgeType, Hostname, RepoAlias, ServerRepoConfig, User,
};
#[test] #[test]
fn should_return_repos() { fn should_return_repos() {
@ -246,14 +241,9 @@ mod forge_config {
} }
} }
mod forge_details { mod forge_details {
use std::collections::BTreeMap; use super::*;
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
use crate::{
tests::given, ApiToken, ForgeAlias, ForgeConfig, ForgeDetails, ForgeType, Hostname, User,
};
#[test] #[test]
fn should_return_forge_alias() { fn should_return_forge_alias() {
let forge_type = ForgeType::MockForge; let forge_type = ForgeType::MockForge;
@ -364,10 +354,9 @@ mod forge_details {
} }
} }
mod forge_name { mod forge_name {
use super::*;
use std::path::PathBuf; use std::path::PathBuf;
use crate::{tests::given, ForgeAlias};
#[test] #[test]
fn should_convert_to_pathbuf() { fn should_convert_to_pathbuf() {
let name = given::a_name(); let name = given::a_name();
@ -379,7 +368,7 @@ mod forge_name {
} }
} }
mod forge_type { mod forge_type {
use crate::ForgeType; use super::*;
#[test] #[test]
fn should_display_as_string() { fn should_display_as_string() {
@ -387,23 +376,22 @@ mod forge_type {
} }
} }
mod gitdir { mod gitdir {
use super::*;
use std::path::PathBuf; use std::path::PathBuf;
use crate::{tests::given, GitDir};
#[test] #[test]
fn should_return_pathbuf() { fn should_return_pathbuf() {
let pathbuf = PathBuf::default().join(given::a_name()); let pathbuf = PathBuf::default().join(given::a_name());
let gitdir = GitDir::new(&pathbuf); let gitdir = GitDir::new(&pathbuf);
let result = gitdir.pathbuf(); let result = gitdir.unwrap();
assert_eq!(result, &pathbuf); assert_eq!(result, pathbuf);
} }
#[test] #[test]
fn should_display() { fn should_display() {
let pathbuf = PathBuf::default().join("foo"); let pathbuf = PathBuf::default().join("foo");
let gitdir = GitDir::new(&pathbuf); let gitdir = GitDir::new(pathbuf);
let result = gitdir.to_string(); let result = gitdir.to_string();
@ -415,7 +403,7 @@ mod gitdir {
let pathbuf = PathBuf::default().join(path.clone()); let pathbuf = PathBuf::default().join(path.clone());
let gitdir: GitDir = path.as_str().into(); let gitdir: GitDir = path.as_str().into();
assert_eq!(gitdir, GitDir::new(&pathbuf)); assert_eq!(gitdir, GitDir::new(pathbuf));
} }
#[test] #[test]
fn should_convert_to_pathbuf_from_ref() { fn should_convert_to_pathbuf_from_ref() {
@ -439,7 +427,7 @@ mod gitdir {
} }
} }
mod repo_branches { mod repo_branches {
use crate::{tests::given, BranchName, RepoBranches}; use super::*;
#[test] #[test]
fn should_return_main() { fn should_return_main() {
@ -470,15 +458,10 @@ mod repo_branches {
} }
} }
mod server { mod server {
use super::*;
mod load { mod load {
use assert2::let_assert; use super::*;
use pretty_assertions::assert_eq;
use crate::{
server::ServerConfig,
tests::{given, TestResult},
};
#[test] #[test]
fn load_should_parse_server_config() -> TestResult { fn load_should_parse_server_config() -> TestResult {
@ -520,6 +503,8 @@ mod server {
let branch = server_repo_config.branch(); let branch = server_repo_config.branch();
let gitdir = server_repo_config let gitdir = server_repo_config
.gitdir() .gitdir()
.map(|gitdir| gitdir.unwrap())
.map(|pathbuf| pathbuf.display().to_string())
.map(|gitdir| format!(r#", gitdir = "{gitdir}" "#)) .map(|gitdir| format!(r#", gitdir = "{gitdir}" "#))
.unwrap_or_default(); .unwrap_or_default();
let repo_server_config = server_repo_config let repo_server_config = server_repo_config
@ -567,8 +552,7 @@ token = "{forge_token}"
} }
} }
mod registered_webhook { mod registered_webhook {
use super::*;
use crate::{tests::given, RegisteredWebhook, WebhookAuth};
#[test] #[test]
fn should_return_id() { fn should_return_id() {
@ -588,11 +572,11 @@ mod registered_webhook {
} }
} }
mod webhook { mod webhook {
use super::*;
mod message { mod message {
use super::*;
use std::collections::HashMap; use std::collections::HashMap;
use crate::{tests::given, WebhookMessage};
#[test] #[test]
fn should_return_forge_alias() { fn should_return_forge_alias() {
let forge_alias = given::a_forge_alias(); let forge_alias = given::a_forge_alias();
@ -643,7 +627,8 @@ mod webhook {
} }
} }
mod push { mod push {
use crate::{tests::given, webhook::push::Branch, BranchName};
use super::*;
#[test] #[test]
fn should_return_main_branch() { fn should_return_main_branch() {
@ -715,18 +700,14 @@ mod push {
} }
} }
mod given { mod given {
use super::*;
use rand::Rng as _;
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use rand::Rng as _;
use crate::{
server::{Http, ServerConfig, ServerStorage, Webhook},
ForgeAlias, ForgeConfig, ForgeType, RepoAlias, RepoBranches, ServerRepoConfig, WebhookId,
};
pub fn a_name() -> String { pub fn a_name() -> String {
use rand::Rng; use rand::Rng;
use std::iter; use std::iter;

View file

@ -733,7 +733,7 @@ mod forgejo {
pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> config::GitDir { pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> config::GitDir {
let dir_name = a_name(); let dir_name = a_name();
let dir = fs.base().join(dir_name); let dir = fs.base().join(dir_name);
config::GitDir::new(&dir) config::GitDir::new(dir)
} }
pub fn a_forge_config() -> config::ForgeConfig { pub fn a_forge_config() -> config::ForgeConfig {
@ -747,9 +747,9 @@ mod forgejo {
} }
pub fn a_server_repo_config() -> config::ServerRepoConfig { pub fn a_server_repo_config() -> config::ServerRepoConfig {
let main = a_branch_name().into_string(); let main = a_branch_name().unwrap();
let next = a_branch_name().into_string(); let next = a_branch_name().unwrap();
let dev = a_branch_name().into_string(); let dev = a_branch_name().unwrap();
config::ServerRepoConfig::new( config::ServerRepoConfig::new(
format!("{}/{}", a_name(), a_name()), format!("{}/{}", a_name(), a_name()),
main.clone(), main.clone(),

View file

@ -92,7 +92,7 @@ mod server_repo_config {
assert_eq!( assert_eq!(
src.gitdir(), src.gitdir(),
Some(config::GitDir::new(&PathBuf::default().join(gitdir))) Some(config::GitDir::new(PathBuf::default().join(gitdir)))
); );
} }
} }

View file

@ -266,7 +266,7 @@ pub mod given {
pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir { pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir {
let dir_name = a_name(); let dir_name = a_name();
let dir = fs.base().join(dir_name); let dir = fs.base().join(dir_name);
GitDir::new(&dir) GitDir::new(dir)
} }
pub fn a_forge_config() -> ForgeConfig { pub fn a_forge_config() -> ForgeConfig {
@ -280,9 +280,9 @@ pub mod given {
} }
pub fn a_server_repo_config() -> ServerRepoConfig { pub fn a_server_repo_config() -> ServerRepoConfig {
let main = a_branch_name().into_string(); let main = a_branch_name().unwrap();
let next = a_branch_name().into_string(); let next = a_branch_name().unwrap();
let dev = a_branch_name().into_string(); let dev = a_branch_name().unwrap();
ServerRepoConfig::new( ServerRepoConfig::new(
format!("{}/{}", a_name(), a_name()), format!("{}/{}", a_name(), a_name()),
main.clone(), main.clone(),