@@ -12,15 +12,21 @@ use std::time::Instant;
1212
1313#[ test]
1414fn test_exit_code_for_empty_arguments ( ) {
15+ // Executing Bear with no arguments should return a non-zero exit code,
16+ // and print usage information.
1517 Command :: cargo_bin ( BEAR_BIN )
1618 . unwrap ( )
19+ . env ( "RUST_LOG" , "debug" )
20+ . env ( "RUST_BACKTRACE" , "1" )
1721 . assert ( )
1822 . failure ( )
1923 . stderr ( predicate:: str:: contains ( "Usage: bear" ) ) ;
2024}
2125
2226#[ test]
2327fn test_exit_code_for_help ( ) {
28+ // Executing help and subcommand help should always has zero exit code,
29+ // and print out usage information
2430 Command :: cargo_bin ( BEAR_BIN )
2531 . unwrap ( )
2632 . arg ( "--help" )
@@ -46,70 +52,93 @@ fn test_exit_code_for_help() {
4652}
4753
4854#[ test]
49- fn test_exit_code_for_invalid_command ( ) {
55+ fn test_exit_code_for_invalid_argument ( ) {
56+ // Executing Bear with an invalid argument should always has non-zero exit code,
57+ // and print relevant information about the reason about the failure.
5058 Command :: cargo_bin ( BEAR_BIN )
5159 . unwrap ( )
52- . arg ( "invalid_command" )
60+ . arg ( "invalid_argument" )
61+ . env ( "RUST_LOG" , "debug" )
62+ . env ( "RUST_BACKTRACE" , "1" )
5363 . assert ( )
5464 . failure ( )
5565 . stderr ( predicate:: str:: contains ( "error: unexpected argument" ) ) ;
5666}
5767
5868#[ test]
69+ #[ cfg( target_os = "linux" ) ] // FIXME: compiler wrappers does not work yet
70+ fn test_exit_code_for_non_existing_command ( ) {
71+ // Executing a non-existing command should always has non-zero exit code,
72+ // and print relevant information about the reason about the failure.
73+ Command :: cargo_bin ( BEAR_BIN )
74+ . unwrap ( )
75+ . args ( [ "--" , "invalid_command" ] )
76+ . env ( "RUST_LOG" , "debug" )
77+ . env ( "RUST_BACKTRACE" , "1" )
78+ . assert ( )
79+ . failure ( )
80+ . stderr ( predicate:: str:: contains (
81+ "Failed to execute the build command" ,
82+ ) ) ;
83+ }
84+
85+ #[ test]
86+ #[ cfg( target_os = "linux" ) ] // FIXME: compiler wrappers does not work yet
5987#[ cfg( has_executable_true) ]
6088fn test_exit_code_for_true ( ) {
89+ // When the executed command returns successfully, Bear exit code should be zero.
6190 Command :: cargo_bin ( BEAR_BIN )
6291 . unwrap ( )
6392 . arg ( "--" )
6493 . arg ( TRUE_PATH )
94+ . env ( "RUST_LOG" , "debug" )
95+ . env ( "RUST_BACKTRACE" , "1" )
6596 . assert ( )
6697 . success ( ) ;
6798}
6899
69100#[ test]
70101#[ cfg( has_executable_false) ]
71102fn test_exit_code_for_false ( ) {
103+ // When the executed command returns unsuccessfully, Bear exit code should be non-zero.
72104 Command :: cargo_bin ( BEAR_BIN )
73105 . unwrap ( )
74106 . arg ( "--" )
75107 . arg ( FALSE_PATH )
108+ . env ( "RUST_LOG" , "debug" )
109+ . env ( "RUST_BACKTRACE" , "1" )
76110 . assert ( )
77111 . failure ( ) ;
78112}
79113
80114#[ test]
81115#[ cfg( has_executable_sleep) ]
82116fn test_exit_code_when_signaled ( ) {
83- // Prepare the command
117+ // When the bear process is signaled, Bear exit code should be non-zero.
118+ // And should terminate the child process and return immediately.
119+
84120 let mut cmd = StdCommand :: new ( cargo_bin ( BEAR_BIN ) ) ;
85121 cmd. arg ( "--" )
86122 . arg ( SLEEP_PATH )
87123 . arg ( "10" )
124+ . env ( "RUST_LOG" , "debug" )
125+ . env ( "RUST_BACKTRACE" , "1" )
88126 . stdout ( Stdio :: null ( ) )
89127 . stderr ( Stdio :: null ( ) ) ;
90128
91- // Start the command
92129 let mut child = cmd. spawn ( ) . expect ( "Failed to spawn command" ) ;
93130
94131 // Wait 200ms to ensure that the sleep command was also executed
95132 std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 200 ) ) ;
96133
97- // Send a termination signal to the process and record the time
98134 let kill_time = Instant :: now ( ) ;
99135 child. kill ( ) . expect ( "Failed to signal the process" ) ;
100-
101- // Wait for the process to complete and record the time
102136 let status = child. wait ( ) . expect ( "Failed to wait for command" ) ;
103137 let wait_end = Instant :: now ( ) ;
104138
105- // Assert that the exit status is not zero
106139 assert ! ( !status. success( ) ) ;
107-
108- // Assert that the process stopped right after the kill call (less than 1 second)
109- let time_diff = wait_end. duration_since ( kill_time) ;
110140 assert ! (
111- time_diff. as_secs( ) < 1 ,
112- "Process took too long to terminate: {:?}" ,
113- time_diff
141+ wait_end. duration_since( kill_time) . as_secs( ) < 1 ,
142+ "Process took too long to terminate." ,
114143 ) ;
115144}
0 commit comments