File tree Expand file tree Collapse file tree 3 files changed +40
-28
lines changed
Expand file tree Collapse file tree 3 files changed +40
-28
lines changed Original file line number Diff line number Diff line change 1+ use std:: process:: { Child , Command } ;
2+
3+ pub struct ChildGuard {
4+ child : Child ,
5+ }
6+
7+ impl ChildGuard {
8+ pub fn spawn ( cmd : & mut Command ) -> std:: io:: Result < Self > {
9+ let child = cmd. spawn ( ) ?;
10+ Ok ( Self { child } )
11+ }
12+ }
13+
14+ impl Drop for ChildGuard {
15+ fn drop ( & mut self ) {
16+ match self . child . try_wait ( ) {
17+ Ok ( Some ( _) ) => {
18+ // Already exited, nothing to do
19+ }
20+ Ok ( None ) => {
21+ if let Err ( e) = self . child . kill ( ) {
22+ eprintln ! ( "Failed to kill child process: {}" , e) ;
23+ }
24+ if let Err ( e) = self . child . wait ( ) {
25+ eprintln ! ( "Failed to wait on child process: {}" , e) ;
26+ }
27+ }
28+ Err ( e) => {
29+ eprintln ! ( "Failed to check child process status: {}" , e) ;
30+ }
31+ }
32+ }
33+ }
Original file line number Diff line number Diff line change 11mod test_env;
2- pub mod os;
2+ mod os;
3+ pub mod guard;
34
45pub use test_env:: TestEnv ;
Original file line number Diff line number Diff line change 11mod common;
22
3- use std:: env:: current_dir;
4- use std:: path:: PathBuf ;
53use std:: process:: Command ;
64
75use crate :: common:: TestEnv ;
86use predicates:: str:: contains;
7+ use crate :: common:: guard:: ChildGuard ;
98
109#[ test]
1110fn hello ( ) {
@@ -20,35 +19,14 @@ fn hello() {
2019 let mut cmd = Command :: new ( icp_path) ;
2120 cmd. env ( "HOME" , testenv. home_path ( ) ) ;
2221
23- let mut child = cmd
22+ let mut cmd = cmd
2423 . current_dir ( icp_project_dir)
2524 . arg ( "network" )
26- . arg ( "run" )
27- . spawn ( )
25+ . arg ( "run" ) ;
26+ let _child_guard = ChildGuard :: spawn ( cmd )
2827 . expect ( "failed to spawn icp network" ) ;
2928
30- struct ChildGuard {
31- child : std:: process:: Child ,
32- }
33- impl ChildGuard {
34- fn new ( child : std:: process:: Child ) -> Self {
35- Self { child }
36- }
37- }
38- impl Drop for ChildGuard {
39- fn drop ( & mut self ) {
40- if let Err ( e) = self . child . kill ( ) {
41- eprintln ! ( "Failed to kill child process: {}" , e) ;
42- }
43- if let Some ( code) = self . child . wait ( ) . ok ( ) . and_then ( |status| status. code ( ) ) {
44- eprintln ! ( "Child process exited with code: {}" , code) ;
45- } else {
46- eprintln ! ( "Child process terminated unexpectedly" ) ;
47- }
48- }
49- }
50-
51- let _child_guard = ChildGuard :: new ( child) ;
29+
5230
5331 // configure the network for dfx
5432 testenv. configure_dfx_local_network ( ) ;
You can’t perform that action at this time.
0 commit comments