@@ -12,11 +12,10 @@ use crate::{
1212 ServiceMetrics ,
1313} ;
1414use anyhow:: Context as _;
15- use arc_swap:: ArcSwap ;
1615use axum:: body:: Bytes ;
1716use axum:: { Router , body:: Body , http:: Request , response:: Response as AxumResponse } ;
1817use fn_error_context:: context;
19- use futures_util:: { FutureExt , stream:: TryStreamExt } ;
18+ use futures_util:: stream:: TryStreamExt ;
2019use http_body_util:: BodyExt ; // for `collect`
2120use serde:: de:: DeserializeOwned ;
2221use sqlx:: Connection as _;
@@ -28,24 +27,7 @@ use tracing::error;
2827#[ track_caller]
2928pub ( 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
5133pub ( crate ) fn async_wrapper < F , Fut > ( f : F )
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
8543pub ( crate ) trait AxumResponseTestExt {
@@ -373,9 +331,13 @@ pub(crate) fn init_logger() {
373331
374332impl 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 ) ]
572517pub ( crate ) struct TestDatabase {
573518 pool : Pool ,
0 commit comments