From adfa7c6b295521a6f8fdf4084993a80a8030e49b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 19 Jun 2026 23:00:38 +0900 Subject: test(command): add basic smoke tests --- crates/shirabe/src/console/application.rs | 45 ++++++++++--------------------- crates/shirabe/src/lib.rs | 20 ++++++++++++++ crates/shirabe/src/main.rs | 29 +++++++------------- 3 files changed, 43 insertions(+), 51 deletions(-) (limited to 'crates/shirabe/src') diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index 591114a..82aa03d 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -127,7 +127,6 @@ pub struct Application { version: String, command_loader: Option>, catch_exceptions: bool, - auto_exit: bool, definition: Option>>, helper_set: Option>>, terminal: Terminal, @@ -201,7 +200,6 @@ impl Application { version, command_loader: None, catch_exceptions: true, - auto_exit: true, definition: None, helper_set: None, terminal: Terminal::new(), @@ -318,7 +316,7 @@ impl Application { application: &std::rc::Rc>, input: Option>>, output: Option>>, - ) -> anyhow::Result { + ) -> anyhow::Result { let output = match output { Some(output) => Some(output), None => Some( @@ -334,7 +332,7 @@ impl Application { application: &std::rc::Rc>, input: std::rc::Rc>, output: std::rc::Rc>, - ) -> anyhow::Result { + ) -> anyhow::Result { application.borrow_mut().disable_plugins_by_default = input .borrow() .has_parameter_option(PhpMixed::from(vec!["--no-plugins"]), false); @@ -907,7 +905,7 @@ impl Application { } let mut start_time: Option = None; - let result_outcome: anyhow::Result = (|| -> anyhow::Result { + let result_outcome: anyhow::Result = (|| -> anyhow::Result { if input .borrow() .has_parameter_option(PhpMixed::from(vec!["--profile"]), false) @@ -921,7 +919,7 @@ impl Application { let _ = start_time.unwrap(); } - let result: i64 = Application::base_do_run(application, input.clone(), output.clone())?; + let result = Application::base_do_run(application, input.clone(), output.clone())?; if input .borrow() @@ -975,7 +973,7 @@ impl Application { ); } - Ok(see.get_code()) + Ok(see.get_code() as i32) } else { let mut ghe = GithubActionError::new(io.clone()); ghe.emit(&e.to_string(), None, None); @@ -1443,7 +1441,7 @@ impl Application { application: &std::rc::Rc>, input: Option>>, output: Option>>, - ) -> anyhow::Result { + ) -> anyhow::Result { if shirabe_php_shim::function_exists("putenv") { let (height, width) = { let app = application.borrow(); @@ -1479,7 +1477,7 @@ impl Application { this.render_throwable(e, output.clone()); }; - let result = (|| -> anyhow::Result { + let result = (|| -> anyhow::Result { application.borrow_mut().configure_io(&input, &output)?; let exit_code = Application::do_run(application, input.clone(), output.clone())?; @@ -1487,7 +1485,7 @@ impl Application { Ok(exit_code) })(); - let mut exit_code = match result { + let exit_code = match result { Ok(exit_code) => exit_code, Err(e) => { if !application.borrow().catch_exceptions { @@ -1512,14 +1510,6 @@ impl Application { // finally: handler restore. See TODO above; no-op here. - if application.borrow().auto_exit { - if exit_code > 255 { - exit_code = 255; - } - - shirabe_php_shim::exit(exit_code); - } - Ok(exit_code) } @@ -1528,7 +1518,7 @@ impl Application { application: &std::rc::Rc>, input: std::rc::Rc>, output: std::rc::Rc>, - ) -> anyhow::Result { + ) -> anyhow::Result { if input.borrow().has_parameter_option( PhpMixed::from(vec![ PhpMixed::from("--version".to_string()), @@ -1801,16 +1791,6 @@ impl Application { self.catch_exceptions = boolean; } - /// Gets whether to automatically exit after a command execution or not. - pub fn is_auto_exit_enabled(&self) -> bool { - self.auto_exit - } - - /// Sets whether to automatically exit after a command execution or not. - pub fn set_auto_exit(&mut self, boolean: bool) { - self.auto_exit = boolean; - } - /// Gets the name of the application. pub fn get_name(&self) -> String { self.name.clone() @@ -2545,7 +2525,7 @@ impl Application { command: std::rc::Rc>, input: std::rc::Rc>, output: std::rc::Rc>, - ) -> anyhow::Result { + ) -> anyhow::Result { if let Some(helper_set) = command.borrow().get_helper_set() { for (_alias, helper) in helper_set.borrow().get_iterator() { // if ($helper instanceof InputAwareInterface) $helper->setInput($input); @@ -2588,7 +2568,10 @@ impl Application { } } - command.borrow_mut().run(input.clone(), output.clone()) + command + .borrow_mut() + .run(input.clone(), output.clone()) + .map(|c| c as i32) } /// Gets the name of the command based on input. diff --git a/crates/shirabe/src/lib.rs b/crates/shirabe/src/lib.rs index 1e17b72..4bd52c2 100644 --- a/crates/shirabe/src/lib.rs +++ b/crates/shirabe/src/lib.rs @@ -24,3 +24,23 @@ pub mod repository; pub mod script; pub mod self_update; pub mod util; + +pub fn run(argv: Vec) -> anyhow::Result { + use crate::console::Application; + use crate::util::Platform; + use shirabe_external_packages::symfony::console::input::argv_input::ArgvInput; + use shirabe_external_packages::symfony::console::input::input_interface::InputInterface; + + // TODO(php-runtime): the full initialization process in composer/bin/composer should be ported + // somewhere else that communicates with the real PHP runtime. + Platform::put_env( + "COMPOSER_BINARY", + &shirabe_php_shim::realpath(argv.first().map(String::as_str).unwrap_or_default()) + .unwrap_or_default(), + ); + + let application = Application::new_shared("Composer".to_string(), String::new())?; + let input = std::rc::Rc::new(std::cell::RefCell::new(ArgvInput::new(Some(argv), None)?)) + as std::rc::Rc>; + Application::run(&application, Some(input), None) +} diff --git a/crates/shirabe/src/main.rs b/crates/shirabe/src/main.rs index f179c52..3e3623d 100644 --- a/crates/shirabe/src/main.rs +++ b/crates/shirabe/src/main.rs @@ -1,30 +1,19 @@ //! ref: composer/bin/composer -use shirabe::console::Application; -use shirabe::util::Platform; -use shirabe_php_shim::{realpath, run_shutdown_functions}; +use shirabe_php_shim::run_shutdown_functions; fn main() { - // TODO(php-runtime): the full initialization process in composer/bin/composer should be ported - // somewhere else that communicates with the real PHP runtime. - - Platform::put_env( - "COMPOSER_BINARY", - &realpath(&std::env::args().next().unwrap_or_default()).unwrap_or_default(), - ); - - // run the command application - let application = match Application::new_shared("Composer".to_string(), String::new()) { - Ok(application) => application, + let result = shirabe::run(std::env::args().collect()); + run_shutdown_functions(); + let mut exit_code = match result { + Ok(exit_code) => exit_code, Err(e) => { eprintln!("{}", e); - std::process::exit(1); + 1 } }; - let result = Application::run(&application, None, None); - run_shutdown_functions(); - if let Err(e) = result { - eprintln!("{}", e); - std::process::exit(1); + if exit_code > 255 { + exit_code = 255; } + std::process::exit(exit_code); } -- cgit v1.3.1