From 6f0dc57fefcea77e26160cb2b42b1e3c4fc0042e Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 4 Aug 2023 20:29:15 +0800 Subject: [PATCH] feat: run Argcfile.sh without child process (#222) --- Cargo.lock | 29 ----------------------------- Cargo.toml | 1 - src/bin/argc/main.rs | 34 +++++++++++++++------------------- tests/interrupt.rs | 34 ---------------------------------- tests/tests.rs | 3 --- 5 files changed, 15 insertions(+), 86 deletions(-) delete mode 100644 tests/interrupt.rs diff --git a/Cargo.lock b/Cargo.lock index 9494882b..dd495f19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,6 @@ dependencies = [ "assert_fs", "base64", "convert_case", - "ctrlc", "dirs", "either", "indexmap", @@ -149,16 +148,6 @@ dependencies = [ "unicode-segmentation", ] -[[package]] -name = "ctrlc" -version = "3.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a011bbe2c35ce9c1f143b7af6f94f29a167beb4cd1d29e6740ce836f723120e" -dependencies = [ - "nix", - "windows-sys 0.48.0", -] - [[package]] name = "difflib" version = "0.4.0" @@ -503,18 +492,6 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308d96db8debc727c3fd9744aac51751243420e46edf401010908da7f8d5e57c" -[[package]] -name = "nix" -version = "0.26.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" -dependencies = [ - "bitflags 1.3.2", - "cfg-if", - "libc", - "static_assertions", -] - [[package]] name = "nom" version = "7.1.3" @@ -818,12 +795,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "syn" version = "2.0.27" diff --git a/Cargo.toml b/Cargo.toml index eb7541a5..d15181f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ either = "1.8" serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", features = ["preserve_order"] } which = "4.2" -ctrlc = "3.4" shell-words = "1.1" textwrap = "0.16" dirs = "5.0" diff --git a/src/bin/argc/main.rs b/src/bin/argc/main.rs index 896ad792..7b6b1894 100644 --- a/src/bin/argc/main.rs +++ b/src/bin/argc/main.rs @@ -8,14 +8,7 @@ use argc::{ Shell, }; use base64::{engine::general_purpose, Engine as _}; -use std::{ - collections::HashMap, - env, fs, process, - sync::{ - atomic::{AtomicBool, Ordering}, - Arc, - }, -}; +use std::{collections::HashMap, env, fs, process}; use utils::*; use which::which; @@ -132,24 +125,27 @@ fn run() -> Result { let shell = get_shell_path().ok_or_else(|| anyhow!("Shell not found"))?; let (script_dir, script_file) = get_script_path(true) .ok_or_else(|| anyhow!("Argcfile not found, try `argc --argc-help` for help."))?; - let interrupt = Arc::new(AtomicBool::new(false)); - let interrupt_me = interrupt.clone(); - ctrlc::set_handler(move || interrupt_me.store(true, Ordering::Relaxed)) - .with_context(|| "Failed to set CTRL-C handler")?; let mut envs = HashMap::new(); if let Some(cwd) = get_current_dir() { envs.insert("ARGC_PWD".to_string(), escape_shell_words(&cwd)); } - let status = process::Command::new(shell) + let mut command = process::Command::new(shell); + command .arg(&script_file) .args(&args[1..]) .current_dir(script_dir) - .envs(envs) - .status() - .with_context(|| format!("Failed to run `{}`", script_file.display()))?; - if interrupt.load(Ordering::Relaxed) { - Ok(130) - } else { + .envs(envs); + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + let err = command.exec(); + bail!("Failed to run `{err}`"); + } + #[cfg(not(unix))] + { + let status = command + .status() + .with_context(|| format!("Failed to run `{}`", script_file.display()))?; Ok(status.code().unwrap_or_default()) } } diff --git a/tests/interrupt.rs b/tests/interrupt.rs deleted file mode 100644 index 31f6be92..00000000 --- a/tests/interrupt.rs +++ /dev/null @@ -1,34 +0,0 @@ -use assert_fs::{fixture::PathChild, TempDir}; -use rstest::rstest; - -use crate::fixtures::{get_path_env_var, tmpdir, Error}; -use assert_cmd::prelude::*; -use std::{process::Command, thread, time::Duration}; - -fn kill(process_id: u32) { - unsafe { - libc::kill(process_id as i32, libc::SIGINT); - } -} - -#[rstest] -fn interrupt(tmpdir: TempDir) -> Result<(), Error> { - let path_env_var = get_path_env_var(); - - let mut child = Command::cargo_bin("argc")? - .current_dir(tmpdir.child("dir1").path()) - .env("PATH", path_env_var) - .args(["task1", "2"]) - .spawn() - .expect("argc invocation failed"); - - thread::sleep(Duration::new(1, 0)); - - kill(child.id()); - - let status = child.wait().unwrap(); - - assert_eq!(status.code(), Some(130)); - - Ok(()) -} diff --git a/tests/tests.rs b/tests/tests.rs index e0155263..d7c6ea6a 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -20,6 +20,3 @@ mod param_fn; mod spec; mod validate; mod wrap_help; - -#[cfg(unix)] -mod interrupt;