refactor: move forgejo branch test to forgejo crate
All checks were successful
Rust / build (push) Successful in 1m11s
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/cron/cron-docker-builder Pipeline was successful
ci/woodpecker/cron/push-next Pipeline was successful
ci/woodpecker/cron/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-05-24 07:00:39 +01:00
parent 0202be19fe
commit 7818b25a5c
5 changed files with 89 additions and 70 deletions

View file

@ -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;

View file

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

View file

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

View file

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