Skip to content

Commit 4d2b9af

Browse files
committed
Handle workflow runs API mock in tests
1 parent 3dab86d commit 4d2b9af

File tree

13 files changed

+304
-346
lines changed

13 files changed

+304
-346
lines changed

src/bors/comment.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ fn list_workflows_status(workflows: &[WorkflowModel]) -> String {
196196
"- [{}]({}) {}",
197197
w.name,
198198
w.url,
199-
if w.status == WorkflowStatus::Success {
200-
":white_check_mark:"
201-
} else {
202-
":x:"
199+
match w.status {
200+
WorkflowStatus::Success => ":white_check_mark:",
201+
WorkflowStatus::Failure => ":x:",
202+
WorkflowStatus::Pending => ":question:",
203203
}
204204
)
205205
})

src/bors/handlers/info.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(super) async fn command_info(
6060

6161
#[cfg(test)]
6262
mod tests {
63-
use crate::tests::mocks::{Workflow, WorkflowEvent, run_test};
63+
use crate::tests::mocks::{WorkflowEvent, WorkflowRunData, run_test};
6464

6565
#[sqlx::test]
6666
async fn info_for_unapproved_pr(pool: sqlx::PgPool) {
@@ -156,7 +156,9 @@ mod tests {
156156
tester.expect_comments(1).await;
157157

158158
tester
159-
.workflow_event(WorkflowEvent::started(Workflow::from(tester.try_branch())))
159+
.workflow_event(WorkflowEvent::started(WorkflowRunData::from(
160+
tester.try_branch(),
161+
)))
160162
.await?;
161163

162164
tester.post_comment("@bors info").await?;

src/bors/handlers/refresh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ timeout = 3600
304304
.run_test(async |tester| {
305305
tester.post_comment("@bors try").await?;
306306
tester.expect_comments(1).await;
307-
tester.start_workflow(tester.try_branch()).await?;
307+
tester.workflow_start(tester.try_branch()).await?;
308308

309309
with_mocked_time(Duration::from_secs(4000), async {
310310
tester.cancel_timed_out_builds().await;

src/bors/handlers/trybuild.rs

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -370,21 +370,22 @@ mod tests {
370370
use crate::bors::handlers::trybuild::{
371371
TRY_BRANCH_NAME, TRY_BUILD_CHECK_RUN_NAME, TRY_MERGE_BRANCH_NAME,
372372
};
373+
use crate::database::WorkflowStatus;
373374
use crate::database::operations::get_all_workflows;
374375
use crate::github::CommitSha;
376+
use crate::tests::BorsTester;
375377
use crate::tests::mocks::{
376-
BorsBuilder, Comment, GitHubState, User, Workflow, WorkflowEvent, default_pr_number,
378+
BorsBuilder, Comment, GitHubState, User, WorkflowEvent, WorkflowRunData, default_pr_number,
377379
default_repo_name, run_test,
378380
};
379381
use octocrab::params::checks::{CheckRunConclusion, CheckRunStatus};
380382

381383
#[sqlx::test]
382384
async fn try_success(pool: sqlx::PgPool) {
383-
run_test(pool, async |tester| {
384-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
385+
run_test(pool.clone(), async |tester| {
385386
tester.post_comment("@bors try").await?;
386387
tester.expect_comments(1).await;
387-
tester.workflow_success(tester.try_branch()).await?;
388+
tester.workflow_full_success(tester.try_branch()).await?;
388389
insta::assert_snapshot!(
389390
tester.get_comment().await?,
390391
@r#"
@@ -402,10 +403,9 @@ mod tests {
402403
#[sqlx::test]
403404
async fn try_failure(pool: sqlx::PgPool) {
404405
run_test(pool, async |tester| {
405-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
406406
tester.post_comment("@bors try").await?;
407407
tester.expect_comments(1).await;
408-
tester.workflow_failure(tester.try_branch()).await?;
408+
tester.workflow_full_failure(tester.try_branch()).await?;
409409
insta::assert_snapshot!(
410410
tester.get_comment().await?,
411411
@r###"
@@ -516,7 +516,7 @@ mod tests {
516516
.post_comment("@bors try parent=ea9c1b050cc8b420c2c211d2177811e564a4dc60")
517517
.await?;
518518
tester.expect_comments(1).await;
519-
tester.workflow_success(tester.try_branch()).await?;
519+
tester.workflow_full_success(tester.try_branch()).await?;
520520
tester.expect_comments(1).await;
521521
tester.post_comment("@bors try parent=last").await?;
522522
insta::assert_snapshot!(tester.get_comment().await?, @r"
@@ -660,7 +660,7 @@ mod tests {
660660
tester.post_comment("@bors try").await?;
661661
tester.expect_comments(1).await;
662662
tester
663-
.start_workflow(Workflow::from(tester.try_branch()).with_run_id(123))
663+
.workflow_start(WorkflowRunData::from(tester.try_branch()).with_run_id(123))
664664
.await?;
665665

666666
tester.post_comment("@bors try").await?;
@@ -683,19 +683,18 @@ mod tests {
683683
tester.post_comment("@bors try").await?;
684684
tester.expect_comments(1).await;
685685
tester
686-
.workflow_success(Workflow::from(tester.try_branch()).with_run_id(1))
686+
.workflow_full_success(WorkflowRunData::from(tester.try_branch()).with_run_id(1))
687687
.await?;
688688
tester.expect_comments(1).await;
689689

690-
tester.get_branch_mut(TRY_BRANCH_NAME).reset_suites();
691690
tester.post_comment("@bors try").await?;
692691
insta::assert_snapshot!(tester.get_comment().await?, @r"
693692
:hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-1…
694693
695694
To cancel the try build, run the command `@bors try cancel`.
696695
");
697696
tester
698-
.workflow_success(Workflow::from(tester.try_branch()).with_run_id(2))
697+
.workflow_full_success(WorkflowRunData::from(tester.try_branch()).with_run_id(2).with_check_suite_id(2))
699698
.await?;
700699
insta::assert_snapshot!(tester.get_comment().await?, @r#"
701700
:sunny: Try build successful ([Workflow1](https://github.com/workflows/Workflow1/2))
@@ -727,12 +726,12 @@ mod tests {
727726
let branch = tester.try_branch();
728727
tester
729728
.workflow_event(WorkflowEvent::started(
730-
Workflow::from(branch.clone()).with_run_id(123),
729+
WorkflowRunData::from(branch.clone()).with_run_id(123),
731730
))
732731
.await?;
733732
tester
734733
.workflow_event(WorkflowEvent::started(
735-
Workflow::from(branch.clone()).with_run_id(124),
734+
WorkflowRunData::from(branch.clone()).with_run_id(124),
736735
))
737736
.await?;
738737
tester.post_comment("@bors try cancel").await?;
@@ -780,45 +779,33 @@ mod tests {
780779

781780
#[sqlx::test]
782781
async fn try_cancel_ignore_finished_workflows(pool: sqlx::PgPool) {
783-
let gh = run_test(pool, async |tester| {
784-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(3);
782+
let gh = run_test(pool, async |tester: &mut BorsTester| {
785783
tester.post_comment("@bors try").await?;
786784
tester.expect_comments(1).await;
787-
788785
let branch = tester.try_branch();
789-
tester
790-
.workflow_success(Workflow::from(branch.clone()).with_run_id(1))
791-
.await?;
792-
tester
793-
.workflow_failure(Workflow::from(branch.clone()).with_run_id(2))
794-
.await?;
795-
tester
796-
.workflow_event(WorkflowEvent::started(
797-
Workflow::from(branch).with_run_id(3),
798-
))
799-
.await?;
800-
tester.post_comment("@bors try cancel").await?;
801-
tester.expect_comments(1).await;
802-
Ok(())
803-
})
804-
.await;
805-
gh.check_cancelled_workflows(default_repo_name(), &[3]);
806-
}
807786

808-
#[sqlx::test]
809-
async fn try_cancel_ignore_external_workflows(pool: sqlx::PgPool) {
810-
let gh = run_test(pool, async |tester| {
811-
tester.post_comment("@bors try").await?;
812-
tester.expect_comments(1).await;
813-
tester
814-
.workflow_success(Workflow::from(tester.try_branch()).make_external())
815-
.await?;
787+
let w1 = WorkflowRunData::from(branch.clone()).with_run_id(1);
788+
let w2 = WorkflowRunData::from(branch.clone()).with_run_id(2);
789+
let w3 = WorkflowRunData::from(branch).with_run_id(3);
790+
for workflow in [&w1, &w2, &w3] {
791+
tester
792+
.default_repo()
793+
.lock()
794+
.update_workflow_run(workflow.clone(), WorkflowStatus::Pending);
795+
}
796+
797+
tester.workflow_full_success(w1).await?;
798+
tester.workflow_full_success(w2).await?;
799+
tester.workflow_start(w3).await?;
816800
tester.post_comment("@bors try cancel").await?;
817-
tester.expect_comments(1).await;
801+
insta::assert_snapshot!(tester.get_comment().await?, @r"
802+
Try build cancelled. Cancelled workflows:
803+
- https://github.com/rust-lang/borstest/actions/runs/3
804+
");
818805
Ok(())
819806
})
820807
.await;
821-
gh.check_cancelled_workflows(default_repo_name(), &[]);
808+
gh.check_cancelled_workflows(default_repo_name(), &[3]);
822809
}
823810

824811
#[sqlx::test]
@@ -883,7 +870,7 @@ try_succeed = ["+foo", "+bar", "-baz"]
883870
repo.lock()
884871
.get_pr(default_pr_number())
885872
.check_added_labels(&[]);
886-
tester.workflow_success(tester.try_branch()).await?;
873+
tester.workflow_full_success(tester.try_branch()).await?;
887874
tester.expect_comments(1).await;
888875
let pr = repo.lock().get_pr(default_pr_number()).clone();
889876
pr.check_added_labels(&["foo", "bar"]);
@@ -903,7 +890,6 @@ try_failed = ["+foo", "+bar", "-baz"]
903890
"#,
904891
))
905892
.run_test(async |tester| {
906-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
907893
tester.post_comment("@bors try").await?;
908894
insta::assert_snapshot!(tester.get_comment().await?, @r"
909895
:hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…
@@ -914,7 +900,7 @@ try_failed = ["+foo", "+bar", "-baz"]
914900
repo.lock()
915901
.get_pr(default_pr_number())
916902
.check_added_labels(&[]);
917-
tester.workflow_failure(tester.try_branch()).await?;
903+
tester.workflow_full_failure(tester.try_branch()).await?;
918904
tester.expect_comments(1).await;
919905
let pr = repo.lock().get_pr(default_pr_number()).clone();
920906
pr.check_added_labels(&["foo", "bar"]);
@@ -927,7 +913,6 @@ try_failed = ["+foo", "+bar", "-baz"]
927913
#[sqlx::test]
928914
async fn try_build_creates_check_run(pool: sqlx::PgPool) {
929915
run_test(pool, async |tester| {
930-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
931916
tester.post_comment("@bors try").await?;
932917
tester.expect_comments(1).await;
933918

@@ -947,11 +932,10 @@ try_failed = ["+foo", "+bar", "-baz"]
947932
#[sqlx::test]
948933
async fn try_build_updates_check_run_on_success(pool: sqlx::PgPool) {
949934
run_test(pool, async |tester| {
950-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
951935
tester.post_comment("@bors try").await?;
952936
tester.expect_comments(1).await;
953937

954-
tester.workflow_success(tester.try_branch()).await?;
938+
tester.workflow_full_success(tester.try_branch()).await?;
955939
tester.expect_comments(1).await;
956940

957941
tester.expect_check_run(
@@ -970,11 +954,10 @@ try_failed = ["+foo", "+bar", "-baz"]
970954
#[sqlx::test]
971955
async fn try_build_updates_check_run_on_failure(pool: sqlx::PgPool) {
972956
run_test(pool, async |tester| {
973-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
974957
tester.post_comment("@bors try").await?;
975958
tester.expect_comments(1).await;
976959

977-
tester.workflow_failure(tester.try_branch()).await?;
960+
tester.workflow_full_failure(tester.try_branch()).await?;
978961
tester.expect_comments(1).await;
979962

980963
tester.expect_check_run(
@@ -993,7 +976,6 @@ try_failed = ["+foo", "+bar", "-baz"]
993976
#[sqlx::test]
994977
async fn try_cancel_updates_check_run_to_cancelled(pool: sqlx::PgPool) {
995978
run_test(pool, async |tester| {
996-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
997979
tester.post_comment("@bors try").await?;
998980
tester.expect_comments(1).await;
999981

@@ -1016,7 +998,6 @@ try_failed = ["+foo", "+bar", "-baz"]
1016998
#[sqlx::test]
1017999
async fn new_try_build_cancels_previous_and_updates_check_run(pool: sqlx::PgPool) {
10181000
run_test(pool, async |tester| {
1019-
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
10201001
tester.post_comment("@bors try").await?;
10211002
tester.expect_comments(1).await;
10221003

0 commit comments

Comments
 (0)