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

228 lines
5.8 KiB
Rust
Raw Normal View History

//
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, repo_details.remote_url()),
(Direction::Fetch, repo_details.remote_url()),
]),
);
}
pub fn has_remote_defaults(
open_repository: &mut MockOpenRepositoryLike,
remotes: HashMap<Direction, Option<RemoteUrl>>,
) {
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) -> WebhookUrl {
InboundWebhook::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, StoragePathType::Internal)
}
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(), 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) -> InboundWebhook {
InboundWebhook::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 RepositoryFactory>,
forge: Box<dyn git::ForgeLike>,
net: kxio::network::Network,
) -> (crate::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();
2024-06-27 21:10:41 +01:00
let log = RepoActorLog::default();
let actors_log = log.clone();
(
crate::RepoActor::new(
repo_details,
forge,
webhook,
generation,
net,
repository_factory,
std::time::Duration::from_nanos(1),
None,
)
.with_log(actors_log),
log,
)
}
pub fn a_hostname() -> Hostname {
Hostname::new(given::a_name())
}
pub fn a_registered_webhook() -> RegisteredWebhook {
RegisteredWebhook::new(given::a_webhook_id(), given::a_webhook_auth())
}
2024-06-27 21:10:41 +01:00
pub fn a_push() -> webhook::Push {
webhook::Push::new(
2024-06-27 21:10:41 +01:00
given::a_branch_name("push"),
given::a_name(),
given::a_name(),
)
}