Skip to content

Commit 7893215

Browse files
committed
less duplication
1 parent 51d9c71 commit 7893215

File tree

2 files changed

+28
-70
lines changed

2 files changed

+28
-70
lines changed

src/context.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,34 @@ pub struct Context {
2626
}
2727

2828
impl Context {
29+
/// Create a new context environment from the given configuration.
30+
#[cfg(not(test))]
2931
pub async fn from_config(config: Config) -> Result<Self> {
30-
let config = Arc::new(config);
31-
3232
let instance_metrics = Arc::new(InstanceMetrics::new()?);
33-
3433
let pool = Pool::new(&config, instance_metrics.clone()).await?;
34+
Self::from_config_with_metrics_and_pool(config, instance_metrics, pool).await
35+
}
36+
37+
/// Create a new context environment from the given configuration, for running tests.
38+
#[cfg(test)]
39+
pub async fn from_config(
40+
config: Config,
41+
instance_metrics: Arc<InstanceMetrics>,
42+
pool: Pool,
43+
) -> Result<Self> {
44+
Self::from_config_with_metrics_and_pool(config, instance_metrics, pool).await
45+
}
46+
47+
/// private function for context environment generation, allows passing in a
48+
/// preconfigured instance metrics & pool from the database.
49+
/// Mostly so we can support test environments with their db
50+
async fn from_config_with_metrics_and_pool(
51+
config: Config,
52+
instance_metrics: Arc<InstanceMetrics>,
53+
pool: Pool,
54+
) -> Result<Self> {
55+
let config = Arc::new(config);
56+
3557
let async_storage = Arc::new(
3658
AsyncStorage::new(pool.clone(), instance_metrics.clone(), config.clone()).await?,
3759
);

src/test/mod.rs

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ use crate::cdn::CdnBackend;
55
use crate::config::ConfigBuilder;
66
use crate::db::{self, AsyncPoolClient, Pool};
77
use crate::error::Result;
8-
use crate::repositories::RepositoryStatsUpdater;
98
use crate::storage::{AsyncStorage, Storage, StorageKind};
109
use crate::web::{build_axum_app, cache, page::TemplateData};
11-
use crate::{
12-
AsyncBuildQueue, BuildQueue, Config, Context, Index, InstanceMetrics, RegistryApi,
13-
ServiceMetrics,
14-
};
10+
use crate::{AsyncBuildQueue, BuildQueue, Config, Context, InstanceMetrics};
1511
use anyhow::Context as _;
1612
use axum::body::Bytes;
1713
use axum::{Router, body::Body, http::Request, response::Response as AxumResponse};
@@ -358,73 +354,13 @@ impl TestEnvironment {
358354
// create index directory
359355
fs::create_dir_all(config.registry_index_path.clone())?;
360356

361-
let config = Arc::new(config);
362-
let instance_metrics =
363-
Arc::new(InstanceMetrics::new().context("failed to initialize the instance metrics")?);
364-
357+
let instance_metrics = Arc::new(InstanceMetrics::new()?);
365358
let test_db = TestDatabase::new(&config, instance_metrics.clone())
366359
.await
367360
.context("can't initialize test database")?;
368-
let pool = test_db.pool();
369-
370-
let async_storage = Arc::new(
371-
AsyncStorage::new(pool.clone(), instance_metrics.clone(), config.clone())
372-
.await
373-
.context("can't create async storage")?,
374-
);
375-
376-
let async_build_queue = Arc::new(AsyncBuildQueue::new(
377-
pool.clone(),
378-
instance_metrics.clone(),
379-
config.clone(),
380-
async_storage.clone(),
381-
));
382-
383-
let runtime = runtime::Handle::current();
384-
385-
let build_queue = Arc::new(BuildQueue::new(runtime.clone(), async_build_queue.clone()));
386-
387-
let storage = Arc::new(Storage::new(async_storage.clone(), runtime.clone()));
388-
389-
let cdn = Arc::new(CdnBackend::new(&config).await);
390-
391-
let index = Arc::new({
392-
let path = config.registry_index_path.clone();
393-
if let Some(registry_url) = config.registry_url.clone() {
394-
Index::from_url(path, registry_url)
395-
} else {
396-
Index::new(path)
397-
}
398-
.context("can't create Index")?
399-
});
400361

401362
Ok(Self {
402-
context: Context {
403-
config: config.clone(),
404-
async_build_queue,
405-
build_queue,
406-
storage,
407-
async_storage,
408-
cdn,
409-
pool: pool.clone(),
410-
service_metrics: Arc::new(
411-
ServiceMetrics::new().context("can't initialize service metrics")?,
412-
),
413-
instance_metrics,
414-
index,
415-
registry_api: Arc::new(
416-
RegistryApi::new(
417-
config.registry_api_host.clone(),
418-
config.crates_io_api_call_retries,
419-
)
420-
.context("can't create registry api")?,
421-
),
422-
repository_stats_updater: Arc::new(RepositoryStatsUpdater::new(
423-
&config,
424-
pool.clone(),
425-
)),
426-
runtime,
427-
},
363+
context: Context::from_config(config, instance_metrics, test_db.pool().clone()).await?,
428364
db: test_db,
429365
owned_runtime: None,
430366
})

0 commit comments

Comments
 (0)