Skip to content

Commit cf5c3bf

Browse files
committed
Add simple test
1 parent c0228b8 commit cf5c3bf

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/bors/handlers/workflow.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ async fn try_complete_build(
167167
return Ok(());
168168
}
169169

170-
let has_failure = checks
171-
.iter()
172-
.any(|check| matches!(check.status, CheckSuiteStatus::Failure));
173-
174170
let mut workflows = db.get_workflows_for_build(&build).await?;
175171
workflows.sort_by(|a, b| a.name.cmp(&b.name));
176172

@@ -187,7 +183,7 @@ async fn try_complete_build(
187183
}
188184

189185
let workflow_list = workflows
190-
.into_iter()
186+
.iter()
191187
.map(|w| {
192188
format!(
193189
"- [{}]({}) {}",
@@ -203,6 +199,10 @@ async fn try_complete_build(
203199
.collect::<Vec<_>>()
204200
.join("\n");
205201

202+
let has_failure = workflows
203+
.iter()
204+
.any(|workflow| matches!(workflow.status, WorkflowStatus::Failure));
205+
206206
let (status, trigger) = if has_failure {
207207
(BuildStatus::Failure, LabelTrigger::TryBuildFailed)
208208
} else {
@@ -234,7 +234,12 @@ mod tests {
234234
use crate::bors::handlers::trybuild::TRY_BRANCH_NAME;
235235
use crate::database::operations::get_all_workflows;
236236
use crate::database::WorkflowStatus;
237-
use crate::tests::mocks::{run_test, Branch, CheckSuite, Workflow, WorkflowEvent};
237+
use crate::tests::mocks::{
238+
run_test, BorsBuilder, Branch, CheckSuite, TestWorkflowStatus, Workflow, WorkflowEvent,
239+
World,
240+
};
241+
use chrono::Utc;
242+
use std::time::Duration;
238243

239244
#[sqlx::test]
240245
async fn workflow_started_unknown_build(pool: sqlx::PgPool) {
@@ -414,4 +419,39 @@ mod tests {
414419
})
415420
.await;
416421
}
422+
423+
#[sqlx::test]
424+
async fn workflow_finished_too_quickly(pool: sqlx::PgPool) {
425+
let world = World::default();
426+
world.default_repo().lock().set_config(
427+
r#"
428+
min_ci_time = 10
429+
"#,
430+
);
431+
BorsBuilder::new(pool)
432+
.world(world)
433+
.run_test(|mut tester| async {
434+
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
435+
tester.post_comment("@bors try").await?;
436+
tester.expect_comments(1).await;
437+
438+
tester
439+
.workflow_full(
440+
Workflow::from(tester.try_branch())
441+
.with_end(Utc::now() + Duration::from_secs(1)),
442+
TestWorkflowStatus::Success,
443+
)
444+
.await?;
445+
tester
446+
.check_suite(CheckSuite::completed(tester.try_branch()))
447+
.await?;
448+
449+
insta::assert_snapshot!(tester.get_comment().await?, @r"
450+
:broken_heart: Test failed
451+
- [Workflow1](https://github.com/workflows/Workflow1/1) :x:
452+
");
453+
Ok(tester)
454+
})
455+
.await;
456+
}
417457
}

src/tests/mocks/workflow.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use chrono::{DateTime, Utc};
22
use octocrab::models::{CheckRunId, RunId, WorkflowId};
33
use serde::Serialize;
4+
use std::time::Duration;
45
use url::Url;
56

67
use crate::github::GithubRepoName;
@@ -93,6 +94,8 @@ pub struct Workflow {
9394
pub head_branch: String,
9495
head_sha: String,
9596
pub external: bool,
97+
start: DateTime<Utc>,
98+
end: DateTime<Utc>,
9699
}
97100

98101
impl Workflow {
@@ -103,6 +106,9 @@ impl Workflow {
103106
self.external = true;
104107
self
105108
}
109+
pub fn with_end(self, end: DateTime<Utc>) -> Self {
110+
Self { end, ..self }
111+
}
106112

107113
fn new(branch: Branch) -> Self {
108114
Self {
@@ -112,6 +118,8 @@ impl Workflow {
112118
head_branch: branch.get_name().to_string(),
113119
head_sha: branch.get_sha().to_string(),
114120
external: false,
121+
start: Utc::now(),
122+
end: Utc::now() + Duration::from_secs(60),
115123
}
116124
}
117125
}
@@ -166,8 +174,8 @@ impl From<WorkflowEvent> for GitHubWorkflowEventPayload {
166174
WorkflowEventKind::Started => None,
167175
WorkflowEventKind::Completed { status } => Some(status),
168176
},
169-
created_at: Default::default(),
170-
updated_at: Default::default(),
177+
created_at: workflow.start,
178+
updated_at: workflow.end,
171179
url: url.clone(),
172180
html_url: url.clone(),
173181
jobs_url: url.clone(),

0 commit comments

Comments
 (0)