2024-05-22 08:41:30 +01:00
|
|
|
//
|
2024-04-24 07:08:03 +01:00
|
|
|
use assert2::let_assert;
|
2024-05-26 08:56:01 +01:00
|
|
|
use git::{repository::Direction, validation::repo::validate_repo};
|
2024-05-11 19:46:20 +01:00
|
|
|
use git_next_config::{
|
2024-05-22 08:41:30 +01:00
|
|
|
self as config, ForgeType, GitDir, Hostname, RepoBranches, RepoConfig, RepoConfigSource,
|
|
|
|
RepoPath,
|
2024-05-11 19:46:20 +01:00
|
|
|
};
|
2024-05-23 16:19:28 +01:00
|
|
|
use git_next_git as git;
|
2024-04-21 18:23:57 +01:00
|
|
|
|
2024-04-28 18:13:45 +01:00
|
|
|
type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
|
|
|
|
|
2024-04-21 18:23:57 +01:00
|
|
|
#[test]
|
2024-04-28 18:13:45 +01:00
|
|
|
fn test_repo_config_load() -> Result<()> {
|
2024-05-08 07:34:35 +01:00
|
|
|
let toml = r#"[branches]
|
2024-04-21 18:23:57 +01:00
|
|
|
main = "main"
|
|
|
|
next = "next"
|
|
|
|
dev = "dev"
|
|
|
|
|
|
|
|
[options]
|
|
|
|
"#;
|
2024-06-19 07:02:18 +01:00
|
|
|
let config = RepoConfig::parse(toml)?;
|
2024-04-21 18:23:57 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
config,
|
2024-05-15 07:55:05 +01:00
|
|
|
RepoConfig::new(
|
|
|
|
RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string(),),
|
|
|
|
RepoConfigSource::Repo
|
|
|
|
)
|
2024-04-21 18:23:57 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-04-23 18:53:55 +01:00
|
|
|
|
|
|
|
#[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");
|
|
|
|
}
|
2024-04-24 07:08:03 +01:00
|
|
|
|
|
|
|
#[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
|
2024-04-28 18:13:45 +01:00
|
|
|
fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> {
|
2024-05-26 08:56:01 +01:00
|
|
|
let cli_crate_dir = std::env::current_dir().map_err(git::validation::repo::Error::Io)?;
|
2024-05-11 11:11:13 +01:00
|
|
|
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
|
2024-05-22 08:41:30 +01:00
|
|
|
let mut repo_details = git::common::repo_details(
|
2024-04-24 07:08:03 +01:00
|
|
|
1,
|
2024-06-20 19:03:11 +01:00
|
|
|
git::Generation::default(),
|
2024-05-22 08:41:30 +01:00
|
|
|
config::common::forge_details(1, ForgeType::MockForge),
|
2024-04-24 07:08:03 +01:00
|
|
|
None,
|
2024-05-11 18:58:47 +01:00
|
|
|
GitDir::new(root), // Server GitDir - should be ignored
|
2024-04-24 07:08:03 +01:00
|
|
|
);
|
2024-05-15 07:55:05 +01:00
|
|
|
repo_details.forge = repo_details
|
|
|
|
.forge
|
|
|
|
.with_hostname(Hostname::new("git.kemitix.net"));
|
|
|
|
repo_details.repo_path = RepoPath::new("kemitix/git-next".to_string());
|
2024-05-03 17:50:50 +01:00
|
|
|
let gitdir = &repo_details.gitdir;
|
2024-06-19 16:40:10 +01:00
|
|
|
let open_repository = git::repository::real().open(gitdir)?;
|
2024-05-18 11:41:18 +01:00
|
|
|
let_assert!(
|
2024-06-19 16:40:10 +01:00
|
|
|
Some(found_git_remote) = open_repository.find_default_remote(Direction::Push),
|
2024-05-18 11:41:18 +01:00
|
|
|
"Default Push Remote not found"
|
|
|
|
);
|
2024-04-24 07:08:03 +01:00
|
|
|
let config_git_remote = repo_details.git_remote();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
found_git_remote, config_git_remote,
|
|
|
|
"Default Push Remote must match config"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-04-28 18:13:45 +01:00
|
|
|
fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> {
|
2024-05-26 08:56:01 +01:00
|
|
|
let cli_crate_dir = std::env::current_dir().map_err(git::validation::repo::Error::Io)?;
|
2024-05-11 11:11:13 +01:00
|
|
|
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
|
2024-05-22 08:41:30 +01:00
|
|
|
let mut repo_details = git::common::repo_details(
|
2024-04-24 07:08:03 +01:00
|
|
|
1,
|
2024-06-20 19:03:11 +01:00
|
|
|
git::Generation::default(),
|
2024-05-22 08:41:30 +01:00
|
|
|
config::common::forge_details(1, ForgeType::MockForge),
|
2024-04-24 07:08:03 +01:00
|
|
|
None,
|
2024-05-11 18:58:47 +01:00
|
|
|
GitDir::new(root), // Server GitDir - should be ignored
|
2024-06-19 16:40:10 +01:00
|
|
|
)
|
|
|
|
.with_repo_path(RepoPath::new("kemitix/git-next".to_string()));
|
2024-05-15 07:55:05 +01:00
|
|
|
repo_details.forge = repo_details
|
|
|
|
.forge
|
|
|
|
.with_hostname(Hostname::new("git.kemitix.net"));
|
2024-05-09 21:53:50 +01:00
|
|
|
let gitdir = &repo_details.gitdir;
|
2024-06-19 16:40:10 +01:00
|
|
|
let repository = git::repository::real().open(gitdir)?;
|
|
|
|
validate_repo(&*repository, &repo_details)?;
|
2024-04-24 07:08:03 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-05-09 21:53:50 +01:00
|
|
|
fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() -> Result<()> {
|
2024-05-26 08:56:01 +01:00
|
|
|
let_assert!(
|
|
|
|
Ok(cli_crate_dir) = std::env::current_dir().map_err(git::validation::repo::Error::Io)
|
|
|
|
);
|
2024-05-11 11:11:13 +01:00
|
|
|
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
|
2024-06-19 16:40:10 +01:00
|
|
|
let repo_details = git::common::repo_details(
|
2024-04-24 07:08:03 +01:00
|
|
|
1,
|
2024-06-20 19:03:11 +01:00
|
|
|
git::Generation::default(),
|
2024-05-22 08:41:30 +01:00
|
|
|
config::common::forge_details(1, ForgeType::MockForge),
|
2024-04-24 07:08:03 +01:00
|
|
|
None,
|
2024-05-11 18:58:47 +01:00
|
|
|
GitDir::new(root), // Server GitDir - should be ignored
|
2024-06-19 16:40:10 +01:00
|
|
|
)
|
|
|
|
.with_repo_path(RepoPath::new("hello/world".to_string()));
|
2024-05-09 21:53:50 +01:00
|
|
|
let gitdir = &repo_details.gitdir;
|
2024-06-19 16:40:10 +01:00
|
|
|
let repository = git::repository::real().open(gitdir)?;
|
|
|
|
let_assert!(Err(_) = validate_repo(&*repository, &repo_details));
|
2024-05-09 21:53:50 +01:00
|
|
|
|
|
|
|
Ok(())
|
2024-04-24 07:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn git_remote_to_string_is_as_expected() {
|
2024-05-23 16:19:28 +01:00
|
|
|
let git_remote = git::GitRemote::new(Hostname::new("foo"), RepoPath::new("bar".to_string()));
|
2024-04-24 07:08:03 +01:00
|
|
|
let as_string = git_remote.to_string();
|
|
|
|
|
|
|
|
assert_eq!(as_string, "foo:bar");
|
|
|
|
}
|