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

455 lines
14 KiB
Rust
Raw Normal View History

2024-05-14 07:59:31 +01:00
type TestResult = Result<(), Box<dyn std::error::Error>>;
mod server_repo_config {
2024-05-17 20:19:47 +01:00
use std::path::PathBuf;
2024-05-14 07:59:31 +01:00
use assert2::let_assert;
2024-05-17 20:19:47 +01:00
use crate::{BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource, RepoPath};
2024-05-14 07:59:31 +01:00
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);
2024-05-14 07:59:31 +01:00
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()),
);
2024-05-14 07:59:31 +01:00
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
)
2024-05-14 07:59:31 +01:00
);
}
2024-05-17 20:19:47 +01:00
#[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")))
);
}
2024-05-14 07:59:31 +01:00
}
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
)
2024-05-14 07:59:31 +01:00
);
Ok(())
}
2024-05-17 20:19:47 +01:00
#[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);
}
2024-05-14 07:59:31 +01:00
}
mod forge_config {
use std::collections::BTreeMap;
2024-05-17 20:19:47 +01:00
use secrecy::ExposeSecret;
use crate::{ForgeConfig, ForgeType, Hostname, RepoAlias, ServerRepoConfig, User};
2024-05-14 07:59:31 +01:00
#[test]
2024-05-17 20:19:47 +01:00
fn should_return_repos() {
2024-05-14 07:59:31 +01:00
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,
);
2024-05-14 07:59:31 +01:00
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);
2024-05-14 07:59:31 +01:00
let returned_repos = fc.repos().collect::<Vec<_>>();
assert_eq!(
returned_repos,
vec![
// alphabetical order by key
(RepoAlias::new("blue"), &blue),
(RepoAlias::new("red"), &red),
]
);
2024-05-17 20:19:47 +01:00
}
#[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"));
2024-05-14 07:59:31 +01:00
}
}