Skip to content

Commit ed4eb9f

Browse files
authored
Merge pull request #9662 from Turbo87/gh-ids
Extract `tests::util::github::next_gh_id()` fn
2 parents ee88277 + 9a3e35a commit ed4eb9f

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub mod tasks;
6060
pub mod team_repo;
6161
mod test_util;
6262
#[cfg(test)]
63-
mod tests;
63+
pub mod tests;
6464
pub mod typosquat;
6565
pub mod util;
6666
pub mod views;

src/tests/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::{
77
EncodableOwner, EncodableVersion, GoodCrate,
88
},
99
};
10-
use std::sync::atomic::{AtomicUsize, Ordering};
1110

11+
use crate::tests::util::github::next_gh_id;
1212
use diesel::prelude::*;
1313

1414
mod account_lock;
@@ -32,7 +32,7 @@ mod team;
3232
mod token;
3333
mod unhealthy_database;
3434
mod user;
35-
mod util;
35+
pub mod util;
3636
mod version;
3737
mod worker;
3838

@@ -86,11 +86,9 @@ pub struct OkBool {
8686
ok: bool,
8787
}
8888

89-
static NEXT_GH_ID: AtomicUsize = AtomicUsize::new(0);
90-
9189
fn new_user(login: &str) -> NewUser<'_> {
9290
NewUser {
93-
gh_id: NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32,
91+
gh_id: next_gh_id(),
9492
gh_login: login,
9593
name: None,
9694
gh_avatar: None,
@@ -100,8 +98,8 @@ fn new_user(login: &str) -> NewUser<'_> {
10098

10199
fn new_team(login: &str) -> NewTeam<'_> {
102100
NewTeam {
103-
org_id: NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32,
104-
github_id: NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32,
101+
org_id: next_gh_id(),
102+
github_id: next_gh_id(),
105103
login,
106104
name: None,
107105
avatar: None,

src/tests/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::net::SocketAddr;
4141
use tower::ServiceExt;
4242

4343
mod chaosproxy;
44-
mod github;
44+
pub mod github;
4545
pub mod insta;
4646
pub mod matchers;
4747
mod mock_request;
@@ -85,6 +85,7 @@ pub fn encode_session_header(session_key: &cookie::Key, user_id: i32) -> String
8585
/// A collection of helper methods for the 3 authentication types
8686
///
8787
/// Helper methods go through public APIs, and should not modify the database directly
88+
#[allow(async_fn_in_trait)]
8889
pub trait RequestHelper {
8990
fn request_builder(&self, method: Method, path: &str) -> MockRequest;
9091
fn app(&self) -> &TestApp;

src/tests/util/github.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ use crates_io_github::{
55
GitHubTeam, GitHubTeamMembership, GithubUser,
66
};
77
use oauth2::AccessToken;
8+
use std::sync::atomic::{AtomicUsize, Ordering};
9+
10+
static NEXT_GH_ID: AtomicUsize = AtomicUsize::new(0);
11+
12+
pub fn next_gh_id() -> i32 {
13+
NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32
14+
}
815

916
pub(crate) const MOCK_GITHUB_DATA: MockData = MockData {
1017
orgs: &[MockOrg {

src/typosquat/test_util.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use diesel::{prelude::*, PgConnection};
22

3+
use crate::tests::util::github::next_gh_id;
34
use crate::{
45
models::{
56
Crate, CrateOwner, NewCrate, NewTeam, NewUser, NewVersion, Owner, OwnerKind, User, Version,
@@ -10,14 +11,12 @@ use crate::{
1011

1112
pub struct Faker {
1213
emails: Emails,
13-
id: i32,
1414
}
1515

1616
impl Faker {
1717
pub fn new() -> Self {
1818
Self {
1919
emails: Emails::new_in_memory(),
20-
id: Default::default(),
2120
}
2221
}
2322

@@ -83,8 +82,8 @@ impl Faker {
8382
Ok(Owner::Team(
8483
NewTeam::new(
8584
&format!("github:{org}:{team}"),
86-
self.next_id(),
87-
self.next_id(),
85+
next_gh_id(),
86+
next_gh_id(),
8887
Some(team.to_string()),
8988
None,
9089
)
@@ -94,16 +93,11 @@ impl Faker {
9493

9594
pub fn user(&mut self, conn: &mut PgConnection, login: &str) -> anyhow::Result<User> {
9695
Ok(
97-
NewUser::new(self.next_id(), login, None, None, "token").create_or_update(
96+
NewUser::new(next_gh_id(), login, None, None, "token").create_or_update(
9897
None,
9998
&self.emails,
10099
conn,
101100
)?,
102101
)
103102
}
104-
105-
fn next_id(&mut self) -> i32 {
106-
self.id += 1;
107-
self.id
108-
}
109103
}

0 commit comments

Comments
 (0)