Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ edition = "2021"
nom = "7.0"
approx = "0.5.1"
once_cell = "1.10"
[target.'cfg(not(windows))'.dependencies]
nix = { version = "0.29", features = ["signal"] }
96 changes: 96 additions & 0 deletions src/stdlib/subprocess/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ pub fn run_shell_command(

use std::process::{Child, ExitStatus};
use std::io;
#[cfg(not(windows))]
use nix::{
sys::signal::{self, Signal},
unistd::Pid,
};

/// Struct representing a running process.
/// It wraps a `std::process::Child` and provides a wait method.
Expand All @@ -141,12 +146,35 @@ impl Processo {
let status: ExitStatus = self.processo.wait()?;
Ok(status.code().unwrap_or(-1))
}

#[cfg(windows)]
pub fn terminate(&mut self) -> io::Result<()> {
self.processo.kill()
}

#[cfg(not(windows))]
pub fn terminate(&mut self) -> io::Result<()> {
let pid = Pid::from_raw(self.processo.id() as i32);
match signal::kill(pid, Signal::SIGTERM) {
Ok(_) => Ok(()),
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
}
}

pub fn kill(&mut self) -> io::Result<()> {
self.processo.kill()
}


}


#[cfg(test)]
mod tests {
use super::*;
use std::process::Command;
use std::thread;
use std::time::Duration;

#[test]
fn test_basic_command_execution() {
Expand Down Expand Up @@ -552,5 +580,73 @@ mod tests {
let exit_code = processo.wait().unwrap();
assert_eq!(exit_code, -1); // May vary by OS
}
// Terminate tests
fn create_long_running_command() -> Command {
if cfg!(windows) {
let mut cmd = Command::new("timeout");
cmd.arg("/T").arg("30");
cmd
} else {
let mut cmd = Command::new("sleep");
cmd.arg("30");
cmd
}
}

#[test]
fn test_terminate_a_running_process() {
let child = create_long_running_command()
.spawn()
.expect("Falha ao iniciar processo para o teste de terminate");

let mut processo = Processo { processo: child };

thread::sleep(Duration::from_millis(100));

processo.terminate().expect("Falha ao chamar terminate");

let exit_code = processo.wait().expect("Falha ao esperar pelo processo terminado");

let expected_code = if cfg!(windows) { 1 } else { -1 };
assert_eq!(exit_code, expected_code, "O código de saída após terminate não foi o esperado.");
}

#[test]
fn test_kill_a_running_process() {
let child = create_long_running_command()
.spawn()
.expect("Falha ao iniciar processo para o teste de kill");


let mut processo = Processo { processo: child };

thread::sleep(Duration::from_millis(100));

processo.kill().expect("Falha ao chamar kill");


let exit_code = processo.wait().expect("Falha ao esperar pelo processo morto");

let expected_code = if cfg!(windows) { 1 } else { -1 };
assert_eq!(exit_code, expected_code, "O código de saída após kill não foi o esperado.");
}

#[test]
fn test_wait_on_a_process_that_finishes_normally() {
let mut command = if cfg!(windows) {
let mut cmd = Command::new("timeout");
cmd.arg("/T").arg("1");
cmd
} else {
let mut cmd = Command::new("sleep");
cmd.arg("1");
cmd
};

let child = command.spawn().expect("Falha ao iniciar processo curto");
let mut processo = Processo { processo: child };

let exit_code = processo.wait().expect("Falha ao esperar pelo processo");
assert_eq!(exit_code, 0);
}
}