Skip to content

Commit 11b567f

Browse files
author
Eric Swanson
committed
checkpoint
1 parent 4e1ba7d commit 11b567f

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

bin/icp-cli/tests/common/guard.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

bin/icp-cli/tests/common/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod test_env;
2-
pub mod os;
2+
mod os;
3+
pub mod guard;
34

45
pub use test_env::TestEnv;

bin/icp-cli/tests/network_tests.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
mod common;
22

3-
use std::env::current_dir;
4-
use std::path::PathBuf;
53
use std::process::Command;
64

75
use crate::common::TestEnv;
86
use predicates::str::contains;
7+
use crate::common::guard::ChildGuard;
98

109
#[test]
1110
fn 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();

0 commit comments

Comments
 (0)