From a9c263cd9c9d93d7f9c8decc89ec3f6751d5b5df Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 6 Jun 2024 18:22:13 +0100 Subject: [PATCH] WIP: refactor: create config-test crate --- Cargo.toml | 2 + crates/config/test/Cargo.toml | 16 +++++++ crates/config/test/src/given.rs | 70 +++++++++++++++++++++++++++++++ crates/config/test/src/lib.rs | 1 + crates/forge-forgejo/Cargo.toml | 1 + crates/forge-forgejo/src/tests.rs | 59 ++++---------------------- 6 files changed, 97 insertions(+), 52 deletions(-) create mode 100644 crates/config/test/Cargo.toml create mode 100644 crates/config/test/src/given.rs create mode 100644 crates/config/test/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 727a9e4..619cf2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "crates/cli", "crates/server", "crates/config", + "crates/config/test", "crates/git", "crates/forge", "crates/forge-forgejo", @@ -24,6 +25,7 @@ expect_used = "warn" [workspace.dependencies] git-next-server = { path = "crates/server" } git-next-config = { path = "crates/config" } +git-next-config-test = { path = "crates/config/test" } git-next-git = { path = "crates/git" } git-next-forge = { path = "crates/forge" } git-next-forge-forgejo = { path = "crates/forge-forgejo" } diff --git a/crates/config/test/Cargo.toml b/crates/config/test/Cargo.toml new file mode 100644 index 0000000..dd06489 --- /dev/null +++ b/crates/config/test/Cargo.toml @@ -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" diff --git a/crates/config/test/src/given.rs b/crates/config/test/src/given.rs new file mode 100644 index 0000000..aeec1ae --- /dev/null +++ b/crates/config/test/src/given.rs @@ -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() +} diff --git a/crates/config/test/src/lib.rs b/crates/config/test/src/lib.rs new file mode 100644 index 0000000..a3b940e --- /dev/null +++ b/crates/config/test/src/lib.rs @@ -0,0 +1 @@ +pub mod given; diff --git a/crates/forge-forgejo/Cargo.toml b/crates/forge-forgejo/Cargo.toml index 271c0a1..09c7caa 100644 --- a/crates/forge-forgejo/Cargo.toml +++ b/crates/forge-forgejo/Cargo.toml @@ -39,6 +39,7 @@ tokio = { workspace = true } [dev-dependencies] # Testing +git-next-config-test = { workspace = true } assert2 = { workspace = true } rand = { workspace = true } diff --git a/crates/forge-forgejo/src/tests.rs b/crates/forge-forgejo/src/tests.rs index 881e0bf..dd751df 100644 --- a/crates/forge-forgejo/src/tests.rs +++ b/crates/forge-forgejo/src/tests.rs @@ -10,8 +10,8 @@ mod forgejo { use crate::ForgeJo; use config::{ - webhook::message::Body, ForgeAlias, ForgeConfig, ForgeType, GitDir, RepoAlias, - RepoBranches, ServerRepoConfig, WebhookAuth, WebhookMessage, + webhook::message::Body, ForgeConfig, ForgeType, GitDir, ServerRepoConfig, WebhookAuth, + WebhookMessage, }; use git::ForgeLike as _; @@ -568,9 +568,9 @@ mod forgejo { } } mod given { + pub use git_next_config_test::given::*; use std::collections::HashMap; - use git_next_config::{server::Webhook, WebhookId}; use kxio::network::{MockNetwork, StatusCode}; use rand::RngCore; use serde_json::json; @@ -604,10 +604,6 @@ mod forgejo { ) } - pub fn a_webhook_auth() -> WebhookAuth { - WebhookAuth::generate() - } - pub enum Header { Valid(WebhookAuth), Missing, @@ -615,6 +611,7 @@ mod forgejo { NonUlid, WrongUlid, } + pub fn a_webhook_message(header: Header) -> WebhookMessage { WebhookMessage::new( given::a_forge_alias(), @@ -623,6 +620,7 @@ mod forgejo { given::a_webhook_message_body(), ) } + pub fn webhook_headers(header: Header) -> HashMap { let mut headers = HashMap::new(); match header { @@ -645,9 +643,7 @@ mod forgejo { } headers } - pub fn a_webhook_message_body() -> Body { - Body::new(a_name()) - } + pub fn a_commit() -> git::Commit { git::Commit::new( 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( repo_details: &git::RepoDetails, net: impl Into, ) -> ForgeJo { ForgeJo::new(repo_details.clone(), net.into()) } + pub fn repo_details() -> git::RepoDetails { git::RepoDetails::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 { rand::thread_rng().next_u32().into() }