@@ -436,6 +436,7 @@ pub mod metrics {
436
436
#[ cfg( any( test, feature = "testing" ) ) ]
437
437
pub mod testing {
438
438
use super :: * ;
439
+ use crate :: event:: snapshot:: Location ;
439
440
use core:: sync:: atomic:: { AtomicU32 , Ordering } ;
440
441
use std:: sync:: Mutex ;
441
442
#[ derive( Clone , Debug ) ]
@@ -450,7 +451,7 @@ pub mod testing {
450
451
return ;
451
452
}
452
453
if let Some ( location) = self . location . as_ref ( ) {
453
- location. snapshot ( & self . output . lock ( ) . unwrap ( ) ) ;
454
+ location. snapshot_log ( & self . output . lock ( ) . unwrap ( ) ) ;
454
455
}
455
456
}
456
457
}
@@ -459,7 +460,14 @@ pub mod testing {
459
460
#[ track_caller]
460
461
pub fn snapshot ( ) -> Self {
461
462
let mut sub = Self :: no_snapshot ( ) ;
462
- sub. location = Location :: try_new ( ) ;
463
+ sub. location = Location :: from_thread_name ( ) ;
464
+ sub
465
+ }
466
+ #[ doc = r" Creates a subscriber with snapshot assertions enabled" ]
467
+ #[ track_caller]
468
+ pub fn named_snapshot < Name : core:: fmt:: Display > ( name : Name ) -> Self {
469
+ let mut sub = Self :: no_snapshot ( ) ;
470
+ sub. location = Some ( Location :: new ( name) ) ;
463
471
sub
464
472
}
465
473
#[ doc = r" Creates a subscriber with snapshot assertions disabled" ]
@@ -505,7 +513,14 @@ pub mod testing {
505
513
#[ track_caller]
506
514
pub fn snapshot ( ) -> Self {
507
515
let mut sub = Self :: no_snapshot ( ) ;
508
- sub. location = Location :: try_new ( ) ;
516
+ sub. location = Location :: from_thread_name ( ) ;
517
+ sub
518
+ }
519
+ #[ doc = r" Creates a subscriber with snapshot assertions enabled" ]
520
+ #[ track_caller]
521
+ pub fn named_snapshot < Name : core:: fmt:: Display > ( name : Name ) -> Self {
522
+ let mut sub = Self :: no_snapshot ( ) ;
523
+ sub. location = Some ( Location :: new ( name) ) ;
509
524
sub
510
525
}
511
526
#[ doc = r" Creates a publisher with snapshot assertions disabled" ]
@@ -543,52 +558,8 @@ pub mod testing {
543
558
return ;
544
559
}
545
560
if let Some ( location) = self . location . as_ref ( ) {
546
- location. snapshot ( & self . output . lock ( ) . unwrap ( ) ) ;
561
+ location. snapshot_log ( & self . output . lock ( ) . unwrap ( ) ) ;
547
562
}
548
563
}
549
564
}
550
- #[ derive( Clone , Debug ) ]
551
- struct Location ( & ' static core:: panic:: Location < ' static > ) ;
552
- impl Location {
553
- #[ track_caller]
554
- fn try_new ( ) -> Option < Self > {
555
- let thread = std:: thread:: current ( ) ;
556
- if thread. name ( ) . map_or ( false , |name| name != "main" ) {
557
- Some ( Self ( core:: panic:: Location :: caller ( ) ) )
558
- } else {
559
- None
560
- }
561
- }
562
- fn snapshot ( & self , output : & [ String ] ) {
563
- if cfg ! ( miri) {
564
- return ;
565
- }
566
- use std:: path:: { Component , Path } ;
567
- let value = output. join ( "\n " ) ;
568
- let thread = std:: thread:: current ( ) ;
569
- let function_name = thread. name ( ) . unwrap ( ) ;
570
- let test_path = Path :: new ( self . 0 . file ( ) . trim_end_matches ( ".rs" ) ) ;
571
- let module_path = test_path
572
- . components ( )
573
- . filter_map ( |comp| match comp {
574
- Component :: Normal ( comp) => comp. to_str ( ) ,
575
- _ => Some ( "_" ) ,
576
- } )
577
- . chain ( Some ( "events" ) )
578
- . collect :: < Vec < _ > > ( )
579
- . join ( "::" ) ;
580
- let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
581
- insta:: _macro_support:: assert_snapshot (
582
- insta:: _macro_support:: AutoName . into ( ) ,
583
- & value,
584
- current_dir. to_str ( ) . unwrap ( ) ,
585
- function_name,
586
- & module_path,
587
- self . 0 . file ( ) ,
588
- self . 0 . line ( ) ,
589
- "" ,
590
- )
591
- . unwrap ( )
592
- }
593
- }
594
565
}
0 commit comments