Skip to content

Commit c23f5e7

Browse files
committed
Extract UpstreamIndex struct
1 parent 552deef commit c23f5e7

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/tests/git.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1+
use git2::Repository;
12
use std::env;
23
use std::fs;
34
use std::path::PathBuf;
45
use std::sync::Once;
56
use std::thread;
7+
use url::Url;
8+
9+
pub struct UpstreamIndex {
10+
pub repository: Repository,
11+
}
12+
13+
impl UpstreamIndex {
14+
pub fn new() -> anyhow::Result<Self> {
15+
init();
16+
17+
let thread_local_path = bare();
18+
let repository = Repository::open_bare(thread_local_path)?;
19+
Ok(Self { repository })
20+
}
21+
22+
pub fn url() -> Url {
23+
Url::from_file_path(&bare()).unwrap()
24+
}
25+
}
626

727
fn root() -> PathBuf {
828
env::current_dir()
@@ -12,11 +32,11 @@ fn root() -> PathBuf {
1232
.join(thread::current().name().unwrap())
1333
}
1434

15-
pub fn bare() -> PathBuf {
35+
fn bare() -> PathBuf {
1636
root().join("bare")
1737
}
1838

19-
pub fn init() {
39+
fn init() {
2040
static INIT: Once = Once::new();
2141
let _ = fs::remove_dir_all(&bare());
2242

src/tests/util/test_app.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{MockAnonymousUser, MockCookieUser, MockTokenUser};
2+
use crate::git::UpstreamIndex;
23
use crate::record;
34
use crate::util::{chaosproxy::ChaosProxy, fresh_schema::FreshSchema};
45
use cargo_registry::config;
@@ -12,18 +13,16 @@ use std::{rc::Rc, sync::Arc, time::Duration};
1213

1314
use cargo_registry::git::{Repository as WorkerRepository, Repository};
1415
use diesel::PgConnection;
15-
use git2::Repository as UpstreamRepository;
1616
use reqwest::{blocking::Client, Proxy};
1717
use std::collections::HashSet;
1818
use swirl::Runner;
19-
use url::Url;
2019

2120
struct TestAppInner {
2221
app: Arc<App>,
2322
// The bomb (if created) needs to be held in scope until the end of the test.
2423
_bomb: Option<record::Bomb>,
2524
middle: conduit_middleware::MiddlewareBuilder,
26-
index: Option<UpstreamRepository>,
25+
index: Option<UpstreamIndex>,
2726
runner: Option<Runner<Environment, DieselPool>>,
2827
db_chaosproxy: Option<Arc<ChaosProxy>>,
2928

@@ -129,8 +128,9 @@ impl TestApp {
129128
}
130129

131130
/// Obtain a reference to the upstream repository ("the index")
132-
pub fn upstream_repository(&self) -> &UpstreamRepository {
133-
self.0.index.as_ref().unwrap()
131+
pub fn upstream_repository(&self) -> &git2::Repository {
132+
let index = self.0.index.as_ref().unwrap();
133+
&index.repository
134134
}
135135

136136
/// Obtain a list of crates from the index HEAD
@@ -186,15 +186,13 @@ pub struct TestAppBuilder {
186186
config: config::Server,
187187
proxy: Option<String>,
188188
bomb: Option<record::Bomb>,
189-
index: Option<UpstreamRepository>,
189+
index: Option<UpstreamIndex>,
190190
build_job_runner: bool,
191191
}
192192

193193
impl TestAppBuilder {
194194
/// Create a `TestApp` with an empty database
195195
pub fn empty(mut self) -> (TestApp, MockAnonymousUser) {
196-
use crate::git;
197-
198196
// Run each test inside a fresh database schema, deleted at the end of the test,
199197
// The schema will be cleared up once the app is dropped.
200198
let (db_chaosproxy, fresh_schema) = if !self.config.use_test_database_pool {
@@ -211,7 +209,7 @@ impl TestAppBuilder {
211209

212210
let runner = if self.build_job_runner {
213211
let repository_config = RepositoryConfig {
214-
index_location: Url::from_file_path(&git::bare()).unwrap(),
212+
index_location: UpstreamIndex::url(),
215213
credentials: Credentials::Missing,
216214
};
217215
let index = WorkerRepository::open(&repository_config).expect("Could not clone index");
@@ -286,12 +284,7 @@ impl TestAppBuilder {
286284
}
287285

288286
pub fn with_git_index(mut self) -> Self {
289-
use crate::git;
290-
291-
git::init();
292-
293-
let thread_local_path = git::bare();
294-
self.index = Some(UpstreamRepository::open_bare(thread_local_path).unwrap());
287+
self.index = Some(UpstreamIndex::new().unwrap());
295288
self
296289
}
297290

0 commit comments

Comments
 (0)