@@ -12,11 +12,10 @@ use crate::{
12
12
ServiceMetrics ,
13
13
} ;
14
14
use anyhow:: Context as _;
15
- use arc_swap:: ArcSwap ;
16
15
use axum:: body:: Bytes ;
17
16
use axum:: { Router , body:: Body , http:: Request , response:: Response as AxumResponse } ;
18
17
use fn_error_context:: context;
19
- use futures_util:: { FutureExt , stream:: TryStreamExt } ;
18
+ use futures_util:: stream:: TryStreamExt ;
20
19
use http_body_util:: BodyExt ; // for `collect`
21
20
use serde:: de:: DeserializeOwned ;
22
21
use sqlx:: Connection as _;
@@ -28,24 +27,7 @@ use tracing::error;
28
27
#[ track_caller]
29
28
pub ( crate ) fn wrapper ( f : impl FnOnce ( & TestEnvironment ) -> Result < ( ) > ) {
30
29
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" ) ;
49
31
}
50
32
51
33
pub ( crate ) fn async_wrapper < F , Fut > ( f : F )
55
37
{
56
38
let env = Rc :: new ( TestEnvironment :: new ( ) ) ;
57
39
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" ) ;
83
41
}
84
42
85
43
pub ( crate ) trait AxumResponseTestExt {
@@ -373,9 +331,13 @@ pub(crate) fn init_logger() {
373
331
374
332
impl TestEnvironment {
375
333
fn new ( ) -> Self {
334
+ Self :: with_config ( Self :: base_config ( ) )
335
+ }
336
+
337
+ fn with_config ( config : Config ) -> Self {
376
338
init_logger ( ) ;
377
339
378
- let config = Arc :: new ( TestEnvironment :: base_config ( ) ) ;
340
+ let config = Arc :: new ( config ) ;
379
341
let runtime = Arc :: new (
380
342
Builder :: new_current_thread ( )
381
343
. enable_all ( )
@@ -451,17 +413,6 @@ impl TestEnvironment {
451
413
}
452
414
}
453
415
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
-
465
416
pub ( crate ) fn base_config ( ) -> Config {
466
417
let mut config = Config :: from_env ( ) . expect ( "failed to get base config" ) ;
467
418
@@ -495,9 +446,10 @@ impl TestEnvironment {
495
446
let mut config = Self :: base_config ( ) ;
496
447
f ( & mut config) ;
497
448
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
+ // }
501
453
}
502
454
503
455
pub ( crate ) fn async_build_queue ( & self ) -> Arc < AsyncBuildQueue > {
@@ -528,30 +480,10 @@ impl TestEnvironment {
528
480
self . context . instance_metrics . clone ( )
529
481
}
530
482
531
- pub ( crate ) fn service_metrics ( & self ) -> Arc < ServiceMetrics > {
532
- self . context . service_metrics . clone ( )
533
- }
534
-
535
483
pub ( crate ) fn runtime ( & self ) -> Arc < Runtime > {
536
484
self . context . runtime . clone ( )
537
485
}
538
486
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
-
555
487
pub ( crate ) fn async_db ( & self ) -> & TestDatabase {
556
488
& self . db
557
489
}
@@ -568,6 +500,19 @@ impl TestEnvironment {
568
500
}
569
501
}
570
502
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
+
571
516
#[ derive( Debug ) ]
572
517
pub ( crate ) struct TestDatabase {
573
518
pool : Pool ,
0 commit comments