git-next/crates/git/src/repository/mod.rs
Paul Campbell 341dc97a51 refactor(git): add mock repository and tests
Repository is now behind an enum to allow selection of a mock Repsitory
for use in tests.
2024-05-21 19:47:01 +01:00

95 lines
2.2 KiB
Rust

//
mod mock;
mod open;
mod real;
#[cfg(test)]
mod tests;
use git_next_config::GitDir;
pub use open::OpenRepository;
use crate::repository::mock::MockRepository;
use super::RepoDetails;
#[derive(Clone, Debug)]
pub enum Repository {
Real,
Mock(MockRepository),
}
pub const fn new() -> Repository {
Repository::Real
}
pub fn mock() -> (Repository, MockRepository) {
let mock_repository = MockRepository::new();
(Repository::Mock(mock_repository.clone()), mock_repository)
}
pub trait RepositoryLike {
fn open(&self, gitdir: &GitDir) -> Result<OpenRepository>;
fn git_clone(&self, repo_details: &RepoDetails) -> Result<OpenRepository>;
}
impl std::ops::Deref for Repository {
type Target = dyn RepositoryLike;
fn deref(&self) -> &Self::Target {
match self {
Self::Real => &real::RealRepository,
Self::Mock(mock_repository) => mock_repository,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Direction {
/// Push local changes to the remote.
Push,
/// Fetch changes from the remote to the local repository.
Fetch,
}
impl From<Direction> for gix::remote::Direction {
fn from(value: Direction) -> Self {
match value {
Direction::Push => Self::Push,
Direction::Fetch => Self::Fetch,
}
}
}
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, derive_more::Display)]
pub enum Error {
InvalidGitDir(git_next_config::GitDir),
Io(std::io::Error),
Wait(std::io::Error),
Spawn(std::io::Error),
Validation(String),
Clone(String),
Open(String),
Fetch(String),
MockLock,
}
impl std::error::Error for Error {}
mod gix_errors {
#![cfg(not(tarpaulin_include))] // third-party library errors
use super::Error;
impl From<gix::clone::Error> for Error {
fn from(value: gix::clone::Error) -> Self {
Self::Clone(value.to_string())
}
}
impl From<gix::open::Error> for Error {
fn from(value: gix::open::Error) -> Self {
Self::Open(value.to_string())
}
}
impl From<gix::clone::fetch::Error> for Error {
fn from(value: gix::clone::fetch::Error) -> Self {
Self::Fetch(value.to_string())
}
}
}