From 7818b25a5c5b33c8a56cd597d48ded2296d1e7f3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 24 May 2024 07:00:39 +0100 Subject: [PATCH] refactor: move forgejo branch test to forgejo crate --- crates/forge-forgejo/src/lib.rs | 3 + .../src/tests/data-forgejo-branches-get.json | 0 crates/forge-forgejo/src/tests/mod.rs | 62 +++++++++++++++++ crates/forge/src/tests/forgejo.rs | 68 ------------------- crates/forge/src/tests/mod.rs | 26 ++++++- 5 files changed, 89 insertions(+), 70 deletions(-) rename crates/{forge => forge-forgejo}/src/tests/data-forgejo-branches-get.json (100%) create mode 100644 crates/forge-forgejo/src/tests/mod.rs delete mode 100644 crates/forge/src/tests/forgejo.rs diff --git a/crates/forge-forgejo/src/lib.rs b/crates/forge-forgejo/src/lib.rs index cbac5017..bdc029df 100644 --- a/crates/forge-forgejo/src/lib.rs +++ b/crates/forge-forgejo/src/lib.rs @@ -1,6 +1,9 @@ pub mod branch; mod file; +#[cfg(test)] +mod tests; + use git_next_config as config; use git_next_git as git; diff --git a/crates/forge/src/tests/data-forgejo-branches-get.json b/crates/forge-forgejo/src/tests/data-forgejo-branches-get.json similarity index 100% rename from crates/forge/src/tests/data-forgejo-branches-get.json rename to crates/forge-forgejo/src/tests/data-forgejo-branches-get.json diff --git a/crates/forge-forgejo/src/tests/mod.rs b/crates/forge-forgejo/src/tests/mod.rs new file mode 100644 index 00000000..30223ca6 --- /dev/null +++ b/crates/forge-forgejo/src/tests/mod.rs @@ -0,0 +1,62 @@ +// +use assert2::let_assert; +use kxio::network::{MockNetwork, Network, StatusCode}; +use std::path::Path; + +use crate as forgejo; +use git::ForgeLike as _; +use git_next_config as config; +use git_next_git as git; + +mod branch { + use super::*; + mod get_all { + + use super::*; + + #[tokio::test] + async fn test_branches_get() { + let_assert!(Ok(fs) = kxio::fs::temp()); + let mut net = MockNetwork::new(); + //given + given_forgejo_has_branches(&mut net, 1); + let repo_details = given_a_repo(fs.base(), 1); + + let net = Network::from(net); + let (repo, _reality) = git::repository::mock(); + let forge = forgejo::ForgeJo::new(repo_details, net.clone(), repo); + + //when + let_assert!(Ok(branches) = forge.branches_get_all().await); + + //then + let_assert!(Some(requests) = net.mocked_requests()); + assert_eq!(requests.len(), 1); + assert_eq!(branches, vec![config::BranchName::new("string")]); + } + } +} + +fn given_forgejo_has_branches(net: &mut MockNetwork, i: u32) { + let hostname = config::common::hostname(i); + let path = config::common::repo_path(i); + let api_token = config::common::api_token(i); + use secrecy::ExposeSecret; + let token = api_token.expose_secret(); + let url = format!("https://{hostname}/api/v1/repos/{path}/branches?token={token}"); + let body = include_str!("./data-forgejo-branches-get.json"); + net.add_get_response(&url, StatusCode::OK, body); +} + +fn given_a_repo(path: &Path, i: u32) -> git::RepoDetails { + git::common::repo_details( + i, + git::Generation::new(), + config::common::forge_details(i, config::ForgeType::ForgeJo), + Some(config::common::repo_config( + i, + config::RepoConfigSource::Repo, + )), + config::GitDir::new(path), + ) +} diff --git a/crates/forge/src/tests/forgejo.rs b/crates/forge/src/tests/forgejo.rs deleted file mode 100644 index 915bf97e..00000000 --- a/crates/forge/src/tests/forgejo.rs +++ /dev/null @@ -1,68 +0,0 @@ -// -use assert2::let_assert; - -use git_next_config as config; -use git_next_git as git; - -use kxio::network::{MockNetwork, StatusCode}; - -use super::*; - -#[test] -fn test_name() { - let Ok(fs) = kxio::fs::temp() else { - panic!("fs") - }; - let net = Network::new_mock(); - let (repo, _reality) = git::repository::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, repo); - assert_eq!(forge.name(), "forgejo"); -} - -#[tokio::test] -async fn test_branches_get() { - let Ok(fs) = kxio::fs::temp() else { - panic!("fs") - }; - let mut net = MockNetwork::new(); - let hostname = config::common::hostname(1); - let path = config::common::repo_path(1); - let api_token = config::common::api_token(1); - use secrecy::ExposeSecret; - let token = api_token.expose_secret(); - let url = format!("https://{hostname}/api/v1/repos/{path}/branches?token={token}"); - let body = include_str!("./data-forgejo-branches-get.json"); - net.add_get_response(&url, StatusCode::OK, body); - let net = Network::from(net); - let (repo, _reality) = git::repository::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.clone(), repo); - - let_assert!(Ok(branches) = forge.branches_get_all().await); - - let_assert!(Some(requests) = net.mocked_requests()); - assert_eq!(requests.len(), 1); - - assert_eq!(branches, vec![config::BranchName::new("string")]); -} diff --git a/crates/forge/src/tests/mod.rs b/crates/forge/src/tests/mod.rs index 52985a7a..39be0a6b 100644 --- a/crates/forge/src/tests/mod.rs +++ b/crates/forge/src/tests/mod.rs @@ -1,7 +1,8 @@ +// use super::*; -#[cfg(feature = "forgejo")] -mod forgejo; +use git_next_config as config; +use git_next_git as git; #[cfg(feature = "github")] mod github; @@ -11,3 +12,24 @@ 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, _reality) = git::repository::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, repo); + assert_eq!(forge.name(), "forgejo"); +}