@@ -3,81 +3,45 @@ use super::*;
33#[ cfg( unix) ]
44#[ test]
55fn test_native_abort_trace ( ) {
6+ const ANSI_LINE_NUMBER_REGEX : & str = r"(?<redacted>:[0-9]+)(?:(?:\x1b\[[0-9;]*m)|(?:\[ANSI_[A-Z]+\]))*(?:[ \t]+(?:(?:\x1b\[[0-9;]*m)|(?:\[ANSI_[A-Z]+\]))*(?:at|by)|\n|$)" ;
67 let dir = TestDir :: new ( "native_abort_trace/native_abort_trace.in" ) ;
7-
8- fn strip_ansi ( input : & str ) -> String {
9- let mut out = String :: with_capacity ( input. len ( ) ) ;
10- let mut chars = input. chars ( ) . peekable ( ) ;
11- while let Some ( c) = chars. next ( ) {
12- if c == '\x1b' {
13- if chars. peek ( ) == Some ( & '[' ) {
14- chars. next ( ) ;
15- for c2 in chars. by_ref ( ) {
16- if c2 == 'm' {
17- break ;
18- }
19- }
20- }
21- continue ;
22- }
23- out. push ( c) ;
24- }
25- out
26- }
27-
28- fn normalize_line_numbers ( input : & str ) -> String {
29- let mut out = String :: with_capacity ( input. len ( ) ) ;
30- for line in input. lines ( ) {
31- let Some ( pos) = line. find ( "main.c:" ) else {
32- out. push_str ( line) ;
33- out. push ( '\n' ) ;
34- continue ;
35- } ;
36- let prefix = & line[ ..pos] ;
37- let rest = & line[ pos + "main.c:" . len ( ) ..] ;
38- let mut digits_len = 0 ;
39- for ch in rest. chars ( ) {
40- if ch. is_ascii_digit ( ) {
41- digits_len += ch. len_utf8 ( ) ;
42- } else {
43- break ;
44- }
45- }
46- if digits_len == 0 {
47- out. push_str ( line) ;
48- out. push ( '\n' ) ;
49- continue ;
50- }
51- let suffix = & rest[ digits_len..] ;
52- out. push_str ( prefix) ;
53- out. push_str ( "main.c:<line>" ) ;
54- out. push_str ( suffix) ;
55- out. push ( '\n' ) ;
56- }
57- out
58- }
59-
60- let output = snapbox:: cmd:: Command :: new ( moon_bin ( ) )
8+ let mut redactions = moon_test_util:: stack_trace:: stack_trace_redactions ( dir. as_ref ( ) ) ;
9+ redactions
10+ . insert ( "[ANSI_RED]" , "\u{1b} [31m" )
11+ . expect ( "valid ANSI red redaction" ) ;
12+ redactions
13+ . insert ( "[ANSI_GRAY]" , "\u{1b} [90m" )
14+ . expect ( "valid ANSI gray redaction" ) ;
15+ redactions
16+ . insert ( "[ANSI_CYAN]" , "\u{1b} [36m" )
17+ . expect ( "valid ANSI cyan redaction" ) ;
18+ redactions
19+ . insert ( "[ANSI_BOLD]" , "\u{1b} [1m" )
20+ . expect ( "valid ANSI bold redaction" ) ;
21+ redactions
22+ . insert ( "[ANSI_RESET]" , "\u{1b} [0m" )
23+ . expect ( "valid ANSI reset redaction" ) ;
24+ redactions
25+ . insert (
26+ "[LINE_NUMBER]" ,
27+ moon_test_util:: stack_trace:: redaction_regex ( ANSI_LINE_NUMBER_REGEX ) ,
28+ )
29+ . expect ( "valid ANSI stack trace line number redaction" ) ;
30+ snapbox:: cmd:: Command :: new ( moon_bin ( ) )
31+ . with_assert ( snapbox:: Assert :: new ( ) . redact_with ( redactions) )
6132 . current_dir ( & dir)
33+ . env ( "NO_COLOR" , "1" )
34+ . env ( "CLICOLOR" , "0" )
6235 . args ( [ "run" , "--target" , "native" , "cmd/main" ] )
6336 . assert ( )
6437 . success ( )
65- . get_output ( )
66- . to_owned ( ) ;
67-
68- let mut out = String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ;
69- out. push_str ( & String :: from_utf8_lossy ( & output. stderr ) ) ;
70- let out = replace_dir ( & normalize_line_numbers ( & strip_ansi ( & out) ) , & dir) ;
38+ . stdout_eq ( "Hello\n " )
39+ . stderr_eq ( snapbox:: str![ [ r#"
40+ [ANSI_RED]RUNTIME ERROR: abort() called[ANSI_RESET]
41+ [ANSI_GRAY][CORE_PATH]/builtin/option.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]at[ANSI_RESET] [ANSI_BOLD]@moonbitlang/core/option.Option::unwrap[Int][ANSI_RESET]
42+ [ANSI_GRAY][..]/cmd/main/main.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]by[ANSI_RESET] [ANSI_BOLD]@username/scratch/cmd/main.g[ANSI_RESET]
43+ [ANSI_GRAY][..]/cmd/main/main.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]by[ANSI_RESET] [ANSI_BOLD]@username/scratch/cmd/main.f[ANSI_RESET]
44+ [ANSI_GRAY][..]/cmd/main/main.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]by[ANSI_RESET] [ANSI_BOLD]main[ANSI_RESET]
7145
72- check (
73- & out,
74- expect ! [ [ r#"
75- Hello
76- RUNTIME ERROR: abort() called
77- $MOON_HOME/lib/core/builtin/option.mbt:39 at @moonbitlang/core/option.Option::unwrap[Int]
78- $ROOT/cmd/main/main.mbt:14 by @username/scratch/cmd/main.g
79- $ROOT/cmd/main/main.mbt:9 by @username/scratch/cmd/main.f
80- $ROOT/cmd/main/main.mbt:4 by main
81- "# ] ] ,
82- ) ;
46+ "# ] ] ) ;
8347}
0 commit comments