Skip to content

Commit 528842c

Browse files
committed
Make ignore_bot_comment test end-to-end
1 parent 02c638e commit 528842c

File tree

9 files changed

+41
-39
lines changed

9 files changed

+41
-39
lines changed

src/bors/handlers/mod.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use anyhow::Context;
21
use std::sync::Arc;
2+
3+
use anyhow::Context;
34
use tracing::Instrument;
45

56
use crate::bors::command::{BorsCommand, CommandParseError};
@@ -273,25 +274,17 @@ fn is_bors_observed_branch(branch: &str) -> bool {
273274

274275
#[cfg(test)]
275276
mod tests {
276-
use crate::tests::event::{comment, default_pr_number};
277-
use crate::tests::state::{test_bot_user, ClientBuilder};
277+
use crate::tests::mocks::{run_test, Comment, User};
278278

279279
#[sqlx::test]
280-
async fn test_ignore_bot_comment(pool: sqlx::PgPool) {
281-
let state = ClientBuilder::default().pool(pool).create_state().await;
282-
state
283-
.comment(comment("@bors ping").author(test_bot_user()).create())
284-
.await;
285-
state.client().check_comments(default_pr_number(), &[]);
280+
async fn ignore_bot_comment(pool: sqlx::PgPool) {
281+
run_test(pool, |mut tester| async {
282+
tester
283+
.post_comment(Comment::from("@bors ping").with_author(User::bors_bot()))
284+
.await;
285+
// Returning here will make sure that no comments were received
286+
Ok(tester)
287+
})
288+
.await;
286289
}
287-
288-
// #[sqlx::test]
289-
// async fn test_do_not_comment_when_pr_fetch_fails(pool: sqlx::PgPool) {
290-
// let state = ClientBuilder::default().pool(pool).create_state().await;
291-
// state
292-
// .client()
293-
// .set_get_pr_fn(|_| Err(anyhow::anyhow!("Foo")));
294-
// state.comment(comment("foo").create()).await;
295-
// state.client().check_comments(default_pr_number(), &[]);
296-
// }
297290
}

src/tests/event.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ impl From<CommentBuilder> for PullRequestComment {
6565
}
6666
}
6767

68-
pub fn comment(text: &str) -> CommentBuilder {
69-
CommentBuilder::default().text(text.to_string())
70-
}
71-
7268
pub fn suite_success() -> CheckSuite {
7369
CheckSuite {
7470
status: CheckSuiteStatus::Success,

src/tests/mocks/app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::tests::mocks::User;
12
use serde::Serialize;
23
use url::Url;
34
use wiremock::{
@@ -53,7 +54,7 @@ impl Default for App {
5354
owner: GitHubUser::default(),
5455
name: "bors".to_string(),
5556
// same as bors user html_url
56-
html_url: "https://test-bors.bot.com".parse().unwrap(),
57+
html_url: GitHubUser::from(User::bors_bot()).html_url,
5758
external_url: "https://test-bors.bot.com".parse().unwrap(),
5859
permissions: Permissions {},
5960
events: vec!["*".to_string()],

src/tests/mocks/bors.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,8 @@ impl BorsTester {
116116
.content
117117
}
118118

119-
pub async fn post_comment(&mut self, content: &str) {
120-
self.webhook_comment(Comment::new(
121-
Repo::default().name,
122-
default_pr_number(),
123-
content,
124-
))
125-
.await;
119+
pub async fn post_comment<C: Into<Comment>>(&mut self, comment: C) {
120+
self.webhook_comment(comment.into()).await;
126121
}
127122

128123
async fn webhook_comment(&mut self, comment: Comment) {

src/tests/mocks/comment.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use serde::Serialize;
66
use url::Url;
77

88
use crate::github::GithubRepoName;
9+
use crate::tests::event::default_pr_number;
910
use crate::tests::mocks::repository::GitHubRepository;
1011
use crate::tests::mocks::user::{GitHubUser, User};
12+
use crate::tests::mocks::Repo;
1113

1214
#[derive(Clone, Debug)]
1315
pub struct Comment {
@@ -32,6 +34,12 @@ impl Comment {
3234
}
3335
}
3436

37+
impl<'a> From<&'a str> for Comment {
38+
fn from(value: &'a str) -> Self {
39+
Comment::new(Repo::default().name, default_pr_number(), value)
40+
}
41+
}
42+
3543
// Copied from octocrab, since its version if #[non_exhaustive]
3644
#[derive(Serialize)]
3745
pub struct GitHubIssueCommentEventPayload {

src/tests/mocks/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::tests::mocks::permissions::TeamApiMockServer;
99
use crate::TeamApiClient;
1010

1111
pub use bors::run_test;
12+
pub use comment::Comment;
1213
pub use repository::Repo;
1314
pub use user::User;
1415

src/tests/mocks/pull_request.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use super::{
1010
Repo, User,
1111
};
1212

13+
pub fn default_pr_number() -> u64 {
14+
1
15+
}
16+
1317
pub async fn mock_pull_requests(
1418
repo: &Repo,
1519
comments_tx: Sender<Comment>,
@@ -35,7 +39,7 @@ pub async fn mock_pull_requests(
3539
let comment_payload: CommentCreatePayload = req.body_json().unwrap();
3640
let comment: Comment =
3741
Comment::new(repo_name.clone(), pr_number, &comment_payload.body)
38-
.with_author(User::new(1002, "bors"));
42+
.with_author(User::bors_bot());
3943

4044
// We cannot use `tx.blocking_send()`, because this function is actually called
4145
// from within an async task, but it is not async, so we also cannot use
@@ -105,10 +109,6 @@ struct Base {
105109
sha: String,
106110
}
107111

108-
fn default_pr_number() -> u64 {
109-
1
110-
}
111-
112112
#[derive(Deserialize)]
113113
struct CommentCreatePayload {
114114
body: String,

src/tests/mocks/repository.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ timeout = 3600
6464
}
6565
}
6666

67-
fn default_repo_name() -> GithubRepoName {
67+
pub fn default_repo_name() -> GithubRepoName {
6868
GithubRepoName::new("rust-lang", "borstest")
6969
}
7070

src/tests/mocks/user.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ pub struct User {
88
}
99

1010
impl User {
11+
pub fn default_user() -> Self {
12+
Self::new(101, "default-user")
13+
}
14+
15+
pub fn bors_bot() -> Self {
16+
Self::new(102, "bors-bot")
17+
}
18+
1119
pub fn new(id: u64, name: &str) -> Self {
1220
Self {
1321
github_id: id,
@@ -18,7 +26,7 @@ impl User {
1826

1927
impl Default for User {
2028
fn default() -> Self {
21-
Self::new(101, "default-user")
29+
Self::default_user()
2230
}
2331
}
2432

@@ -30,7 +38,7 @@ pub struct GitHubUser {
3038
avatar_url: Url,
3139
gravatar_id: String,
3240
url: Url,
33-
html_url: Url,
41+
pub html_url: Url,
3442
followers_url: Url,
3543
following_url: Url,
3644
gists_url: Url,

0 commit comments

Comments
 (0)