33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6+ use console:: Style ;
67use libc:: STDIN_FILENO ;
78use libc:: { close, dup, dup2, pipe, STDERR_FILENO , STDOUT_FILENO } ;
9+ use pretty_print:: {
10+ print_diff, print_end_with_status, print_or_empty, print_section, print_with_style,
11+ } ;
812use rand:: prelude:: IndexedRandom ;
913use rand:: Rng ;
10- use similar:: TextDiff ;
1114use std:: env:: temp_dir;
1215use std:: ffi:: OsString ;
1316use std:: fs:: File ;
@@ -18,6 +21,8 @@ use std::sync::atomic::Ordering;
1821use std:: sync:: { atomic:: AtomicBool , Once } ;
1922use std:: { io, thread} ;
2023
24+ pub mod pretty_print;
25+
2126/// Represents the result of running a command, including its standard output,
2227/// standard error, and exit code.
2328pub struct CommandResult {
@@ -315,8 +320,8 @@ pub fn compare_result(
315320 gnu_result : & CommandResult ,
316321 fail_on_stderr_diff : bool ,
317322) {
318- println ! ( "Test Type : {}" , test_type) ;
319- println ! ( "Input: {}" , input ) ;
323+ print_section ( format ! ( "Compare result for : {} {} " , test_type, input ) ) ;
324+
320325 if let Some ( pipe) = pipe_input {
321326 println ! ( "Pipe: {}" , pipe) ;
322327 }
@@ -326,49 +331,58 @@ pub fn compare_result(
326331
327332 if rust_result. stdout . trim ( ) != gnu_result. stdout . trim ( ) {
328333 discrepancies. push ( "stdout differs" ) ;
329- println ! ( "Rust stdout: {}" , rust_result. stdout) ;
330- println ! ( "GNU stdout: {}" , gnu_result. stdout) ;
334+ println ! ( "Rust stdout:" , ) ;
335+ print_or_empty ( rust_result. stdout . as_str ( ) ) ;
336+ println ! ( "GNU stdout:" , ) ;
337+ print_or_empty ( gnu_result. stdout . as_ref ( ) ) ;
331338 print_diff ( & rust_result. stdout , & gnu_result. stdout ) ;
332339 should_panic = true ;
333340 }
341+
334342 if rust_result. stderr . trim ( ) != gnu_result. stderr . trim ( ) {
335343 discrepancies. push ( "stderr differs" ) ;
336- println ! ( "Rust stderr: {}" , rust_result. stderr) ;
337- println ! ( "GNU stderr: {}" , gnu_result. stderr) ;
344+ println ! ( "Rust stderr:" , ) ;
345+ print_or_empty ( rust_result. stderr . as_str ( ) ) ;
346+ println ! ( "GNU stderr:" , ) ;
347+ print_or_empty ( gnu_result. stderr . as_str ( ) ) ;
338348 print_diff ( & rust_result. stderr , & gnu_result. stderr ) ;
339349 if fail_on_stderr_diff {
340350 should_panic = true ;
341351 }
342352 }
353+
343354 if rust_result. exit_code != gnu_result. exit_code {
344355 discrepancies. push ( "exit code differs" ) ;
345- println ! ( "Rust exit code: {}" , rust_result. exit_code) ;
346- println ! ( "GNU exit code: {}" , gnu_result. exit_code) ;
356+ println ! (
357+ "Different exit code: (Rust: {}, GNU: {})" ,
358+ rust_result. exit_code, gnu_result. exit_code
359+ ) ;
347360 should_panic = true ;
348361 }
349362
350363 if discrepancies. is_empty ( ) {
351- println ! ( "All outputs and exit codes matched." ) ;
364+ print_end_with_status ( "Same behavior" , true ) ;
352365 } else {
353- println ! ( "Discrepancy detected: {}" , discrepancies. join( ", " ) ) ;
366+ print_with_style (
367+ format ! ( "Discrepancies detected: {}" , discrepancies. join( ", " ) ) ,
368+ Style :: new ( ) . red ( ) ,
369+ ) ;
354370 if should_panic {
355- panic ! ( "Test failed for {}: {}" , test_type, input) ;
371+ print_end_with_status (
372+ format ! ( "Test failed and will panic for: {} {}" , test_type, input) ,
373+ false ,
374+ ) ;
375+ panic ! ( "Test failed for: {} {}" , test_type, input) ;
356376 } else {
357- println ! (
358- "Test completed with discrepancies for {}: {}" ,
359- test_type, input
377+ print_end_with_status (
378+ format ! (
379+ "Test completed with discrepancies for: {} {}" ,
380+ test_type, input
381+ ) ,
382+ false ,
360383 ) ;
361384 }
362385 }
363- }
364-
365- /// When we have different outputs, print the diff
366- fn print_diff ( rust_output : & str , gnu_output : & str ) {
367- println ! ( "Diff=" ) ;
368- let diff = TextDiff :: from_lines ( rust_output, gnu_output) ;
369- for change in diff. iter_all_changes ( ) {
370- print ! ( "{}{}" , change. tag( ) , change) ;
371- }
372386 println ! ( ) ;
373387}
374388
@@ -414,3 +428,10 @@ pub fn generate_random_file() -> Result<String, std::io::Error> {
414428
415429 Ok ( file_path. to_str ( ) . unwrap ( ) . to_string ( ) )
416430}
431+
432+ pub fn replace_fuzz_binary_name ( cmd : & str , result : & mut CommandResult ) {
433+ let fuzz_bin_name = format ! ( "fuzz/target/x86_64-unknown-linux-gnu/release/fuzz_{cmd}" ) ;
434+
435+ result. stdout = result. stdout . replace ( & fuzz_bin_name, cmd) ;
436+ result. stderr = result. stderr . replace ( & fuzz_bin_name, cmd) ;
437+ }
0 commit comments