From 98839c8a00148b1c6bfb3cd26f0780d1e66d451c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 1 Jun 2024 12:03:30 +0100 Subject: [PATCH] test: add more tests to forge crate --- crates/forge/src/lib.rs | 24 ++++++++--------- crates/forge/src/tests.rs | 50 +++++++++++++++++++++++++++++++++++ crates/forge/src/tests/mod.rs | 31 ---------------------- crates/repo-actor/src/lib.rs | 7 +---- 4 files changed, 63 insertions(+), 49 deletions(-) create mode 100644 crates/forge/src/tests.rs delete mode 100644 crates/forge/src/tests/mod.rs diff --git a/crates/forge/src/lib.rs b/crates/forge/src/lib.rs index 5aacc1ff..8c43c25d 100644 --- a/crates/forge/src/lib.rs +++ b/crates/forge/src/lib.rs @@ -17,18 +17,18 @@ pub enum Forge { Github(git_next_forge_github::Github), } impl Forge { - pub const fn new_mock() -> Self { - Self::Mock(mock_forge::MockForge::new()) - } - - #[cfg(feature = "forgejo")] - pub const fn new_forgejo(repo_details: git::RepoDetails, net: Network) -> Self { - Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net)) - } - - #[cfg(feature = "github")] - pub const fn new_github(repo_details: git::RepoDetails, net: Network) -> Self { - Self::Github(github::Github::new(repo_details, net)) + pub fn new(repo_details: git::RepoDetails, net: Network) -> Self { + match repo_details.forge.forge_type() { + #[cfg(feature = "forgejo")] + git_next_config::ForgeType::ForgeJo => { + Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net)) + } + #[cfg(feature = "github")] + git_next_config::ForgeType::GitHub => { + Self::Github(github::Github::new(repo_details, net)) + } + git_next_config::ForgeType::MockForge => Self::Mock(mock_forge::MockForge::new()), + } } } impl std::ops::Deref for Forge { diff --git a/crates/forge/src/tests.rs b/crates/forge/src/tests.rs new file mode 100644 index 00000000..49e190ce --- /dev/null +++ b/crates/forge/src/tests.rs @@ -0,0 +1,50 @@ +// +use super::*; + +use git_next_config as config; +use git_next_git::{self as git, RepoDetails}; + +#[test] +fn test_mock_name() { + let net = Network::new_mock(); + let repo_details = given_repo_details(config::ForgeType::MockForge); + let forge = Forge::new(repo_details, net); + assert_eq!(forge.name(), "mock"); +} + +#[test] +fn test_forgejo_name() { + let net = Network::new_mock(); + let repo_details = given_repo_details(config::ForgeType::ForgeJo); + let forge = Forge::new(repo_details, net); + assert_eq!(forge.name(), "forgejo"); +} + +#[test] +fn test_github_name() { + let net = Network::new_mock(); + let repo_details = given_repo_details(config::ForgeType::GitHub); + let forge = Forge::new(repo_details, net); + assert_eq!(forge.name(), "github"); +} + +fn given_fs() -> kxio::fs::FileSystem { + kxio::fs::temp().unwrap_or_else(|e| { + eprintln!("{e}"); + panic!("fs") + }) +} + +fn given_repo_details(forge_type: config::ForgeType) -> RepoDetails { + let fs = given_fs(); + git::common::repo_details( + 1, + git::Generation::new(), + config::common::forge_details(1, forge_type), + Some(config::common::repo_config( + 1, + config::RepoConfigSource::Repo, + )), + config::GitDir::new(fs.base()), + ) +} diff --git a/crates/forge/src/tests/mod.rs b/crates/forge/src/tests/mod.rs deleted file mode 100644 index 6436d9d9..00000000 --- a/crates/forge/src/tests/mod.rs +++ /dev/null @@ -1,31 +0,0 @@ -// -use super::*; - -use git_next_config as config; -use git_next_git as git; - -#[test] -fn test_mock_name() { - let forge = Forge::new_mock(); - assert_eq!(forge.name(), "mock"); -} - -#[test] -fn test_forgejo_name() { - let Ok(fs) = kxio::fs::temp() else { - panic!("fs") - }; - let net = Network::new_mock(); - let repo_details = git::common::repo_details( - 1, - git::Generation::new(), - config::common::forge_details(1, config::ForgeType::MockForge), - Some(config::common::repo_config( - 1, - config::RepoConfigSource::Repo, - )), - config::GitDir::new(fs.base()), - ); - let forge = Forge::new_forgejo(repo_details, net); - assert_eq!(forge.name(), "forgejo"); -} diff --git a/crates/repo-actor/src/lib.rs b/crates/repo-actor/src/lib.rs index c507ee07..fbcd170e 100644 --- a/crates/repo-actor/src/lib.rs +++ b/crates/repo-actor/src/lib.rs @@ -45,12 +45,7 @@ impl RepoActor { net: Network, repo: git::Repository, ) -> Self { - let forge = match details.forge.forge_type() { - #[cfg(feature = "forgejo")] - config::ForgeType::ForgeJo => forge::Forge::new_forgejo(details.clone(), net.clone()), - config::ForgeType::GitHub => forge::Forge::new_github(details.clone(), net.clone()), - config::ForgeType::MockForge => forge::Forge::new_mock(), - }; + let forge = forge::Forge::new(details.clone(), net.clone()); debug!(?forge, "new"); Self { generation,