3
3
//! This private module is only compiled for test runs.
4
4
#![ allow( clippy:: indexing_slicing) ]
5
5
use std:: collections:: BTreeMap ;
6
+ use std:: fmt:: Write ;
6
7
use std:: ops:: { Deref , DerefMut } ;
7
8
use std:: panic;
9
+ use std:: path:: Path ;
8
10
use std:: sync:: Arc ;
9
11
use std:: time:: { Duration , Instant } ;
10
12
11
13
use ansi_term:: Color ;
12
14
use async_channel:: { self as channel, Receiver , Sender } ;
13
15
use chat:: ChatItem ;
14
16
use once_cell:: sync:: Lazy ;
17
+ use pretty_assertions:: assert_eq;
15
18
use rand:: Rng ;
16
19
use tempfile:: { tempdir, TempDir } ;
17
20
use tokio:: runtime:: Handle ;
18
21
use tokio:: sync:: RwLock ;
19
- use tokio:: task;
22
+ use tokio:: { fs , task} ;
20
23
21
24
use crate :: chat:: {
22
25
self , add_to_chat_contacts_table, create_group_chat, Chat , ChatId , MessageListOptions ,
@@ -285,7 +288,7 @@ impl TestContext {
285
288
println ! ( "\n ========== Chats of {}: ==========" , self . name( ) ) ;
286
289
if let Ok ( chats) = Chatlist :: try_load ( self , 0 , None , None ) . await {
287
290
for ( chat, _) in chats. iter ( ) {
288
- self . print_chat ( * chat) . await ;
291
+ print ! ( "{}" , self . display_chat ( * chat) . await ) ;
289
292
}
290
293
}
291
294
println ! ( ) ;
@@ -624,14 +627,38 @@ impl TestContext {
624
627
res
625
628
}
626
629
630
+ #[ allow( unused) ]
631
+ pub async fn golden_test_chat ( & self , chat_id : ChatId , filename : & str ) {
632
+ let filename = Path :: new ( "test-data/golden/" ) . join ( filename) ;
633
+
634
+ let actual = self . display_chat ( chat_id) . await ;
635
+
636
+ // We're using `unwrap_or_default()` here so that if the file doesn't exist,
637
+ // it can be created using `write` below.
638
+ let expected = fs:: read ( & filename) . await . unwrap_or_default ( ) ;
639
+ let expected = String :: from_utf8 ( expected) . unwrap ( ) ;
640
+ if ( std:: env:: var ( "UPDATE_GOLDEN_TESTS" ) == Ok ( "1" . to_string ( ) ) ) && actual != expected {
641
+ fs:: write ( & filename, & actual)
642
+ . await
643
+ . unwrap_or_else ( |e| panic ! ( "Error writing {filename:?}: {e}" ) ) ;
644
+ } else {
645
+ assert_eq ! (
646
+ actual, expected,
647
+ "To update the expected value, run `UPDATE_GOLDEN_TESTS=1 cargo test`"
648
+ ) ;
649
+ }
650
+ }
651
+
627
652
/// Prints out the entire chat to stdout.
628
653
///
629
654
/// You can use this to debug your test by printing the entire chat conversation.
630
655
// This code is mainly the same as `log_msglist` in `cmdline.rs`, so one day, we could
631
656
// merge them to a public function in the `deltachat` crate.
632
657
#[ allow( dead_code) ]
633
658
#[ allow( clippy:: indexing_slicing) ]
634
- pub async fn print_chat ( & self , chat_id : ChatId ) {
659
+ async fn display_chat ( & self , chat_id : ChatId ) -> String {
660
+ let mut res = String :: new ( ) ;
661
+
635
662
let msglist = chat:: get_chat_msgs_ex (
636
663
self ,
637
664
chat_id,
@@ -662,7 +689,8 @@ impl TestContext {
662
689
} else {
663
690
format ! ( "{} member(s)" , members. len( ) )
664
691
} ;
665
- println ! (
692
+ writeln ! (
693
+ res,
666
694
"{}#{}: {} [{}]{}{}{} {}" ,
667
695
sel_chat. typ,
668
696
sel_chat. get_id( ) ,
@@ -686,32 +714,38 @@ impl TestContext {
686
714
} else {
687
715
""
688
716
} ,
689
- ) ;
717
+ )
718
+ . unwrap ( ) ;
690
719
691
720
let mut lines_out = 0 ;
692
721
for msg_id in msglist {
693
722
if msg_id == MsgId :: new ( DC_MSG_ID_DAYMARKER ) {
694
- println ! (
723
+ writeln ! ( res ,
695
724
"--------------------------------------------------------------------------------"
696
- ) ;
725
+ )
726
+ . unwrap ( ) ;
697
727
698
728
lines_out += 1
699
729
} else if !msg_id. is_special ( ) {
700
730
if lines_out == 0 {
701
- println ! (
731
+ writeln ! ( res ,
702
732
"--------------------------------------------------------------------------------" ,
703
- ) ;
733
+ ) . unwrap ( ) ;
704
734
lines_out += 1
705
735
}
706
736
let msg = Message :: load_from_db ( self , msg_id) . await . unwrap ( ) ;
707
- log_msg ( self , "" , & msg) . await ;
737
+ write_msg ( self , "" , & msg, & mut res ) . await ;
708
738
}
709
739
}
710
740
if lines_out > 0 {
711
- println ! (
741
+ writeln ! (
742
+ res,
712
743
"--------------------------------------------------------------------------------"
713
- ) ;
744
+ )
745
+ . unwrap ( ) ;
714
746
}
747
+
748
+ res
715
749
}
716
750
717
751
pub async fn create_group_with_members (
@@ -1041,7 +1075,7 @@ fn print_event(event: &Event) {
1041
1075
/// Logs an individual message to stdout.
1042
1076
///
1043
1077
/// This includes a bunch of the message meta-data as well.
1044
- async fn log_msg ( context : & Context , prefix : & str , msg : & Message ) {
1078
+ async fn write_msg ( context : & Context , prefix : & str , msg : & Message , buf : & mut String ) {
1045
1079
let contact = match Contact :: get_by_id ( context, msg. get_from_id ( ) ) . await {
1046
1080
Ok ( contact) => contact,
1047
1081
Err ( e) => {
@@ -1061,7 +1095,8 @@ async fn log_msg(context: &Context, prefix: &str, msg: &Message) {
1061
1095
_ => "" ,
1062
1096
} ;
1063
1097
let msgtext = msg. get_text ( ) ;
1064
- println ! (
1098
+ writeln ! (
1099
+ buf,
1065
1100
"{}{}{}{}: {} (Contact#{}): {} {}{}{}{}{}" ,
1066
1101
prefix,
1067
1102
msg. get_id( ) ,
@@ -1095,7 +1130,8 @@ async fn log_msg(context: &Context, prefix: &str, msg: &Message) {
1095
1130
""
1096
1131
} ,
1097
1132
statestr,
1098
- ) ;
1133
+ )
1134
+ . unwrap ( ) ;
1099
1135
}
1100
1136
1101
1137
#[ cfg( test) ]
0 commit comments