Skip to content

Commit f76d562

Browse files
committed
fixes
1 parent b2020a3 commit f76d562

File tree

5 files changed

+40
-109
lines changed

5 files changed

+40
-109
lines changed

src/cdn.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ mod tests {
10491049
// handle the queued invalidations
10501050
handle_queued_invalidation_requests(
10511051
&env.config(),
1052-
&*env.cdn(),
1052+
&env.cdn(),
10531053
&env.instance_metrics(),
10541054
&mut conn,
10551055
"distribution_id_web",
@@ -1112,7 +1112,7 @@ mod tests {
11121112
// handle the queued invalidations
11131113
handle_queued_invalidation_requests(
11141114
&env.config(),
1115-
&*env.cdn(),
1115+
&env.cdn(),
11161116
&env.instance_metrics(),
11171117
&mut conn,
11181118
"distribution_id_web",
@@ -1143,7 +1143,7 @@ mod tests {
11431143
// now handle again
11441144
handle_queued_invalidation_requests(
11451145
&env.config(),
1146-
&*env.cdn(),
1146+
&env.cdn(),
11471147
&env.instance_metrics(),
11481148
&mut conn,
11491149
"distribution_id_web",
@@ -1187,7 +1187,7 @@ mod tests {
11871187
// run the handler
11881188
handle_queued_invalidation_requests(
11891189
&env.config(),
1190-
&*env.cdn(),
1190+
&env.cdn(),
11911191
&env.instance_metrics(),
11921192
&mut conn,
11931193
"distribution_id_web",

src/db/delete.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ mod tests {
311311
);
312312
}
313313

314-
delete_crate(&mut conn, &*env.async_storage(), &env.config(), "package-1").await?;
314+
delete_crate(&mut conn, &env.async_storage(), &env.config(), "package-1").await?;
315315

316316
assert!(!crate_exists(&mut conn, "package-1").await?);
317317
assert!(crate_exists(&mut conn, "package-2").await?);
@@ -426,7 +426,7 @@ mod tests {
426426
.rustdoc_file_exists("a", "1.0.0", None, "a/index.html", archive_storage)
427427
.await?
428428
);
429-
assert!(json_exists(&*env.async_storage(), "1.0.0").await?);
429+
assert!(json_exists(&env.async_storage(), "1.0.0").await?);
430430
let crate_id = sqlx::query_scalar!(
431431
r#"SELECT crate_id as "crate_id: CrateId" FROM releases WHERE id = $1"#,
432432
v1.0
@@ -457,20 +457,13 @@ mod tests {
457457
.rustdoc_file_exists("a", "2.0.0", None, "a/index.html", archive_storage)
458458
.await?
459459
);
460-
assert!(json_exists(&*env.async_storage(), "2.0.0").await?);
460+
assert!(json_exists(&env.async_storage(), "2.0.0").await?);
461461
assert_eq!(
462462
owners(&mut conn, crate_id).await?,
463463
vec!["Peter Rabbit".to_string()]
464464
);
465465

466-
delete_version(
467-
&mut conn,
468-
&*env.async_storage(),
469-
&env.config(),
470-
"a",
471-
"1.0.0",
472-
)
473-
.await?;
466+
delete_version(&mut conn, &env.async_storage(), &env.config(), "a", "1.0.0").await?;
474467
assert!(!release_exists(&mut conn, v1).await?);
475468
if archive_storage {
476469
// for archive storage the archive and index files
@@ -494,15 +487,15 @@ mod tests {
494487
.await?
495488
);
496489
}
497-
assert!(!json_exists(&*env.async_storage(), "1.0.0").await?);
490+
assert!(!json_exists(&env.async_storage(), "1.0.0").await?);
498491

499492
assert!(release_exists(&mut conn, v2).await?);
500493
assert!(
501494
env.async_storage()
502495
.rustdoc_file_exists("a", "2.0.0", None, "a/index.html", archive_storage)
503496
.await?
504497
);
505-
assert!(json_exists(&*env.async_storage(), "2.0.0").await?);
498+
assert!(json_exists(&env.async_storage(), "2.0.0").await?);
506499
assert_eq!(
507500
owners(&mut conn, crate_id).await?,
508501
vec!["Peter Rabbit".to_string()]
@@ -527,14 +520,7 @@ mod tests {
527520
fake_release_that_failed_before_build(&mut conn, "a", "1.0.0", "some-error")
528521
.await?;
529522

530-
delete_version(
531-
&mut conn,
532-
&*env.async_storage(),
533-
&env.config(),
534-
"a",
535-
"1.0.0",
536-
)
537-
.await?;
523+
delete_version(&mut conn, &env.async_storage(), &env.config(), "a", "1.0.0").await?;
538524

539525
assert!(!release_exists(&mut conn, release_id).await?);
540526

@@ -552,7 +538,7 @@ mod tests {
552538
fake_release_that_failed_before_build(&mut conn, "a", "1.0.0", "some-error")
553539
.await?;
554540

555-
delete_crate(&mut conn, &*env.async_storage(), &env.config(), "a").await?;
541+
delete_crate(&mut conn, &env.async_storage(), &env.config(), "a").await?;
556542

557543
assert!(!crate_exists(&mut conn, "a").await?);
558544
assert!(!release_exists(&mut conn, release_id).await?);

src/test/mod.rs

Lines changed: 25 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ use crate::{
1212
ServiceMetrics,
1313
};
1414
use anyhow::Context as _;
15-
use arc_swap::ArcSwap;
1615
use axum::body::Bytes;
1716
use axum::{Router, body::Body, http::Request, response::Response as AxumResponse};
1817
use fn_error_context::context;
19-
use futures_util::{FutureExt, stream::TryStreamExt};
18+
use futures_util::stream::TryStreamExt;
2019
use http_body_util::BodyExt; // for `collect`
2120
use serde::de::DeserializeOwned;
2221
use sqlx::Connection as _;
@@ -28,24 +27,7 @@ use tracing::error;
2827
#[track_caller]
2928
pub(crate) fn wrapper(f: impl FnOnce(&TestEnvironment) -> Result<()>) {
3029
let env = TestEnvironment::new();
31-
// if we didn't catch the panic, the server would hang forever
32-
let maybe_panic = panic::catch_unwind(panic::AssertUnwindSafe(|| f(&env)));
33-
env.cleanup();
34-
let result = match maybe_panic {
35-
Ok(r) => r,
36-
Err(payload) => panic::resume_unwind(payload),
37-
};
38-
39-
if let Err(err) = result {
40-
eprintln!("the test failed: {err}");
41-
for cause in err.chain() {
42-
eprintln!(" caused by: {cause}");
43-
}
44-
45-
eprintln!("{}", err.backtrace());
46-
47-
panic!("the test failed");
48-
}
30+
f(&env).expect("test failed");
4931
}
5032

5133
pub(crate) fn async_wrapper<F, Fut>(f: F)
@@ -55,31 +37,7 @@ where
5537
{
5638
let env = Rc::new(TestEnvironment::new());
5739

58-
let fut = f(env.clone());
59-
60-
let runtime = env.runtime();
61-
62-
// if we didn't catch the panic, the server would hang forever
63-
let maybe_panic = runtime.block_on(panic::AssertUnwindSafe(fut).catch_unwind());
64-
65-
let env = Rc::into_inner(env).unwrap();
66-
env.cleanup();
67-
68-
let result = match maybe_panic {
69-
Ok(r) => r,
70-
Err(payload) => panic::resume_unwind(payload),
71-
};
72-
73-
if let Err(err) = result {
74-
eprintln!("the test failed: {err}");
75-
for cause in err.chain() {
76-
eprintln!(" caused by: {cause}");
77-
}
78-
79-
eprintln!("{}", err.backtrace());
80-
81-
panic!("the test failed");
82-
}
40+
env.runtime().block_on(f(env.clone())).expect("test failed");
8341
}
8442

8543
pub(crate) trait AxumResponseTestExt {
@@ -373,9 +331,13 @@ pub(crate) fn init_logger() {
373331

374332
impl TestEnvironment {
375333
fn new() -> Self {
334+
Self::with_config(Self::base_config())
335+
}
336+
337+
fn with_config(config: Config) -> Self {
376338
init_logger();
377339

378-
let config = Arc::new(TestEnvironment::base_config());
340+
let config = Arc::new(config);
379341
let runtime = Arc::new(
380342
Builder::new_current_thread()
381343
.enable_all()
@@ -451,17 +413,6 @@ impl TestEnvironment {
451413
}
452414
}
453415

454-
fn cleanup(self) {
455-
self.context
456-
.storage
457-
.cleanup_after_test()
458-
.expect("failed to cleanup after tests");
459-
460-
if self.context.config.local_archive_cache_path.exists() {
461-
fs::remove_dir_all(&self.context.config.local_archive_cache_path).unwrap();
462-
}
463-
}
464-
465416
pub(crate) fn base_config() -> Config {
466417
let mut config = Config::from_env().expect("failed to get base config");
467418

@@ -495,9 +446,10 @@ impl TestEnvironment {
495446
let mut config = Self::base_config();
496447
f(&mut config);
497448

498-
if self.context.config.set(Arc::new(config)).is_err() {
499-
panic!("can't call override_config after the configuration is accessed!");
500-
}
449+
// FIXME: fix
450+
// if self.context.config.set(Arc::new(config)).is_err() {
451+
// panic!("can't call override_config after the configuration is accessed!");
452+
// }
501453
}
502454

503455
pub(crate) fn async_build_queue(&self) -> Arc<AsyncBuildQueue> {
@@ -528,30 +480,10 @@ impl TestEnvironment {
528480
self.context.instance_metrics.clone()
529481
}
530482

531-
pub(crate) fn service_metrics(&self) -> Arc<ServiceMetrics> {
532-
self.context.service_metrics.clone()
533-
}
534-
535483
pub(crate) fn runtime(&self) -> Arc<Runtime> {
536484
self.context.runtime.clone()
537485
}
538486

539-
pub(crate) fn index(&self) -> Arc<Index> {
540-
self.context.index.clone()
541-
}
542-
543-
pub(crate) fn registry_api(&self) -> Arc<RegistryApi> {
544-
self.context.registry_api.clone()
545-
}
546-
547-
pub(crate) fn repository_stats_updater(&self) -> Arc<RepositoryStatsUpdater> {
548-
self.context.repository_stats_updater.clone()
549-
}
550-
551-
pub(crate) fn db(&self) -> &TestDatabase {
552-
&self.db
553-
}
554-
555487
pub(crate) fn async_db(&self) -> &TestDatabase {
556488
&self.db
557489
}
@@ -568,6 +500,19 @@ impl TestEnvironment {
568500
}
569501
}
570502

503+
impl Drop for TestEnvironment {
504+
fn drop(&mut self) {
505+
self.context
506+
.storage
507+
.cleanup_after_test()
508+
.expect("failed to cleanup after tests");
509+
510+
if self.context.config.local_archive_cache_path.exists() {
511+
fs::remove_dir_all(&self.context.config.local_archive_cache_path).unwrap();
512+
}
513+
}
514+
}
515+
571516
#[derive(Debug)]
572517
pub(crate) struct TestDatabase {
573518
pool: Pool,

src/web/crate_details.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ mod tests {
22032203
let mut conn = env.async_db().async_conn().await;
22042204
let details = crate_details(&mut conn, "dummy", "0.5.0", None).await;
22052205
assert!(matches!(
2206-
details.fetch_readme(&*env.async_storage()).await,
2206+
details.fetch_readme(&env.async_storage()).await,
22072207
Ok(None)
22082208
));
22092209
Ok(())

src/web/file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ mod tests {
101101
env.fake_release().await.create().await?;
102102

103103
let mut file = File::from_path(
104-
&*env.async_storage(),
104+
&env.async_storage(),
105105
"rustdoc/fake-package/1.0.0/fake-package/index.html",
106106
&env.config(),
107107
)
@@ -153,7 +153,7 @@ mod tests {
153153
let env = env.clone();
154154
async move {
155155
File::from_path(
156-
&*env.async_storage(),
156+
&env.async_storage(),
157157
&format!("rustdoc/dummy/0.1.0/{path}"),
158158
&env.config(),
159159
)

0 commit comments

Comments
 (0)