// type Result = core::result::Result>; type TestResult = Result<()>; mod server_repo_config { use std::path::PathBuf; use assert2::let_assert; use crate::{BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource, RepoPath}; use super::super::server_repo_config::*; #[test] fn should_not_return_repo_config_when_no_branches() { let src = ServerRepoConfig::new("".to_string(), "".to_string(), None, None, None, None); let_assert!(None = src.repo_config()); } #[test] fn should_return_repo_config_when_branches() { let src = ServerRepoConfig::new( "".to_string(), "".to_string(), None, Some("main".to_string()), Some("next".to_string()), Some("dev".to_string()), ); let_assert!(Some(rc) = src.repo_config()); assert_eq!( rc, RepoConfig::new( RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()), RepoConfigSource::Server ) ); } #[test] fn should_return_repo() { let src = ServerRepoConfig::new( "repo".to_string(), "branch".to_string(), None, Some("main".to_string()), Some("next".to_string()), Some("dev".to_string()), ); assert_eq!(src.repo(), RepoPath::new("repo".to_string())); } #[test] fn should_return_branch() { let src = ServerRepoConfig::new( "repo".to_string(), "branch".to_string(), None, Some("main".to_string()), Some("next".to_string()), Some("dev".to_string()), ); assert_eq!(src.branch(), BranchName::new("branch".to_string())); } #[test] fn should_return_gitdir() { let src = ServerRepoConfig::new( "repo".to_string(), "branch".to_string(), Some("gitdir".into()), Some("main".to_string()), Some("next".to_string()), Some("dev".to_string()), ); assert_eq!( src.gitdir(), Some(GitDir::new(&PathBuf::default().join("gitdir"))) ); } } mod repo_config { use crate::{RepoBranches, RepoConfigSource}; use super::super::repo_config::*; use super::*; #[test] fn should_parse_toml() -> TestResult { let toml = r#" [branches] main = "main" next = "next" dev = "dev" "#; let rc = RepoConfig::load(toml)?; assert_eq!( rc, RepoConfig::new( RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string(),), RepoConfigSource::Repo // reading from repo is the default ) ); Ok(()) } #[test] fn should_return_branches() { let branches = RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()); let repo_config = RepoConfig::new(branches.clone(), RepoConfigSource::Repo); assert_eq!(repo_config.branches(), &branches); } #[test] fn should_return_source() { let repo_config = RepoConfig::new( RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()), RepoConfigSource::Repo, ); assert_eq!(repo_config.source(), RepoConfigSource::Repo); } } mod forge_config { use std::collections::BTreeMap; use secrecy::ExposeSecret; use crate::{ForgeConfig, ForgeType, Hostname, RepoAlias, ServerRepoConfig, User}; #[test] fn should_return_repos() { let forge_type = ForgeType::MockForge; let hostname = "localhost".to_string(); let user = "bob".to_string(); let token = "alpha".to_string(); let red = ServerRepoConfig::new( "red".to_string(), "main".to_string(), None, None, None, None, ); let blue = ServerRepoConfig::new( "blue".to_string(), "main".to_string(), None, None, None, None, ); let mut repos = BTreeMap::new(); repos.insert("red".to_string(), red.clone()); repos.insert("blue".to_string(), blue.clone()); let fc = ForgeConfig::new(forge_type, hostname, user, token, repos); let returned_repos = fc.repos().collect::>(); assert_eq!( returned_repos, vec![ // alphabetical order by key (RepoAlias::new("blue"), &blue), (RepoAlias::new("red"), &red), ] ); } #[test] fn should_return_forge_type() { let forge_type = ForgeType::MockForge; let hostname = "localhost".to_string(); let user = "bob".to_string(); let token = "alpha".to_string(); let repos = BTreeMap::new(); let fc = ForgeConfig::new(forge_type, hostname, user, token, repos); assert_eq!(fc.forge_type(), ForgeType::MockForge); } #[test] fn should_return_hostname() { let forge_type = ForgeType::MockForge; let hostname = "localhost".to_string(); let user = "bob".to_string(); let token = "alpha".to_string(); let repos = BTreeMap::new(); let fc = ForgeConfig::new(forge_type, hostname, user, token, repos); assert_eq!(fc.hostname(), Hostname::new("localhost")); } #[test] fn should_return_user() { let forge_type = ForgeType::MockForge; let hostname = "localhost".to_string(); let user = "bob".to_string(); let token = "alpha".to_string(); let repos = BTreeMap::new(); let fc = ForgeConfig::new(forge_type, hostname, user, token, repos); assert_eq!(fc.user(), User::new("bob".to_string())); } #[test] fn should_return_token() { let forge_type = ForgeType::MockForge; let hostname = "localhost".to_string(); let user = "bob".to_string(); let token = "alpha".to_string(); let repos = BTreeMap::new(); let fc = ForgeConfig::new(forge_type, hostname, user, token, repos); assert_eq!(fc.token().expose_secret(), "alpha"); } #[test] fn should_return_repo() { let forge_type = ForgeType::MockForge; let hostname = "localhost".to_string(); let user = "bob".to_string(); let token = "alpha".to_string(); let red = ServerRepoConfig::new( "red".to_string(), "main".to_string(), None, None, None, None, ); let blue = ServerRepoConfig::new( "blue".to_string(), "main".to_string(), None, None, None, None, ); let mut repos = BTreeMap::new(); repos.insert("red".to_string(), red.clone()); repos.insert("blue".to_string(), blue); let fc = ForgeConfig::new(forge_type, hostname, user, token, repos); let returned_repo = fc.get_repo("red"); assert_eq!(returned_repo, Some(&red),); } } mod forge_details { use std::collections::BTreeMap; use secrecy::ExposeSecret; use crate::{ApiToken, ForgeConfig, ForgeDetails, ForgeName, ForgeType, Hostname, User}; #[test] fn should_return_forge_name() { let forge_type = ForgeType::MockForge; let hostname = Hostname::new("localhost".to_string()); let user = User::new("bob".to_string()); let token = ApiToken::new("alpha".to_string().into()); let forge_name = ForgeName::new("gamma".to_string()); let forge_details = ForgeDetails::new(forge_name.clone(), forge_type, hostname, user, token); let result = forge_details.forge_name(); assert_eq!(result, &forge_name); } #[test] fn should_return_forge_type() { let forge_type = ForgeType::MockForge; let hostname = Hostname::new("localhost".to_string()); let user = User::new("bob".to_string()); let token = ApiToken::new("alpha".to_string().into()); let forge_name = ForgeName::new("gamma".to_string()); let forge_details = ForgeDetails::new(forge_name, forge_type, hostname, user, token); let result = forge_details.forge_type(); assert_eq!(result, forge_type); } #[test] fn should_return_hostname() { let forge_type = ForgeType::MockForge; let hostname = Hostname::new("localhost".to_string()); let user = User::new("bob".to_string()); let token = ApiToken::new("alpha".to_string().into()); let forge_name = ForgeName::new("gamma".to_string()); let forge_details = ForgeDetails::new(forge_name, forge_type, hostname.clone(), user, token); let result = forge_details.hostname(); assert_eq!(result, &hostname); } #[test] fn should_return_user() { let forge_type = ForgeType::MockForge; let hostname = Hostname::new("localhost".to_string()); let user = User::new("bob".to_string()); let token = ApiToken::new("alpha".to_string().into()); let forge_name = ForgeName::new("gamma".to_string()); let forge_details = ForgeDetails::new(forge_name, forge_type, hostname, user.clone(), token); let result = forge_details.user(); assert_eq!(result, &user); } #[test] fn should_return_token() { let forge_type = ForgeType::MockForge; let hostname = Hostname::new("localhost".to_string()); let user = User::new("bob".to_string()); let token = ApiToken::new("alpha".to_string().into()); let forge_name = ForgeName::new("gamma".to_string()); let forge_details = ForgeDetails::new(forge_name, forge_type, hostname, user, token.clone()); let result = forge_details.token(); assert_eq!(result.expose_secret(), token.expose_secret()); } #[test] fn with_hostname_should_return_new_instance() { let forge_type = ForgeType::MockForge; let hostname = Hostname::new("localhost".to_string()); let user = User::new("bob".to_string()); let token = ApiToken::new("alpha".to_string().into()); let forge_name = ForgeName::new("gamma".to_string()); let forge_details = ForgeDetails::new(forge_name, forge_type, hostname, user, token); let result = forge_details.with_hostname(Hostname::new("remotehost".to_string())); assert_eq!(result.hostname(), &Hostname::new("remotehost".to_string())); } #[test] fn should_convert_from_name_and_config() { let forge_type = ForgeType::MockForge; let hostname = Hostname::new("localhost".to_string()); let user = User::new("bob".to_string()); let token = ApiToken::new("alpha".to_string().into()); let forge_name = ForgeName::new("gamma".to_string()); let forge_config = ForgeConfig::new( forge_type, "localhost".to_string(), "bob".to_string(), "alpha".to_string(), BTreeMap::new(), ); let forge_details = ForgeDetails::from((&forge_name, &forge_config)); assert_eq!(forge_details.forge_name(), &forge_name); assert_eq!(forge_details.hostname(), &hostname); assert_eq!(forge_details.user(), &user); assert_eq!(forge_details.token().expose_secret(), token.expose_secret()); } } mod forge_name { use std::path::PathBuf; use crate::ForgeName; #[test] fn should_convert_to_pathbuf() { let forge_name = ForgeName::new("alpha".to_string()); let pathbuf: PathBuf = (&forge_name).into(); assert_eq!(pathbuf, PathBuf::new().join("alpha")); } } mod forge_type { use crate::ForgeType; #[test] fn should_display_as_lowercase() { assert_eq!(ForgeType::MockForge.to_string(), "mockforge".to_string()); } } mod gitdir { use std::path::PathBuf; use crate::GitDir; #[test] fn should_return_pathbuf() { let pathbuf = PathBuf::default().join("foo"); let gitdir = GitDir::new(&pathbuf); let result = gitdir.pathbuf(); assert_eq!(result, &pathbuf); } #[test] fn should_display() { let pathbuf = PathBuf::default().join("foo"); let gitdir = GitDir::new(&pathbuf); let result = gitdir.to_string(); assert_eq!(result, "foo"); } #[test] fn should_convert_from_str() { let pathbuf = PathBuf::default().join("foo"); let gitdir: GitDir = "foo".into(); assert_eq!(gitdir, GitDir::new(&pathbuf)); } #[test] fn should_convert_to_pathbuf_from_ref() { let pathbuf = PathBuf::default().join("foo"); let gitdir: GitDir = "foo".into(); let result: PathBuf = (&gitdir).into(); assert_eq!(result, pathbuf); } #[test] fn should_convert_to_pathbuf_from_inst() { let pathbuf = PathBuf::default().join("foo"); let gitdir: GitDir = "foo".into(); let result: PathBuf = gitdir.into(); assert_eq!(result, pathbuf); } } mod repo_branches { use crate::{BranchName, RepoBranches}; #[test] fn should_return_main() { let repo_branches = RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()); assert_eq!(repo_branches.main(), BranchName::new("main")); } #[test] fn should_return_next() { let repo_branches = RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()); assert_eq!(repo_branches.next(), BranchName::new("next")); } #[test] fn should_return_dev() { let repo_branches = RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()); assert_eq!(repo_branches.dev(), BranchName::new("dev")); } } mod server { mod load { // use std::collections::{BTreeMap, HashMap}; use assert2::let_assert; use crate::{ server::{Http, ServerConfig, ServerStorage, Webhook}, tests::TestResult, ForgeConfig, ForgeType, RepoBranches, RepoConfig, RepoConfigSource, ServerRepoConfig, }; #[test] fn load_should_parse_server_config() -> TestResult { let fs = kxio::fs::temp()?; fs.file_write( &fs.base().join("git-next-server.toml"), r#" [http] addr = "0.0.0.0" port = 8080 [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" "#, ) ?; let_assert!(Ok(config) = ServerConfig::load(&fs)); let expected = ServerConfig::new( Http::new("0.0.0.0".to_string(), 8080), Webhook::new("http://localhost:9909/webhook".to_string()), ServerStorage::new("/opt/git-next/data".into()), HashMap::from([( "default".to_string(), ForgeConfig::new( ForgeType::MockForge, "git.example.net".to_string(), "Bob".to_string(), "API-Token".to_string(), BTreeMap::from([ ( "hello".to_string(), ServerRepoConfig::new( "user/hello".to_string(), "main".to_string(), Some("/opt/git/user/hello.git".into()), None, None, None, ), ), ( "world".to_string(), ServerRepoConfig::new( "user/world".to_string(), "master".to_string(), None, Some("main".to_string()), Some("next".to_string()), Some("dev".to_string()), ), ), ( "sam".to_string(), ServerRepoConfig::new( "user/sam".to_string(), "main".to_string(), None, Some("master".to_string()), Some("upcoming".to_string()), Some("sam-dev".to_string()), ), ), ]), ), )]), ); assert_eq!(config, expected, "ServerConfig"); if let Some(forge) = config.forge.get("world") { if let Some(repo) = forge.get_repo("sam") { let repo_config = repo.repo_config(); let expected = Some(RepoConfig::new( RepoBranches::new( "master".to_string(), "upcoming".to_string(), "sam-dev".to_string(), ), RepoConfigSource::Server, )); assert_eq!(repo_config, expected, "RepoConfig"); } } Ok(()) } } }