File tree 1 file changed +20
-0
lines changed 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,9 @@ use winapi::um::winnt::PROCESS_TERMINATE;
8
8
pub ( crate ) fn kill_process ( id : u32 ) -> Fallible < ( ) > {
9
9
unsafe {
10
10
let handle = OpenProcess ( PROCESS_TERMINATE , 0 , id) ;
11
+ if handle. is_null ( ) {
12
+ bail ! ( "OpenProcess for process {} failed" , id) ;
13
+ }
11
14
if TerminateProcess ( handle, 101 ) == 0 {
12
15
bail ! ( "TerminateProcess for process {} failed" , id) ;
13
16
}
@@ -45,3 +48,20 @@ pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> Fallible<()> {
45
48
failure:: bail!( "Downloaded binaries should be executable by default" ) ;
46
49
}
47
50
}
51
+
52
+
53
+ #[ cfg( test) ]
54
+ mod tests {
55
+ use super :: * ;
56
+ use std:: process:: Command ;
57
+
58
+ #[ test]
59
+ fn test_kill_process ( ) {
60
+ // Try to kill a sleep command
61
+ let mut cmd = Command :: new ( "timeout" ) . args ( & [ "2" ] ) . spawn ( ) . unwrap ( ) ;
62
+ kill_process ( cmd. id ( ) ) . unwrap ( ) ;
63
+
64
+ // Ensure it returns the code passed to `TerminateProcess`
65
+ assert_eq ! ( cmd. wait( ) . unwrap( ) . code( ) , Some ( 101 ) ) ;
66
+ }
67
+ }
You can’t perform that action at this time.
0 commit comments