Skip to content

Commit 1eccc92

Browse files
committed
Auto merge of #4384 - Turbo87:upstream-index, r=Turbo87
tests: Extract `UpstreamIndex` struct This should encapsulate our mocked upstream index git operations a little bit better in the testing code.
2 parents b50d1bc + d2d3292 commit 1eccc92

File tree

3 files changed

+74
-43
lines changed

3 files changed

+74
-43
lines changed

src/tests/git.rs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,69 @@
1+
use anyhow::anyhow;
2+
use git2::Repository;
13
use std::env;
24
use std::fs;
35
use std::path::PathBuf;
46
use std::sync::Once;
57
use std::thread;
8+
use url::Url;
9+
10+
pub struct UpstreamIndex {
11+
pub repository: Repository,
12+
}
13+
14+
impl UpstreamIndex {
15+
pub fn new() -> anyhow::Result<Self> {
16+
init();
17+
18+
let thread_local_path = bare();
19+
let repository = Repository::open_bare(thread_local_path)?;
20+
Ok(Self { repository })
21+
}
22+
23+
pub fn url() -> Url {
24+
Url::from_file_path(&bare()).unwrap()
25+
}
26+
27+
/// Obtain a list of crates from the index HEAD
28+
pub fn crates_from_index_head(
29+
&self,
30+
crate_name: &str,
31+
) -> anyhow::Result<Vec<cargo_registry::git::Crate>> {
32+
let repo = &self.repository;
33+
34+
let path = cargo_registry::git::Repository::relative_index_file(crate_name);
35+
36+
let head = repo.head()?;
37+
let tree = head.peel_to_tree()?;
38+
let blob = tree.get_path(&path)?.to_object(repo)?.peel_to_blob()?;
39+
40+
let content = blob.content();
41+
42+
// The index format consists of one JSON object per line
43+
// It is not a JSON array
44+
let lines = std::str::from_utf8(content)?.lines();
45+
let versions = lines.map(serde_json::from_str).collect::<Result<_, _>>()?;
46+
47+
Ok(versions)
48+
}
49+
50+
pub fn create_empty_commit(&self) -> anyhow::Result<()> {
51+
let repo = &self.repository;
52+
53+
let head = repo.head()?;
54+
let target = head
55+
.target()
56+
.ok_or_else(|| anyhow!("Missing target for HEAD"))?;
57+
58+
let sig = repo.signature()?;
59+
let parent = repo.find_commit(target)?;
60+
let tree = repo.find_tree(parent.tree_id())?;
61+
62+
repo.commit(Some("HEAD"), &sig, &sig, "empty commit", &tree, &[&parent])?;
63+
64+
Ok(())
65+
}
66+
}
667

768
fn root() -> PathBuf {
869
env::current_dir()
@@ -12,11 +73,11 @@ fn root() -> PathBuf {
1273
.join(thread::current().name().unwrap())
1374
}
1475

15-
pub fn bare() -> PathBuf {
76+
fn bare() -> PathBuf {
1677
root().join("bare")
1778
}
1879

19-
pub fn init() {
80+
fn init() {
2081
static INIT: Once = Once::new();
2182
let _ = fs::remove_dir_all(&bare());
2283

src/tests/krate/publish.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,7 @@ fn new_krate_git_upload_appends() {
564564
fn new_krate_git_upload_with_conflicts() {
565565
let (app, _, _, token) = TestApp::full().with_token();
566566

567-
let index = app.upstream_repository();
568-
let target = index.head().unwrap().target().unwrap();
569-
let sig = index.signature().unwrap();
570-
let parent = index.find_commit(target).unwrap();
571-
let tree = index.find_tree(parent.tree_id()).unwrap();
572-
index
573-
.commit(Some("HEAD"), &sig, &sig, "empty commit", &tree, &[&parent])
574-
.unwrap();
567+
app.upstream_index().create_empty_commit().unwrap();
575568

576569
let crate_to_publish = PublishBuilder::new("foo_conflicts");
577570
token.enqueue_publish(crate_to_publish).good();

src/tests/util/test_app.rs

Lines changed: 10 additions & 33 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;
@@ -10,20 +11,18 @@ use cargo_registry::{
1011
};
1112
use std::{rc::Rc, sync::Arc, time::Duration};
1213

13-
use cargo_registry::git::{Repository as WorkerRepository, Repository};
14+
use cargo_registry::git::Repository as WorkerRepository;
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,30 +128,15 @@ 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_index(&self) -> &UpstreamIndex {
132+
assert_some!(self.0.index.as_ref())
134133
}
135134

136135
/// Obtain a list of crates from the index HEAD
137136
pub fn crates_from_index_head(&self, crate_name: &str) -> Vec<cargo_registry::git::Crate> {
138-
let path = Repository::relative_index_file(crate_name);
139-
let index = self.upstream_repository();
140-
let tree = index.head().unwrap().peel_to_tree().unwrap();
141-
let blob = tree
142-
.get_path(&path)
137+
self.upstream_index()
138+
.crates_from_index_head(crate_name)
143139
.unwrap()
144-
.to_object(index)
145-
.unwrap()
146-
.peel_to_blob()
147-
.unwrap();
148-
let content = blob.content();
149-
150-
// The index format consists of one JSON object per line
151-
// It is not a JSON array
152-
let lines = std::str::from_utf8(content).unwrap().lines();
153-
lines
154-
.map(|line| serde_json::from_str(line).unwrap())
155-
.collect()
156140
}
157141

158142
pub fn run_pending_background_jobs(&self) {
@@ -186,15 +170,13 @@ pub struct TestAppBuilder {
186170
config: config::Server,
187171
proxy: Option<String>,
188172
bomb: Option<record::Bomb>,
189-
index: Option<UpstreamRepository>,
173+
index: Option<UpstreamIndex>,
190174
build_job_runner: bool,
191175
}
192176

193177
impl TestAppBuilder {
194178
/// Create a `TestApp` with an empty database
195179
pub fn empty(mut self) -> (TestApp, MockAnonymousUser) {
196-
use crate::git;
197-
198180
// Run each test inside a fresh database schema, deleted at the end of the test,
199181
// The schema will be cleared up once the app is dropped.
200182
let (db_chaosproxy, fresh_schema) = if !self.config.use_test_database_pool {
@@ -211,7 +193,7 @@ impl TestAppBuilder {
211193

212194
let runner = if self.build_job_runner {
213195
let repository_config = RepositoryConfig {
214-
index_location: Url::from_file_path(&git::bare()).unwrap(),
196+
index_location: UpstreamIndex::url(),
215197
credentials: Credentials::Missing,
216198
};
217199
let index = WorkerRepository::open(&repository_config).expect("Could not clone index");
@@ -286,12 +268,7 @@ impl TestAppBuilder {
286268
}
287269

288270
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());
271+
self.index = Some(UpstreamIndex::new().unwrap());
295272
self
296273
}
297274

0 commit comments

Comments
 (0)