refactor: CloneRepo use actor::do_send to send LoadConfigFromRepo
All checks were successful
Rust / build (push) Successful in 2m12s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-06-30 07:41:13 +01:00
parent f038ab508b
commit c571e9ee8d
6 changed files with 31 additions and 97 deletions

View file

@ -1,21 +1,22 @@
```mermaid ```mermaid
stateDiagram-v2 stateDiagram-v2
[*] --> CloneRepo :Start <!-- [*] --> CloneRepo :Start -->
CloneRepo --> LoadConfigFromRepo CloneRepo --> LoadConfigFromRepo :on repo config
CloneRepo --> ValidateRepo CloneRepo --> ValidateRepo :on server config
LoadConfigFromRepo --> ReceiveRepoConfig LoadConfigFromRepo --> ReceiveRepoConfig
ValidateRepo --> CheckCIStatus ValidateRepo --> CheckCIStatus :on next ahead of main
ValidateRepo --> AdvanceNext ValidateRepo --> AdvanceNext :on dev ahead of next
ValidateRepo --> ValidateRepo :invalid ValidateRepo --> [*] :on dev == next == main
ValidateRepo --> ValidateRepo :on invalid
CheckCIStatus --> ReceiveCIStatus CheckCIStatus --> ReceiveCIStatus
ReceiveCIStatus --> AdvanceMain :Pass ReceiveCIStatus --> AdvanceMain :on Pass
ReceiveCIStatus --> ValidateRepo :Pending ReceiveCIStatus --> ValidateRepo :on Pending
ReceiveCIStatus --> [*] :Fail ReceiveCIStatus --> [*] :on Fail
AdvanceNext --> ValidateRepo AdvanceNext --> ValidateRepo
@ -26,10 +27,10 @@ RegisterWebhook --> WebhookRegistered
WebhookRegistered --> [*] WebhookRegistered --> [*]
AdvanceMain --> LoadConfigFromRepo :on repo config - reload AdvanceMain --> LoadConfigFromRepo :on repo config
AdvanceMain --> ValidateRepo :on server config AdvanceMain --> ValidateRepo :on server config
[*] --> WebhookNotification :WEBHOOK [*] --> WebhookNotification :on push
WebhookNotification --> ValidateRepo WebhookNotification --> ValidateRepo
``` ```

View file

@ -12,6 +12,7 @@ impl Handler<actor::messages::CloneRepo> for actor::RepoActor {
_msg: actor::messages::CloneRepo, _msg: actor::messages::CloneRepo,
ctx: &mut Self::Context, ctx: &mut Self::Context,
) -> Self::Result { ) -> Self::Result {
actor::logger(&self.log, "Handler: CloneRepo: start");
tracing::debug!("Handler: CloneRepo: start"); tracing::debug!("Handler: CloneRepo: start");
let gitdir = self.repo_details.gitdir.clone(); let gitdir = self.repo_details.gitdir.clone();
match git::repository::open(&*self.repository_factory, &self.repo_details, gitdir) { match git::repository::open(&*self.repository_factory, &self.repo_details, gitdir) {
@ -20,18 +21,17 @@ impl Handler<actor::messages::CloneRepo> for actor::RepoActor {
tracing::debug!("open okay"); tracing::debug!("open okay");
self.open_repository.replace(repository); self.open_repository.replace(repository);
if self.repo_details.repo_config.is_none() { if self.repo_details.repo_config.is_none() {
tracing::debug!("Handler: CloneRepo: Sending: LoadConfigFromRepo"); actor::do_send(
actor::logger(&self.log, "send: LoadConfigFromRepo"); ctx.address(),
ctx.address().do_send(actor::messages::LoadConfigFromRepo); actor::messages::LoadConfigFromRepo,
&self.log,
);
} else { } else {
tracing::debug!("Handler: CloneRepo: Sending: ValidateRepo"); actor::do_send(
actor::logger(&self.log, "send: ValidateRepo"); ctx.address(),
if let Err(_e) = ctx actor::messages::ValidateRepo::new(self.message_token),
.address() &self.log,
.try_send(actor::messages::ValidateRepo::new(self.message_token)) );
{
actor::logger(&self.log, format!("ValidateRepo: error: {_e:?}"));
}
} }
} }
Err(err) => { Err(err) => {

View file

@ -26,13 +26,6 @@ pub fn push(open_repository: &mut MockOpenRepositoryLike, result: Result<(), git
.return_once(move |_, _, _, _| result); .return_once(move |_, _, _, _| result);
} }
pub fn duplicate(open_repository: &mut MockOpenRepositoryLike, result: MockOpenRepositoryLike) {
open_repository
.expect_duplicate()
.times(1)
.return_once(move || Box::new(result));
}
pub fn open_repository( pub fn open_repository(
repository_factory: &mut MockRepositoryFactory, repository_factory: &mut MockRepositoryFactory,
open_repository: MockOpenRepositoryLike, open_repository: MockOpenRepositoryLike,

View file

@ -26,46 +26,6 @@ pub fn has_remote_defaults(
}); });
} }
#[allow(clippy::type_complexity)]
pub fn open_repository_for_loading_config_from_repo(
repo_config: &RepoConfig,
) -> (
MockOpenRepositoryLike,
Arc<Mutex<Vec<(BranchName, PathBuf)>>>,
) {
let mut load_config_from_repo_open_repository = MockOpenRepositoryLike::new();
let read_files = Arc::new(Mutex::new(vec![]));
let read_files_ref = read_files.clone();
let branches = repo_config.branches().clone();
load_config_from_repo_open_repository
.expect_read_file()
.return_once(move |branch_name, file_name| {
let branch_name = branch_name.clone();
let file_name = file_name.to_path_buf();
let _ = read_files_ref
.lock()
.map(move |mut l| l.push((branch_name, file_name)));
let contents = format!(
r#"
[branches]
main = "{}"
next = "{}"
dev = "{}"
"#,
branches.main(),
branches.next(),
branches.dev()
);
Ok(contents)
});
let branches = repo_config.branches().clone();
let remote_branches = vec![branches.main(), branches.next(), branches.dev()];
load_config_from_repo_open_repository
.expect_remote_branches()
.return_once(move || Ok(remote_branches));
(load_config_from_repo_open_repository, read_files)
}
pub fn a_webhook_auth() -> WebhookAuth { pub fn a_webhook_auth() -> WebhookAuth {
WebhookAuth::generate() WebhookAuth::generate()
} }

View file

@ -78,42 +78,29 @@ async fn should_open() -> TestResult {
} }
/// The server config can optionally include the names of the main, next and dev /// The server config can optionally include the names of the main, next and dev
/// branches. When it doesn't we should load the .git-next.yaml from from the /// branches. When it doesn't we should load the `.git-next.yaml` from from the
/// repo and get the branch names from there. /// repo and get the branch names from there by sending a [LoadConfigFromRepo] message.
#[actix::test] #[actix::test]
async fn when_server_has_no_repo_config_load_from_repo_and_validate() -> TestResult { async fn when_server_has_no_repo_config_should_send_load_from_repo() -> TestResult {
//given //given
let fs = given::a_filesystem(); let fs = given::a_filesystem();
let (mut open_repository, mut repo_details) = given::an_open_repository(&fs); let (mut open_repository, mut repo_details) = given::an_open_repository(&fs);
#[allow(clippy::unwrap_used)] #[allow(clippy::unwrap_used)]
let repo_config = repo_details.repo_config.take().unwrap(); let _repo_config = repo_details.repo_config.take().unwrap();
given::has_all_valid_remote_defaults(&mut open_repository, &repo_details); given::has_all_valid_remote_defaults(&mut open_repository, &repo_details);
// load config from repo
let (load_config_from_repo_open_repository, read_files) =
given::open_repository_for_loading_config_from_repo(&repo_config);
expect::duplicate(&mut open_repository, load_config_from_repo_open_repository);
// handles_validate_repo_message(&mut open_repository, repo_config.branches());
let mut repository_factory = MockRepositoryFactory::new(); let mut repository_factory = MockRepositoryFactory::new();
expect::open_repository(&mut repository_factory, open_repository); expect::open_repository(&mut repository_factory, open_repository);
fs.dir_create(&repo_details.gitdir)?; fs.dir_create(&repo_details.gitdir)?;
let branch = repo_details.branch.clone();
//when //when
let (addr, _log) = when::start_actor(repository_factory, repo_details, given::a_forge()); let (addr, log) = when::start_actor(repository_factory, repo_details, given::a_forge());
addr.send(CloneRepo::new()).await?; addr.send(CloneRepo::new()).await?;
System::current().stop(); System::current().stop();
//then //then
tracing::debug!("{read_files:#?}"); log.require_message_containing("send: LoadConfigFromRepo")?;
let file_name = PathBuf::from(".git-next.toml".to_string());
read_files
.lock()
.map_err(|e| e.to_string())
.map(|files| assert_eq!(files.clone(), vec![(branch, file_name)]))?;
Ok(()) Ok(())
} }
@ -142,10 +129,7 @@ async fn opened_repo_with_no_default_push_should_not_proceed() -> TestResult {
System::current().stop(); System::current().stop();
//then //then
log.lock() log.require_message_containing("open failed")?;
.map_err(|e| e.to_string())
// doesn't log that it sent any messages to load config or validate the repo
.map(|l| assert_eq!(l.clone(), vec!["open failed"]))?;
Ok(()) Ok(())
} }
@ -174,10 +158,7 @@ async fn opened_repo_with_no_default_fetch_should_not_proceed() -> TestResult {
System::current().stop(); System::current().stop();
//then //then
log.lock() log.require_message_containing("open failed")?;
.map_err(|e| e.to_string())
// doesn't log that it sent any messages to load config or validate the repo
.map(|l| assert_eq!(l.clone(), vec!["open failed"]))?;
Ok(()) Ok(())
} }

View file

@ -22,7 +22,6 @@ use git_next_git as git;
use mockall::predicate::eq; use mockall::predicate::eq;
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
path::PathBuf,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };