tests: tidy up config, forgejo and git tests
All checks were successful
ci/woodpecker/cron/cron-docker-builder Pipeline was successful
ci/woodpecker/cron/push-next Pipeline was successful
ci/woodpecker/cron/tag-created Pipeline was successful
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
Rust / build (push) Successful in 1m13s

Coverage for config, forge, forgejo and github is now 100%.
This commit is contained in:
Paul Campbell 2024-06-06 17:45:33 +01:00
parent ea9a858f48
commit 271f4ec1dc
5 changed files with 46 additions and 19 deletions

View file

@ -23,10 +23,6 @@ impl WebhookMessage {
pub const fn body(&self) -> &Body { pub const fn body(&self) -> &Body {
&self.body &self.body
} }
#[deprecated]
pub const fn authorisation(&self) -> &config::WebhookAuth {
todo!()
}
pub fn header(&self, header: &str) -> Option<String> { pub fn header(&self, header: &str) -> Option<String> {
self.headers.get(header).map(|value| value.to_string()) self.headers.get(header).map(|value| value.to_string())
} }

View file

@ -70,8 +70,7 @@ impl git::ForgeLike for ForgeJo {
); );
let result = self.net.get::<CombinedStatus>(request).await; let result = self.net.get::<CombinedStatus>(request).await;
match result { match result {
Ok(response) => { Ok(response) => match response.response_body() {
match response.response_body() {
Some(status) => match status.state { Some(status) => match status.state {
ForgejoState::Success => Status::Pass, ForgejoState::Success => Status::Pass,
ForgejoState::Pending => Status::Pending, ForgejoState::Pending => Status::Pending,
@ -80,11 +79,11 @@ impl git::ForgeLike for ForgeJo {
ForgejoState::Blank => Status::Pending, ForgejoState::Blank => Status::Pending,
}, },
None => { None => {
warn!("No status found for commit"); #[cfg(not(tarpaulin_include))]
Status::Pending // assume issue is transient and allow retry unreachable!(); // response.response_body() is always Some when
} // request responseType::Json
}
} }
},
Err(e) => { Err(e) => {
warn!(?e, "Failed to get commit status"); warn!(?e, "Failed to get commit status");
Status::Pending // assume issue is transient and allow retry Status::Pending // assume issue is transient and allow retry

View file

@ -276,6 +276,33 @@ mod forgejo {
let_assert!(Ok(_) = forge.unregister_webhook(&webhook_id).await); let_assert!(Ok(_) = forge.unregister_webhook(&webhook_id).await);
} }
#[tokio::test]
async fn should_return_error_on_network_error() {
let repo_details = given::repo_details();
let hostname = repo_details.forge.hostname();
let repo_path = &repo_details.repo_path;
use secrecy::ExposeSecret;
let token = repo_details.forge.token().expose_secret();
let webhook_id = given::a_webhook_id();
let mut net = given::a_network();
net.add_delete_error(
format!(
"https://{hostname}/api/v1/repos/{repo_path}/hooks/{webhook_id}?token={token}"
)
.as_str(),
"error",
);
let forge = given::a_forgejo_forge(&repo_details, net);
let_assert!(Err(err) = forge.unregister_webhook(&webhook_id).await);
assert!(
matches!(err, git::forge::webhook::Error::FailedToUnregister(_)),
"{err:?}"
);
}
} }
mod register_webhook { mod register_webhook {

View file

@ -28,7 +28,9 @@ pub async fn unregister(
let result = net.delete(request).await; let result = net.delete(request).await;
if let Err(e) = result { if let Err(e) = result {
tracing::warn!("Failed to unregister webhook"); tracing::warn!("Failed to unregister webhook");
return Err(git::forge::webhook::Error::FailedToRegister(e.to_string())); return Err(git::forge::webhook::Error::FailedToUnregister(
e.to_string(),
));
} }
Ok(()) Ok(())
} }

View file

@ -9,6 +9,9 @@ pub enum Error {
#[error("failed to register: {0}")] #[error("failed to register: {0}")]
FailedToRegister(String), FailedToRegister(String),
#[error("failed to unregister: {0}")]
FailedToUnregister(String),
#[error("network response was empty")] #[error("network response was empty")]
NetworkResponseEmpty, NetworkResponseEmpty,