2024-04-24 07:08:03 +01:00
|
|
|
use assert2::let_assert;
|
2024-05-11 19:46:20 +01:00
|
|
|
use git_next_config::{
|
|
|
|
ForgeType, GitDir, Hostname, RepoBranches, RepoConfig, RepoConfigSource, RepoPath,
|
|
|
|
ServerRepoConfig,
|
|
|
|
};
|
|
|
|
use git_next_git::{self as git, Generation, GitRemote, Repository};
|
2024-05-03 17:50:50 +01:00
|
|
|
use gix::remote::Direction;
|
2024-05-11 19:46:20 +01:00
|
|
|
use kxio::fs;
|
2024-04-21 18:23:57 +01:00
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
|
2024-05-11 19:46:20 +01:00
|
|
|
use crate::gitforge::tests::common;
|
2024-04-21 18:23:57 +01:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
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 load_should_parse_server_config() -> Result<()> {
|
2024-04-28 08:05:09 +01:00
|
|
|
let fs = fs::temp()?;
|
|
|
|
fs.file_write(
|
|
|
|
&fs.base().join("git-next-server.toml"),
|
2024-04-21 18:23:57 +01:00
|
|
|
r#"
|
2024-05-10 22:01:47 +01:00
|
|
|
[http]
|
|
|
|
addr = "0.0.0.0"
|
|
|
|
port = 8080
|
|
|
|
|
2024-04-21 18:23:57 +01:00
|
|
|
[webhook]
|
|
|
|
url = "http://localhost:9909/webhook"
|
|
|
|
|
|
|
|
[storage]
|
|
|
|
path = "/opt/git-next/data"
|
|
|
|
|
|
|
|
[forge.default]
|
|
|
|
forge_type = "MockForge"
|
|
|
|
hostname = "git.example.net"
|
|
|
|
user = "Bob"
|
|
|
|
token = "API-Token"
|
|
|
|
|
|
|
|
[forge.default.repos]
|
|
|
|
hello = { repo = "user/hello", branch = "main", gitdir = "/opt/git/user/hello.git" }
|
|
|
|
world = { repo = "user/world", branch = "master", main = "main", next = "next", dev = "dev" }
|
|
|
|
|
|
|
|
[forge.default.repos.sam]
|
|
|
|
repo = "user/sam"
|
|
|
|
branch = "main"
|
|
|
|
main = "master"
|
|
|
|
next = "upcoming"
|
|
|
|
dev = "sam-dev"
|
|
|
|
"#,
|
|
|
|
)
|
2024-04-28 08:05:09 +01:00
|
|
|
?;
|
|
|
|
let_assert!(Ok(config) = ServerConfig::load(&fs));
|
2024-04-21 18:23:57 +01:00
|
|
|
let expected = ServerConfig {
|
2024-05-10 22:01:47 +01:00
|
|
|
http: Http {
|
|
|
|
addr: "0.0.0.0".to_string(),
|
|
|
|
port: 8080,
|
|
|
|
},
|
2024-04-21 18:23:57 +01:00
|
|
|
webhook: Webhook {
|
|
|
|
url: "http://localhost:9909/webhook".to_string(),
|
|
|
|
},
|
|
|
|
storage: ServerStorage {
|
|
|
|
path: "/opt/git-next/data".into(),
|
|
|
|
},
|
|
|
|
forge: HashMap::from([(
|
|
|
|
"default".to_string(),
|
|
|
|
ForgeConfig {
|
|
|
|
forge_type: ForgeType::MockForge,
|
|
|
|
hostname: "git.example.net".to_string(),
|
|
|
|
user: "Bob".to_string(),
|
|
|
|
token: "API-Token".to_string(),
|
|
|
|
repos: HashMap::from([
|
|
|
|
(
|
|
|
|
"hello".to_string(),
|
|
|
|
ServerRepoConfig {
|
|
|
|
repo: "user/hello".to_string(),
|
|
|
|
branch: "main".to_string(),
|
|
|
|
gitdir: Some("/opt/git/user/hello.git".into()),
|
|
|
|
main: None,
|
|
|
|
next: None,
|
|
|
|
dev: None,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"world".to_string(),
|
|
|
|
ServerRepoConfig {
|
|
|
|
repo: "user/world".to_string(),
|
|
|
|
branch: "master".to_string(),
|
|
|
|
gitdir: None,
|
|
|
|
main: Some("main".to_string()),
|
|
|
|
next: Some("next".to_string()),
|
|
|
|
dev: Some("dev".to_string()),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"sam".to_string(),
|
|
|
|
ServerRepoConfig {
|
|
|
|
repo: "user/sam".to_string(),
|
|
|
|
branch: "main".to_string(),
|
|
|
|
gitdir: None,
|
|
|
|
main: Some("master".to_string()),
|
|
|
|
next: Some("upcoming".to_string()),
|
|
|
|
dev: Some("sam-dev".to_string()),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
)]),
|
|
|
|
};
|
|
|
|
assert_eq!(config, expected, "ServerConfig");
|
|
|
|
|
|
|
|
if let Some(forge) = config.forge.get("world") {
|
|
|
|
if let Some(repo) = forge.repos.get("sam") {
|
|
|
|
let repo_config = repo.repo_config();
|
|
|
|
let expected = Some(RepoConfig {
|
|
|
|
branches: RepoBranches {
|
|
|
|
main: "master".to_string(),
|
|
|
|
next: "upcoming".to_string(),
|
|
|
|
dev: "sam-dev".to_string(),
|
|
|
|
},
|
2024-05-08 07:34:35 +01:00
|
|
|
source: RepoConfigSource::Server,
|
2024-04-21 18:23:57 +01:00
|
|
|
});
|
|
|
|
assert_eq!(repo_config, expected, "RepoConfig");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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]
|
|
|
|
"#;
|
|
|
|
let config = RepoConfig::load(toml)?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
config,
|
|
|
|
RepoConfig {
|
|
|
|
branches: RepoBranches {
|
|
|
|
main: "main".to_string(),
|
|
|
|
next: "next".to_string(),
|
|
|
|
dev: "dev".to_string(),
|
|
|
|
},
|
2024-05-08 07:34:35 +01:00
|
|
|
source: 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-11 19:46:20 +01:00
|
|
|
let cli_crate_dir = std::env::current_dir().map_err(git::validate::Error::Io)?;
|
2024-05-11 11:11:13 +01:00
|
|
|
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
|
2024-04-24 07:08:03 +01:00
|
|
|
let mut repo_details = common::repo_details(
|
|
|
|
1,
|
2024-05-11 19:46:20 +01:00
|
|
|
Generation::new(),
|
2024-04-24 07:08:03 +01:00
|
|
|
common::forge_details(1, ForgeType::MockForge),
|
|
|
|
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
|
|
|
);
|
|
|
|
repo_details.forge.hostname = Hostname("git.kemitix.net".to_string());
|
|
|
|
repo_details.repo_path = RepoPath("kemitix/git-next".to_string());
|
2024-05-03 17:50:50 +01:00
|
|
|
let gitdir = &repo_details.gitdir;
|
2024-05-09 22:23:57 +01:00
|
|
|
let repository = Repository::open(gitdir)?;
|
2024-05-11 19:46:20 +01:00
|
|
|
let found_git_remote = git::validate::find_default_remote(&repository, Direction::Push)?;
|
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-11 19:46:20 +01:00
|
|
|
let cli_crate_dir = std::env::current_dir().map_err(git::validate::Error::Io)?;
|
2024-05-11 11:11:13 +01:00
|
|
|
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
|
2024-04-24 07:08:03 +01:00
|
|
|
let mut repo_details = common::repo_details(
|
|
|
|
1,
|
2024-05-11 19:46:20 +01:00
|
|
|
Generation::new(),
|
2024-04-24 07:08:03 +01:00
|
|
|
common::forge_details(1, ForgeType::MockForge),
|
|
|
|
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
|
|
|
);
|
|
|
|
repo_details.forge.hostname = Hostname("git.kemitix.net".to_string());
|
|
|
|
repo_details.repo_path = RepoPath("kemitix/git-next".to_string());
|
2024-05-09 21:53:50 +01:00
|
|
|
let gitdir = &repo_details.gitdir;
|
2024-05-09 22:23:57 +01:00
|
|
|
let repository = Repository::open(gitdir)?;
|
2024-05-11 19:46:20 +01:00
|
|
|
git::validate(&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-11 19:46:20 +01:00
|
|
|
let_assert!(Ok(cli_crate_dir) = std::env::current_dir().map_err(git::validate::Error::Io));
|
2024-05-11 11:11:13 +01:00
|
|
|
let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent()));
|
2024-04-24 07:08:03 +01:00
|
|
|
let mut repo_details = common::repo_details(
|
|
|
|
1,
|
2024-05-11 19:46:20 +01:00
|
|
|
Generation::new(),
|
2024-04-24 07:08:03 +01:00
|
|
|
common::forge_details(1, ForgeType::MockForge),
|
|
|
|
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
|
|
|
);
|
|
|
|
repo_details.forge.hostname = Hostname("localhost".to_string());
|
|
|
|
repo_details.repo_path = RepoPath("hello/world".to_string());
|
2024-05-09 21:53:50 +01:00
|
|
|
let gitdir = &repo_details.gitdir;
|
2024-05-09 22:23:57 +01:00
|
|
|
let repository = Repository::open(gitdir)?;
|
2024-05-11 19:46:20 +01:00
|
|
|
let_assert!(Err(_) = git::validate(&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() {
|
|
|
|
let git_remote = GitRemote::new(Hostname("foo".to_string()), RepoPath("bar".to_string()));
|
|
|
|
let as_string = git_remote.to_string();
|
|
|
|
|
|
|
|
assert_eq!(as_string, "foo:bar");
|
|
|
|
}
|