WIP: refactor: create config-test crate

This commit is contained in:
Paul Campbell 2024-06-06 18:22:13 +01:00
parent 271f4ec1dc
commit a9c263cd9c
6 changed files with 97 additions and 52 deletions

View file

@ -4,6 +4,7 @@ members = [
"crates/cli", "crates/cli",
"crates/server", "crates/server",
"crates/config", "crates/config",
"crates/config/test",
"crates/git", "crates/git",
"crates/forge", "crates/forge",
"crates/forge-forgejo", "crates/forge-forgejo",
@ -24,6 +25,7 @@ expect_used = "warn"
[workspace.dependencies] [workspace.dependencies]
git-next-server = { path = "crates/server" } git-next-server = { path = "crates/server" }
git-next-config = { path = "crates/config" } git-next-config = { path = "crates/config" }
git-next-config-test = { path = "crates/config/test" }
git-next-git = { path = "crates/git" } git-next-git = { path = "crates/git" }
git-next-forge = { path = "crates/forge" } git-next-forge = { path = "crates/forge" }
git-next-forge-forgejo = { path = "crates/forge-forgejo" } git-next-forge-forgejo = { path = "crates/forge-forgejo" }

View file

@ -0,0 +1,16 @@
[package]
name = "git-next-config-test"
version = { workspace = true }
edition = { workspace = true }
[dependencies]
git-next-config = { workspace = true }
rand = { workspace = true }
kxio = { workspace = true }
[lints.clippy]
nursery = { level = "warn", priority = -1 }
# pedantic = "warn"
unwrap_used = "warn"
expect_used = "warn"

View file

@ -0,0 +1,70 @@
//
use config::{
server::Webhook, webhook::message::Body, ForgeAlias, RepoAlias, RepoBranches, WebhookAuth,
WebhookId,
};
use git_next_config as config;
use rand::RngCore;
pub fn a_webhook_auth() -> WebhookAuth {
WebhookAuth::generate()
}
pub enum Header {
Valid(WebhookAuth, Body),
Missing,
Invalid,
}
pub fn a_webhook_message_body() -> Body {
Body::new(a_name())
}
pub fn repo_branches() -> RepoBranches {
RepoBranches::new(a_name(), a_name(), a_name())
}
pub fn a_forge_alias() -> ForgeAlias {
ForgeAlias::new(a_name())
}
pub fn a_repo_alias() -> RepoAlias {
RepoAlias::new(a_name())
}
pub fn a_network() -> kxio::network::MockNetwork {
kxio::network::MockNetwork::new()
}
pub fn a_webhook_url(
forge_alias: &ForgeAlias,
repo_alias: &RepoAlias,
) -> git_next_config::server::WebhookUrl {
Webhook::new(a_name()).url(forge_alias, repo_alias)
}
pub fn any_webhook_url() -> git_next_config::server::WebhookUrl {
a_webhook_url(&a_forge_alias(), &a_repo_alias())
}
pub fn a_name() -> String {
use rand::Rng;
use std::iter;
fn generate(len: usize) -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let mut rng = rand::thread_rng();
let one_char = || CHARSET[rng.gen_range(0..CHARSET.len())] as char;
iter::repeat_with(one_char).take(len).collect()
}
generate(5)
}
pub fn a_webhook_id() -> WebhookId {
WebhookId::new(a_name())
}
pub fn a_github_webhook_id() -> i64 {
rand::thread_rng().next_u32().into()
}

View file

@ -0,0 +1 @@
pub mod given;

View file

@ -39,6 +39,7 @@ tokio = { workspace = true }
[dev-dependencies] [dev-dependencies]
# Testing # Testing
git-next-config-test = { workspace = true }
assert2 = { workspace = true } assert2 = { workspace = true }
rand = { workspace = true } rand = { workspace = true }

View file

@ -10,8 +10,8 @@ mod forgejo {
use crate::ForgeJo; use crate::ForgeJo;
use config::{ use config::{
webhook::message::Body, ForgeAlias, ForgeConfig, ForgeType, GitDir, RepoAlias, webhook::message::Body, ForgeConfig, ForgeType, GitDir, ServerRepoConfig, WebhookAuth,
RepoBranches, ServerRepoConfig, WebhookAuth, WebhookMessage, WebhookMessage,
}; };
use git::ForgeLike as _; use git::ForgeLike as _;
@ -568,9 +568,9 @@ mod forgejo {
} }
} }
mod given { mod given {
pub use git_next_config_test::given::*;
use std::collections::HashMap; use std::collections::HashMap;
use git_next_config::{server::Webhook, WebhookId};
use kxio::network::{MockNetwork, StatusCode}; use kxio::network::{MockNetwork, StatusCode};
use rand::RngCore; use rand::RngCore;
use serde_json::json; use serde_json::json;
@ -604,10 +604,6 @@ mod forgejo {
) )
} }
pub fn a_webhook_auth() -> WebhookAuth {
WebhookAuth::generate()
}
pub enum Header { pub enum Header {
Valid(WebhookAuth), Valid(WebhookAuth),
Missing, Missing,
@ -615,6 +611,7 @@ mod forgejo {
NonUlid, NonUlid,
WrongUlid, WrongUlid,
} }
pub fn a_webhook_message(header: Header) -> WebhookMessage { pub fn a_webhook_message(header: Header) -> WebhookMessage {
WebhookMessage::new( WebhookMessage::new(
given::a_forge_alias(), given::a_forge_alias(),
@ -623,6 +620,7 @@ mod forgejo {
given::a_webhook_message_body(), given::a_webhook_message_body(),
) )
} }
pub fn webhook_headers(header: Header) -> HashMap<String, String> { pub fn webhook_headers(header: Header) -> HashMap<String, String> {
let mut headers = HashMap::new(); let mut headers = HashMap::new();
match header { match header {
@ -645,9 +643,7 @@ mod forgejo {
} }
headers headers
} }
pub fn a_webhook_message_body() -> Body {
Body::new(a_name())
}
pub fn a_commit() -> git::Commit { pub fn a_commit() -> git::Commit {
git::Commit::new( git::Commit::new(
git::commit::Sha::new(a_name()), git::commit::Sha::new(a_name()),
@ -655,16 +651,13 @@ mod forgejo {
) )
} }
pub fn repo_branches() -> RepoBranches {
RepoBranches::new(a_name(), a_name(), a_name())
}
pub fn a_forgejo_forge( pub fn a_forgejo_forge(
repo_details: &git::RepoDetails, repo_details: &git::RepoDetails,
net: impl Into<kxio::network::Network>, net: impl Into<kxio::network::Network>,
) -> ForgeJo { ) -> ForgeJo {
ForgeJo::new(repo_details.clone(), net.into()) ForgeJo::new(repo_details.clone(), net.into())
} }
pub fn repo_details() -> git::RepoDetails { pub fn repo_details() -> git::RepoDetails {
git::RepoDetails::new( git::RepoDetails::new(
git::Generation::new(), git::Generation::new(),
@ -689,44 +682,6 @@ mod forgejo {
) )
} }
pub fn a_forge_alias() -> ForgeAlias {
ForgeAlias::new(a_name())
}
pub fn a_repo_alias() -> RepoAlias {
RepoAlias::new(a_name())
}
pub fn a_network() -> kxio::network::MockNetwork {
kxio::network::MockNetwork::new()
}
pub fn a_webhook_url(
forge_alias: &ForgeAlias,
repo_alias: &RepoAlias,
) -> git_next_config::server::WebhookUrl {
Webhook::new(a_name()).url(forge_alias, repo_alias)
}
pub fn any_webhook_url() -> git_next_config::server::WebhookUrl {
given::a_webhook_url(&given::a_forge_alias(), &given::a_repo_alias())
}
pub fn a_name() -> String {
use rand::Rng;
use std::iter;
fn generate(len: usize) -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let mut rng = rand::thread_rng();
let one_char = || CHARSET[rng.gen_range(0..CHARSET.len())] as char;
iter::repeat_with(one_char).take(len).collect()
}
generate(5)
}
pub fn a_webhook_id() -> WebhookId {
WebhookId::new(given::a_name())
}
pub fn a_forgejo_webhook_id() -> i64 { pub fn a_forgejo_webhook_id() -> i64 {
rand::thread_rng().next_u32().into() rand::thread_rng().next_u32().into()
} }