Skip to content

Commit 12a21d6

Browse files
committed
Small cleanup
1 parent b210ad4 commit 12a21d6

File tree

6 files changed

+51
-66
lines changed

6 files changed

+51
-66
lines changed

src/bors/handlers/refresh.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,17 @@ mod tests {
126126
.await;
127127
}
128128

129-
#[sqlx::test]
130-
async fn refresh_do_nothing_before_timeout(pool: sqlx::PgPool) {
131-
let gh = GitHubState::default();
132-
gh.default_repo().lock().set_config(
129+
fn gh_state_with_long_timeout() -> GitHubState {
130+
GitHubState::default().with_default_config(
133131
r#"
134132
timeout = 3600
135133
"#,
136-
);
134+
)
135+
}
136+
#[sqlx::test]
137+
async fn refresh_do_nothing_before_timeout(pool: sqlx::PgPool) {
137138
BorsBuilder::new(pool)
138-
.github(gh)
139+
.github(gh_state_with_long_timeout())
139140
.run_test(|mut tester| async move {
140141
tester.post_comment("@bors try").await?;
141142
tester.expect_comments(1).await;
@@ -150,14 +151,8 @@ timeout = 3600
150151

151152
#[sqlx::test]
152153
async fn refresh_cancel_build_after_timeout(pool: sqlx::PgPool) {
153-
let gh = GitHubState::default();
154-
gh.default_repo().lock().set_config(
155-
r#"
156-
timeout = 3600
157-
"#,
158-
);
159154
BorsBuilder::new(pool)
160-
.github(gh)
155+
.github(gh_state_with_long_timeout())
161156
.run_test(|mut tester| async move {
162157
tester.post_comment("@bors try").await?;
163158
tester.expect_comments(1).await;
@@ -191,14 +186,8 @@ timeout = 3600
191186

192187
#[sqlx::test]
193188
async fn refresh_cancel_workflow_after_timeout(pool: sqlx::PgPool) {
194-
let gh = GitHubState::default();
195-
gh.default_repo().lock().set_config(
196-
r#"
197-
timeout = 3600
198-
"#,
199-
);
200189
let gh = BorsBuilder::new(pool)
201-
.github(gh)
190+
.github(gh_state_with_long_timeout())
202191
.run_test(|mut tester| async move {
203192
tester.post_comment("@bors try").await?;
204193
tester.expect_comments(1).await;

src/bors/handlers/review.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ mod tests {
547547
#[sqlx::test]
548548
async fn cannot_approve_without_delegation(pool: sqlx::PgPool) {
549549
BorsBuilder::new(pool)
550-
.github(GitHubState::with_unauthorized_pr_author())
550+
.github(GitHubState::unauthorized_pr_author())
551551
.run_test(|mut tester| async {
552552
tester.post_comment("@bors r+").await?;
553553
insta::assert_snapshot!(
@@ -562,7 +562,7 @@ mod tests {
562562
#[sqlx::test]
563563
async fn delegate_author(pool: sqlx::PgPool) {
564564
BorsBuilder::new(pool)
565-
.github(GitHubState::with_unauthorized_pr_author())
565+
.github(GitHubState::unauthorized_pr_author())
566566
.run_test(|mut tester| async {
567567
tester
568568
.post_comment(review_comment("@bors delegate+"))
@@ -581,7 +581,7 @@ mod tests {
581581
#[sqlx::test]
582582
async fn delegatee_can_approve(pool: sqlx::PgPool) {
583583
BorsBuilder::new(pool)
584-
.github(GitHubState::with_unauthorized_pr_author())
584+
.github(GitHubState::unauthorized_pr_author())
585585
.run_test(|mut tester| async {
586586
tester
587587
.post_comment(review_comment("@bors delegate+"))
@@ -603,7 +603,7 @@ mod tests {
603603
#[sqlx::test]
604604
async fn delegatee_can_try(pool: sqlx::PgPool) {
605605
let gh = BorsBuilder::new(pool)
606-
.github(GitHubState::with_unauthorized_pr_author())
606+
.github(GitHubState::unauthorized_pr_author())
607607
.run_test(|mut tester| async {
608608
tester
609609
.post_comment(review_comment("@bors delegate+"))
@@ -630,7 +630,7 @@ mod tests {
630630
#[sqlx::test]
631631
async fn delegatee_can_set_priority(pool: sqlx::PgPool) {
632632
BorsBuilder::new(pool)
633-
.github(GitHubState::with_unauthorized_pr_author())
633+
.github(GitHubState::unauthorized_pr_author())
634634
.run_test(|mut tester| async {
635635
tester
636636
.post_comment(review_comment("@bors delegate+"))
@@ -655,7 +655,7 @@ mod tests {
655655
#[sqlx::test]
656656
async fn delegate_insufficient_permission(pool: sqlx::PgPool) {
657657
BorsBuilder::new(pool)
658-
.github(GitHubState::with_unauthorized_pr_author())
658+
.github(GitHubState::unauthorized_pr_author())
659659
.run_test(|mut tester| async {
660660
tester.post_comment("@bors delegate+").await?;
661661
insta::assert_snapshot!(
@@ -672,7 +672,7 @@ mod tests {
672672
#[sqlx::test]
673673
async fn undelegate_by_reviewer(pool: sqlx::PgPool) {
674674
BorsBuilder::new(pool)
675-
.github(GitHubState::with_unauthorized_pr_author())
675+
.github(GitHubState::unauthorized_pr_author())
676676
.run_test(|mut tester| async {
677677
tester
678678
.post_comment(review_comment("@bors delegate+"))
@@ -700,7 +700,7 @@ mod tests {
700700
#[sqlx::test]
701701
async fn undelegate_by_delegatee(pool: sqlx::PgPool) {
702702
BorsBuilder::new(pool)
703-
.github(GitHubState::with_unauthorized_pr_author())
703+
.github(GitHubState::unauthorized_pr_author())
704704
.run_test(|mut tester| async {
705705
tester
706706
.post_comment(review_comment("@bors delegate+"))
@@ -725,7 +725,7 @@ mod tests {
725725
#[sqlx::test]
726726
async fn reviewer_unapprove_delegated_approval(pool: sqlx::PgPool) {
727727
BorsBuilder::new(pool)
728-
.github(GitHubState::with_unauthorized_pr_author())
728+
.github(GitHubState::unauthorized_pr_author())
729729
.run_test(|mut tester| async {
730730
tester
731731
.post_comment(review_comment("@bors delegate+"))
@@ -752,7 +752,7 @@ mod tests {
752752
#[sqlx::test]
753753
async fn non_author_cannot_use_delegation(pool: sqlx::PgPool) {
754754
BorsBuilder::new(pool)
755-
.github(GitHubState::with_unauthorized_pr_author())
755+
.github(GitHubState::unauthorized_pr_author())
756756
.run_test(|mut tester| async {
757757
tester
758758
.post_comment(review_comment("@bors delegate+"))

src/bors/handlers/trybuild.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ handled during merge and rebase. This is normal, and you should still perform st
341341
mod tests {
342342
use crate::bors::handlers::trybuild::{TRY_BRANCH_NAME, TRY_MERGE_BRANCH_NAME};
343343
use crate::database::operations::get_all_workflows;
344-
use crate::database::BuildStatus;
345344
use crate::github::CommitSha;
346345
use crate::tests::mocks::{
347346
default_pr_number, default_repo_name, run_test, BorsBuilder, Comment, GitHubState, User,
@@ -394,9 +393,9 @@ mod tests {
394393
tester
395394
.post_comment(Comment::from("@bors try").with_author(User::unprivileged()))
396395
.await?;
397-
assert_eq!(
396+
insta::assert_snapshot!(
398397
tester.get_comment().await?,
399-
"@unprivileged-user: :key: Insufficient privileges: not in try users"
398+
@"@unprivileged-user: :key: Insufficient privileges: not in try users"
400399
);
401400
Ok(tester)
402401
})
@@ -409,9 +408,9 @@ mod tests {
409408
tester
410409
.post_comment(Comment::from("@bors try").with_author(User::try_user()))
411410
.await?;
412-
assert_eq!(
411+
insta::assert_snapshot!(
413412
tester.get_comment().await?,
414-
":hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…"
413+
@":hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…"
415414
);
416415
Ok(tester)
417416
})
@@ -422,9 +421,9 @@ mod tests {
422421
async fn try_merge_comment(pool: sqlx::PgPool) {
423422
run_test(pool, |mut tester| async {
424423
tester.post_comment("@bors try").await?;
425-
assert_eq!(
424+
insta::assert_snapshot!(
426425
tester.get_comment().await?,
427-
":hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…"
426+
@":hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…"
428427
);
429428
Ok(tester)
430429
})
@@ -674,8 +673,8 @@ mod tests {
674673
tester.expect_comments(1).await;
675674
tester.post_comment("@bors try cancel").await?;
676675
tester.expect_comments(1).await;
677-
let pr = tester.default_pr_db().await?.unwrap();
678-
assert_eq!(pr.try_build.unwrap().status, BuildStatus::Cancelled);
676+
677+
tester.default_pr().await.expect_try_build_cancelled();
679678
Ok(tester)
680679
})
681680
.await;
@@ -744,15 +743,11 @@ mod tests {
744743

745744
#[sqlx::test]
746745
async fn try_build_start_modify_labels(pool: sqlx::PgPool) {
747-
let gh = GitHubState::default();
748-
gh.default_repo().lock().set_config(
749-
r#"
746+
BorsBuilder::new(pool)
747+
.github(GitHubState::default().with_default_config(r#"
750748
[labels]
751749
try = ["+foo", "+bar", "-baz"]
752-
"#,
753-
);
754-
BorsBuilder::new(pool)
755-
.github(gh)
750+
"#))
756751
.run_test(|mut tester| async {
757752
tester.post_comment("@bors try").await?;
758753
insta::assert_snapshot!(tester.get_comment().await?, @":hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…");
@@ -767,15 +762,11 @@ try = ["+foo", "+bar", "-baz"]
767762

768763
#[sqlx::test]
769764
async fn try_build_succeeded_modify_labels(pool: sqlx::PgPool) {
770-
let gh = GitHubState::default();
771-
gh.default_repo().lock().set_config(
772-
r#"
765+
BorsBuilder::new(pool)
766+
.github(GitHubState::default().with_default_config(r#"
773767
[labels]
774768
try_succeed = ["+foo", "+bar", "-baz"]
775-
"#,
776-
);
777-
BorsBuilder::new(pool)
778-
.github(gh)
769+
"#))
779770
.run_test(|mut tester| async {
780771
tester.post_comment("@bors try").await?;
781772
insta::assert_snapshot!(tester.get_comment().await?, @":hourglass: Trying commit pr-1-sha with merge merge-main-sha1-pr-1-sha-0…");
@@ -795,15 +786,11 @@ try_succeed = ["+foo", "+bar", "-baz"]
795786

796787
#[sqlx::test]
797788
async fn try_build_failed_modify_labels(pool: sqlx::PgPool) {
798-
let gh = GitHubState::default();
799-
gh.default_repo().lock().set_config(
800-
r#"
789+
BorsBuilder::new(pool)
790+
.github(GitHubState::default().with_default_config(r#"
801791
[labels]
802792
try_failed = ["+foo", "+bar", "-baz"]
803-
"#,
804-
);
805-
BorsBuilder::new(pool)
806-
.github(gh)
793+
"#))
807794
.run_test(|mut tester| async {
808795
tester.create_branch(TRY_BRANCH_NAME).expect_suites(1);
809796
tester.post_comment("@bors try").await?;

src/tests/mocks/bors.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::task::JoinHandle;
1313
use tower::Service;
1414

1515
use crate::bors::{RollupMode, WAIT_FOR_REFRESH};
16-
use crate::database::PullRequestModel;
16+
use crate::database::{BuildStatus, PullRequestModel};
1717
use crate::github::api::load_repositories;
1818
use crate::github::{GithubRepoName, PullRequestNumber};
1919
use crate::tests::mocks::comment::{Comment, GitHubIssueCommentEventPayload};
@@ -538,6 +538,14 @@ impl PullRequestProxy {
538538
self
539539
}
540540

541+
#[track_caller]
542+
pub fn expect_try_build_cancelled(&self) {
543+
assert_eq!(
544+
self.require_db_pr().try_build.as_ref().unwrap().status,
545+
BuildStatus::Cancelled
546+
);
547+
}
548+
541549
#[track_caller]
542550
fn require_db_pr(&self) -> &PullRequestModel {
543551
self.db_pr.as_ref().unwrap()

src/tests/mocks/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl GitHubState {
5050
}
5151

5252
/// Creates a new GitHubState where the default PR author has no permissions.
53-
pub fn with_unauthorized_pr_author() -> Self {
53+
pub fn unauthorized_pr_author() -> Self {
5454
let state = Self::default();
5555
state
5656
.default_repo()
@@ -61,6 +61,11 @@ impl GitHubState {
6161
state
6262
}
6363

64+
pub fn with_default_config(self, config: &str) -> Self {
65+
self.default_repo().lock().config = config.to_string();
66+
self
67+
}
68+
6469
pub fn default_repo(&self) -> Arc<Mutex<Repo>> {
6570
self.get_repo(&default_repo_name())
6671
}

src/tests/mocks/repository.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ impl Repo {
138138
self.pull_requests.get_mut(&pr).unwrap()
139139
}
140140

141-
pub fn set_config(&mut self, config: &str) {
142-
self.config = config.to_string();
143-
}
144-
145141
pub fn get_branch_by_name(&mut self, name: &str) -> Option<&mut Branch> {
146142
self.branches.iter_mut().find(|b| b.name == name)
147143
}

0 commit comments

Comments
 (0)