@@ -25,6 +25,7 @@ pub mod pretty_print;
2525
2626/// Represents the result of running a command, including its standard output,
2727/// standard error, and exit code.
28+ #[ derive( Debug ) ]
2829pub struct CommandResult {
2930 /// The standard output (stdout) of the command as a string.
3031 pub stdout : String ,
@@ -436,3 +437,106 @@ pub fn replace_fuzz_binary_name(cmd: &str, result: &mut CommandResult) {
436437 result. stdout = result. stdout . replace ( & fuzz_bin_name, cmd) ;
437438 result. stderr = result. stderr . replace ( & fuzz_bin_name, cmd) ;
438439}
440+
441+ #[ cfg( test) ]
442+ mod tests {
443+ use super :: * ;
444+ use std:: ffi:: OsString ;
445+
446+ #[ test]
447+ fn test_command_result_creation ( ) {
448+ let result = CommandResult {
449+ stdout : "Hello, world!" . to_string ( ) ,
450+ stderr : "" . to_string ( ) ,
451+ exit_code : 0 ,
452+ } ;
453+
454+ assert_eq ! ( result. stdout, "Hello, world!" ) ;
455+ assert_eq ! ( result. stderr, "" ) ;
456+ assert_eq ! ( result. exit_code, 0 ) ;
457+ }
458+
459+ #[ test]
460+ fn test_generate_random_string ( ) {
461+ let result = generate_random_string ( 10 ) ;
462+ // Check character count, not byte count (emojis are multi-byte)
463+ assert ! ( result. chars( ) . count( ) <= 10 ) ;
464+
465+ // Test that empty string can be generated (max_length = 0)
466+ let empty_result = generate_random_string ( 0 ) ;
467+ assert_eq ! ( empty_result. chars( ) . count( ) , 0 ) ;
468+ }
469+
470+ #[ test]
471+ fn test_replace_fuzz_binary_name ( ) {
472+ let mut result = CommandResult {
473+ stdout : "fuzz/target/x86_64-unknown-linux-gnu/release/fuzz_echo: error" . to_string ( ) ,
474+ stderr : "fuzz/target/x86_64-unknown-linux-gnu/release/fuzz_echo failed" . to_string ( ) ,
475+ exit_code : 1 ,
476+ } ;
477+
478+ replace_fuzz_binary_name ( "echo" , & mut result) ;
479+
480+ assert_eq ! ( result. stdout, "echo: error" ) ;
481+ assert_eq ! ( result. stderr, "echo failed" ) ;
482+ assert_eq ! ( result. exit_code, 1 ) ;
483+ }
484+
485+ #[ test]
486+ fn test_run_gnu_cmd_nonexistent ( ) {
487+ let args = vec ! [ OsString :: from( "--version" ) ] ;
488+ let result = run_gnu_cmd ( "nonexistent_command_12345" , & args, false , None ) ;
489+
490+ // Should return an error since the command doesn't exist
491+ assert ! ( result. is_err( ) ) ;
492+ let error_result = result. unwrap_err ( ) ;
493+ assert_ne ! ( error_result. exit_code, 0 ) ;
494+ }
495+
496+ #[ test]
497+ fn test_run_gnu_cmd_basic ( ) {
498+ // Test with a simple command that should exist on most systems
499+ let args = vec ! [ OsString :: from( "--version" ) ] ;
500+ let result = run_gnu_cmd ( "echo" , & args, false , None ) ;
501+
502+ // Should succeed (echo --version might not be standard but echo should exist)
503+ match result {
504+ Ok ( _) => { } // Command succeeded
505+ Err ( err_result) => {
506+ // Command failed but at least ran
507+ assert_ne ! ( err_result. exit_code, -1 ) ; // -1 would indicate the command couldn't be found
508+ }
509+ }
510+ }
511+
512+ #[ test]
513+ fn test_run_gnu_cmd_with_pipe_input ( ) {
514+ let args: Vec < OsString > = vec ! [ ] ;
515+ let pipe_input = "hello world" ;
516+ let result = run_gnu_cmd ( "cat" , & args, false , Some ( pipe_input) ) ;
517+
518+ match result {
519+ Ok ( cmd_result) => {
520+ assert_eq ! ( cmd_result. stdout. trim( ) , "hello world" ) ;
521+ }
522+ Err ( _) => {
523+ // cat might not be available in test environment, that's ok
524+ }
525+ }
526+ }
527+
528+ #[ test]
529+ fn test_generate_random_file ( ) {
530+ let result = generate_random_file ( ) ;
531+ match result {
532+ Ok ( file_path) => {
533+ assert ! ( !file_path. is_empty( ) ) ;
534+ // Clean up - try to remove the file
535+ let _ = std:: fs:: remove_file ( & file_path) ;
536+ }
537+ Err ( _) => {
538+ // File creation might fail due to permissions, that's acceptable for this test
539+ }
540+ }
541+ }
542+ }
0 commit comments