Compare commits
2 commits
0202be19fe
...
22a0b401c0
Author | SHA1 | Date | |
---|---|---|---|
22a0b401c0 | |||
7818b25a5c |
8 changed files with 181 additions and 88 deletions
|
@ -1,6 +1,9 @@
|
||||||
pub mod branch;
|
pub mod branch;
|
||||||
mod file;
|
mod file;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
use git_next_config as config;
|
use git_next_config as config;
|
||||||
use git_next_git as git;
|
use git_next_git as git;
|
||||||
|
|
||||||
|
|
103
crates/forge-forgejo/src/tests/mod.rs
Normal file
103
crates/forge-forgejo/src/tests/mod.rs
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
//
|
||||||
|
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_repo_details(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")]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// mod validate_positions {
|
||||||
|
//
|
||||||
|
// use git::ForgeLike;
|
||||||
|
//
|
||||||
|
// use super::*;
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn should_ok_all_branches_on_same_commit() {
|
||||||
|
// let_assert!(Ok(fs) = kxio::fs::temp());
|
||||||
|
// let mut net = MockNetwork::new();
|
||||||
|
// //given
|
||||||
|
// let repo_details = given_repo_details(fs.base(), 1);
|
||||||
|
// let net = Network::from(net);
|
||||||
|
// let (repo, _reality) = git::repository::mock();
|
||||||
|
// let forge = given_a_forge(repo_details, &net, repo);
|
||||||
|
// let open_repository = given_an_open_repository();
|
||||||
|
// let repo_config = given_a_repo_config();
|
||||||
|
//
|
||||||
|
// let forge = forgejo::ForgeJo::new(repo_details, net.clone(), repo);
|
||||||
|
//
|
||||||
|
// let_assert!(
|
||||||
|
// Ok(positions) = forge
|
||||||
|
// .branches_validate_positions(open_repository, repo_config)
|
||||||
|
// .await
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// fn given_a_forge(
|
||||||
|
// repo_details: git::RepoDetails,
|
||||||
|
// net: &Network,
|
||||||
|
// repo: git::Repository,
|
||||||
|
// ) -> forgejo::ForgeJo {
|
||||||
|
// forgejo::ForgeJo::new(repo_details, net.clone(), repo)
|
||||||
|
// }
|
||||||
|
// fn given_an_open_repository() -> git::OpenRepository {
|
||||||
|
// todo!()
|
||||||
|
// }
|
||||||
|
// fn given_a_repo_config() -> config::RepoConfig {
|
||||||
|
// todo!()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
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_repo_details(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),
|
||||||
|
)
|
||||||
|
}
|
|
@ -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")]);
|
|
||||||
}
|
|
|
@ -1,7 +1,8 @@
|
||||||
|
//
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[cfg(feature = "forgejo")]
|
use git_next_config as config;
|
||||||
mod forgejo;
|
use git_next_git as git;
|
||||||
|
|
||||||
#[cfg(feature = "github")]
|
#[cfg(feature = "github")]
|
||||||
mod github;
|
mod github;
|
||||||
|
@ -11,3 +12,24 @@ fn test_mock_name() {
|
||||||
let forge = Forge::new_mock();
|
let forge = Forge::new_mock();
|
||||||
assert_eq!(forge.name(), "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");
|
||||||
|
}
|
||||||
|
|
|
@ -133,6 +133,17 @@ impl OpenRepositoryLike for MockOpenRepository {
|
||||||
.map(|inner| inner.push(repo_details, branch_name, to_commit, force))
|
.map(|inner| inner.push(repo_details, branch_name, to_commit, force))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn commit_log(
|
||||||
|
&self,
|
||||||
|
branch_name: git_next_config::BranchName,
|
||||||
|
find_commits: Vec<crate::Commit>,
|
||||||
|
) -> Vec<crate::Commit> {
|
||||||
|
self.inner
|
||||||
|
.lock()
|
||||||
|
.map(|inner| inner.commit_log(branch_name, find_commits))
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl OpenRepositoryLike for InnerMockOpenRepository {
|
impl OpenRepositoryLike for InnerMockOpenRepository {
|
||||||
fn find_default_remote(&self, direction: Direction) -> Option<GitRemote> {
|
fn find_default_remote(&self, direction: Direction) -> Option<GitRemote> {
|
||||||
|
@ -155,4 +166,12 @@ impl OpenRepositoryLike for InnerMockOpenRepository {
|
||||||
) -> std::prelude::v1::Result<(), crate::push::Error> {
|
) -> std::prelude::v1::Result<(), crate::push::Error> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn commit_log(
|
||||||
|
&self,
|
||||||
|
branch_name: git_next_config::BranchName,
|
||||||
|
find_commits: Vec<crate::Commit>,
|
||||||
|
) -> Vec<crate::Commit> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,38 +6,44 @@ pub mod oreal;
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use git_next_config::BranchName;
|
use crate as git;
|
||||||
|
use git::repository::open::oreal::RealOpenRepository;
|
||||||
use crate::{
|
use git::repository::Direction;
|
||||||
fetch, push,
|
use git_next_config as config;
|
||||||
repository::{mock::MockOpenRepository, open::oreal::RealOpenRepository, Direction},
|
|
||||||
GitRef, GitRemote, RepoDetails,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum OpenRepository {
|
pub enum OpenRepository {
|
||||||
Real(oreal::RealOpenRepository),
|
Real(RealOpenRepository),
|
||||||
Mock(MockOpenRepository), // TODO: (#38) contain a mock model of a repo
|
Mock(git::repository::mock::MockOpenRepository), // TODO: (#38) contain a mock model of a repo
|
||||||
}
|
}
|
||||||
impl OpenRepository {
|
impl OpenRepository {
|
||||||
pub fn real(gix_repo: gix::Repository) -> Self {
|
pub fn real(gix_repo: gix::Repository) -> Self {
|
||||||
Self::Real(RealOpenRepository::new(Arc::new(Mutex::new(gix_repo))))
|
Self::Real(oreal::RealOpenRepository::new(Arc::new(Mutex::new(
|
||||||
|
gix_repo,
|
||||||
|
))))
|
||||||
}
|
}
|
||||||
#[cfg(not(tarpaulin_include))] // don't test mocks
|
#[cfg(not(tarpaulin_include))] // don't test mocks
|
||||||
pub const fn mock(mock: MockOpenRepository) -> Self {
|
pub const fn mock(mock: git::repository::mock::MockOpenRepository) -> Self {
|
||||||
Self::Mock(mock)
|
Self::Mock(mock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub trait OpenRepositoryLike {
|
pub trait OpenRepositoryLike {
|
||||||
fn find_default_remote(&self, direction: Direction) -> Option<GitRemote>;
|
fn find_default_remote(&self, direction: Direction) -> Option<git::GitRemote>;
|
||||||
fn fetch(&self) -> Result<(), fetch::Error>;
|
fn fetch(&self) -> Result<(), git::fetch::Error>;
|
||||||
fn push(
|
fn push(
|
||||||
&self,
|
&self,
|
||||||
repo_details: &RepoDetails,
|
repo_details: &git::RepoDetails,
|
||||||
branch_name: BranchName,
|
branch_name: config::BranchName,
|
||||||
to_commit: GitRef,
|
to_commit: git::GitRef,
|
||||||
force: push::Force,
|
force: git::push::Force,
|
||||||
) -> Result<(), push::Error>;
|
) -> Result<(), git::push::Error>;
|
||||||
|
|
||||||
|
/// List of commits in a branch, optionally up-to any specified commit.
|
||||||
|
fn commit_log(
|
||||||
|
&self,
|
||||||
|
branch_name: config::BranchName,
|
||||||
|
find_commits: Vec<git::Commit>,
|
||||||
|
) -> Vec<git::Commit>;
|
||||||
}
|
}
|
||||||
impl std::ops::Deref for OpenRepository {
|
impl std::ops::Deref for OpenRepository {
|
||||||
type Target = dyn OpenRepositoryLike;
|
type Target = dyn OpenRepositoryLike;
|
||||||
|
|
|
@ -100,6 +100,14 @@ impl super::OpenRepositoryLike for RealOpenRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn commit_log(
|
||||||
|
&self,
|
||||||
|
branch_name: git_next_config::BranchName,
|
||||||
|
find_commits: Vec<crate::Commit>,
|
||||||
|
) -> Vec<crate::Commit> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&gix::Url> for GitRemote {
|
impl From<&gix::Url> for GitRemote {
|
||||||
|
|
Loading…
Reference in a new issue