git-next/crates/repo-actor/src/tests/given.rs

229 lines
5.9 KiB
Rust

//
use super::*;
pub fn has_all_valid_remote_defaults(
open_repository: &mut MockOpenRepositoryLike,
repo_details: &RepoDetails,
) {
has_remote_defaults(
open_repository,
HashMap::from([
(Direction::Push, Some(repo_details.git_remote())),
(Direction::Fetch, Some(repo_details.git_remote())),
]),
);
}
pub fn has_remote_defaults(
open_repository: &mut MockOpenRepositoryLike,
remotes: HashMap<Direction, Option<GitRemote>>,
) {
remotes.into_iter().for_each(|(direction, remote)| {
open_repository
.expect_find_default_remote()
.with(eq(direction))
.return_once(|_| remote);
});
}
pub fn a_webhook_auth() -> WebhookAuth {
WebhookAuth::generate()
}
pub fn repo_branches() -> RepoBranches {
RepoBranches::new(
format!("main-{}", a_name()),
format!("next-{}", a_name()),
format!("dev-{}", a_name()),
)
}
pub fn a_forge_alias() -> ForgeAlias {
ForgeAlias::new(a_name())
}
pub fn a_repo_alias() -> RepoAlias {
RepoAlias::new(a_name())
}
pub fn a_network() -> kxio::network::MockNetwork {
kxio::network::MockNetwork::new()
}
pub fn a_webhook_url(
forge_alias: &ForgeAlias,
repo_alias: &RepoAlias,
) -> git_next_config::server::WebhookUrl {
config::server::Webhook::new(a_name()).url(forge_alias, repo_alias)
}
pub fn a_name() -> String {
use rand::Rng;
use std::iter;
fn generate(len: usize) -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let mut rng = rand::thread_rng();
let one_char = || CHARSET[rng.gen_range(0..CHARSET.len())] as char;
iter::repeat_with(one_char).take(len).collect()
}
generate(5)
}
pub fn a_webhook_id() -> WebhookId {
WebhookId::new(a_name())
}
pub fn a_branch_name(prefix: impl Into<String>) -> BranchName {
BranchName::new(format!("{}-{}", prefix.into(), a_name()))
}
pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir {
let dir_name = a_name();
let dir = fs.base().join(dir_name);
GitDir::new(dir)
}
pub fn a_forge_config() -> ForgeConfig {
ForgeConfig::new(
ForgeType::MockForge,
a_name(),
a_name(),
a_name(),
Default::default(), // no repos
)
}
pub fn a_server_repo_config() -> ServerRepoConfig {
let main = a_branch_name("main").to_string();
let next = a_branch_name("next").to_string();
let dev = a_branch_name("dev").to_string();
ServerRepoConfig::new(
format!("{}/{}", a_name(), a_name()),
main.clone(),
None,
Some(main),
Some(next),
Some(dev),
)
}
pub fn a_repo_config() -> RepoConfig {
RepoConfig::new(given::repo_branches(), config::RepoConfigSource::Repo)
}
pub fn a_named_commit(name: impl Into<String>) -> git::Commit {
git::Commit::new(a_named_commit_sha(name), a_commit_message())
}
pub fn a_commit() -> git::Commit {
git::Commit::new(a_commit_sha(), a_commit_message())
}
pub fn a_commit_with_message(message: impl Into<git::commit::Message>) -> git::Commit {
git::Commit::new(a_commit_sha(), message.into())
}
pub fn a_commit_message() -> git::commit::Message {
git::commit::Message::new(a_name())
}
pub fn a_named_commit_sha(name: impl Into<String>) -> git::commit::Sha {
git::commit::Sha::new(format!("{}-{}", name.into(), a_name()))
}
pub fn a_commit_sha() -> git::commit::Sha {
git::commit::Sha::new(a_name())
}
pub fn a_filesystem() -> kxio::fs::FileSystem {
kxio::fs::temp().unwrap_or_else(|e| panic!("{}", e))
}
pub fn repo_details(fs: &kxio::fs::FileSystem) -> git::RepoDetails {
let generation = git::Generation::default();
let repo_alias = a_repo_alias();
let server_repo_config = a_server_repo_config();
let forge_alias = a_forge_alias();
let forge_config = a_forge_config();
let gitdir = a_git_dir(fs);
RepoDetails::new(
generation,
&repo_alias,
&server_repo_config,
&forge_alias,
&forge_config,
gitdir,
)
}
pub fn an_open_repository(
fs: &kxio::fs::FileSystem,
) -> (
git::repository::open::MockOpenRepositoryLike,
git::RepoDetails,
) {
let open_repository = MockOpenRepositoryLike::new();
let gitdir = given::a_git_dir(fs);
let hostname = given::a_hostname();
let repo_details = given::repo_details(fs)
.with_gitdir(gitdir)
.with_hostname(hostname);
(open_repository, repo_details)
}
pub fn a_message_token() -> MessageToken {
MessageToken::default()
}
pub fn a_webhook(url: &WebhookUrl) -> Webhook {
Webhook::new(url.clone().into())
}
pub fn a_forge() -> Box<MockForgeLike> {
Box::new(MockForgeLike::new())
}
pub fn a_repo_actor(
repo_details: git::RepoDetails,
repository_factory: Box<dyn git::repository::RepositoryFactory>,
forge: Box<dyn git::ForgeLike>,
net: kxio::network::Network,
) -> (actor::RepoActor, RepoActorLog) {
let forge_alias = repo_details.forge.forge_alias();
let repo_alias = &repo_details.repo_alias;
let webhook_url = given::a_webhook_url(forge_alias, repo_alias);
let webhook = given::a_webhook(&webhook_url);
let generation = Generation::default();
let log = RepoActorLog::default();
let actors_log = log.clone();
(
actor::RepoActor::new(
repo_details,
forge,
webhook,
generation,
net,
repository_factory,
std::time::Duration::from_nanos(1),
)
.with_log(actors_log),
log,
)
}
pub fn a_hostname() -> config::Hostname {
config::Hostname::new(given::a_name())
}
pub fn a_registered_webhook() -> RegisteredWebhook {
RegisteredWebhook::new(given::a_webhook_id(), given::a_webhook_auth())
}
pub fn a_push() -> config::webhook::Push {
config::webhook::Push::new(
given::a_branch_name("push"),
given::a_name(),
given::a_name(),
)
}