test: add more tests to forge crate
All checks were successful
Rust / build (push) Successful in 1m11s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful

This commit is contained in:
Paul Campbell 2024-06-01 12:03:30 +01:00
parent 1010eaec64
commit 98839c8a00
4 changed files with 63 additions and 49 deletions

View file

@ -17,18 +17,18 @@ pub enum Forge {
Github(git_next_forge_github::Github), Github(git_next_forge_github::Github),
} }
impl Forge { impl Forge {
pub const fn new_mock() -> Self { pub fn new(repo_details: git::RepoDetails, net: Network) -> Self {
Self::Mock(mock_forge::MockForge::new()) match repo_details.forge.forge_type() {
} #[cfg(feature = "forgejo")]
git_next_config::ForgeType::ForgeJo => {
#[cfg(feature = "forgejo")] Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net))
pub const fn new_forgejo(repo_details: git::RepoDetails, net: Network) -> Self { }
Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net)) #[cfg(feature = "github")]
} git_next_config::ForgeType::GitHub => {
Self::Github(github::Github::new(repo_details, net))
#[cfg(feature = "github")] }
pub const fn new_github(repo_details: git::RepoDetails, net: Network) -> Self { git_next_config::ForgeType::MockForge => Self::Mock(mock_forge::MockForge::new()),
Self::Github(github::Github::new(repo_details, net)) }
} }
} }
impl std::ops::Deref for Forge { impl std::ops::Deref for Forge {

50
crates/forge/src/tests.rs Normal file
View file

@ -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()),
)
}

View file

@ -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");
}

View file

@ -45,12 +45,7 @@ impl RepoActor {
net: Network, net: Network,
repo: git::Repository, repo: git::Repository,
) -> Self { ) -> Self {
let forge = match details.forge.forge_type() { let forge = forge::Forge::new(details.clone(), net.clone());
#[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(),
};
debug!(?forge, "new"); debug!(?forge, "new");
Self { Self {
generation, generation,