git-next/crates/server/src/config/tests.rs

128 lines
4.1 KiB
Rust
Raw Normal View History

//
use assert2::let_assert;
use git::{repository::Direction, validation::repo::validate_repo};
use git_next_config::{
self as config, ForgeType, GitDir, Hostname, RepoBranches, RepoConfig, RepoConfigSource,
RepoPath,
};
use git_next_git as git;
type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
#[test]
fn test_repo_config_load() -> Result<()> {
let toml = r#"[branches]
main = "main"
next = "next"
dev = "dev"
[options]
"#;
2024-06-19 07:02:18 +01:00
let config = RepoConfig::parse(toml)?;
assert_eq!(
config,
RepoConfig::new(
RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string(),),
RepoConfigSource::Repo
)
);
Ok(())
}
#[test]
fn gitdir_should_display_as_pathbuf() {
//given
let gitdir = GitDir::from("foo/dir");
//when
let result = format!("{}", gitdir);
//then
assert_eq!(result, "foo/dir");
}
#[test]
// NOTE: this test assumes it is being run in a cloned worktree from the project's home repo:
// git.kemitix.net:kemitix/git-next
// If the default push remote is something else, then this test will fail
fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> {
let cli_crate_dir = std::env::current_dir().map_err(git::validation::repo::Error::Io)?;
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
let mut repo_details = git::common::repo_details(
1,
git::Generation::new(),
config::common::forge_details(1, ForgeType::MockForge),
None,
2024-05-11 18:58:47 +01:00
GitDir::new(root), // Server GitDir - should be ignored
);
repo_details.forge = repo_details
.forge
.with_hostname(Hostname::new("git.kemitix.net"));
repo_details.repo_path = RepoPath::new("kemitix/git-next".to_string());
let gitdir = &repo_details.gitdir;
let open_repository = git::repository::real().open(gitdir)?;
let_assert!(
Some(found_git_remote) = open_repository.find_default_remote(Direction::Push),
"Default Push Remote not found"
);
let config_git_remote = repo_details.git_remote();
assert_eq!(
found_git_remote, config_git_remote,
"Default Push Remote must match config"
);
Ok(())
}
#[test]
fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> {
let cli_crate_dir = std::env::current_dir().map_err(git::validation::repo::Error::Io)?;
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
let mut repo_details = git::common::repo_details(
1,
git::Generation::new(),
config::common::forge_details(1, ForgeType::MockForge),
None,
2024-05-11 18:58:47 +01:00
GitDir::new(root), // Server GitDir - should be ignored
)
.with_repo_path(RepoPath::new("kemitix/git-next".to_string()));
repo_details.forge = repo_details
.forge
.with_hostname(Hostname::new("git.kemitix.net"));
let gitdir = &repo_details.gitdir;
let repository = git::repository::real().open(gitdir)?;
validate_repo(&*repository, &repo_details)?;
Ok(())
}
#[test]
fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() -> Result<()> {
let_assert!(
Ok(cli_crate_dir) = std::env::current_dir().map_err(git::validation::repo::Error::Io)
);
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
let repo_details = git::common::repo_details(
1,
git::Generation::new(),
config::common::forge_details(1, ForgeType::MockForge),
None,
2024-05-11 18:58:47 +01:00
GitDir::new(root), // Server GitDir - should be ignored
)
.with_repo_path(RepoPath::new("hello/world".to_string()));
let gitdir = &repo_details.gitdir;
let repository = git::repository::real().open(gitdir)?;
let_assert!(Err(_) = validate_repo(&*repository, &repo_details));
Ok(())
}
#[test]
fn git_remote_to_string_is_as_expected() {
let git_remote = git::GitRemote::new(Hostname::new("foo"), RepoPath::new("bar".to_string()));
let as_string = git_remote.to_string();
assert_eq!(as_string, "foo:bar");
}