diff options
Diffstat (limited to 'crates/mozart/src/commands/run_script.rs')
| -rw-r--r-- | crates/mozart/src/commands/run_script.rs | 496 |
1 files changed, 0 insertions, 496 deletions
diff --git a/crates/mozart/src/commands/run_script.rs b/crates/mozart/src/commands/run_script.rs index ab3700d..2983ab9 100644 --- a/crates/mozart/src/commands/run_script.rs +++ b/crates/mozart/src/commands/run_script.rs @@ -451,499 +451,3 @@ fn is_composer_prefix(entry: &str) -> bool { fn is_putenv(entry: &str) -> bool { entry.starts_with("@putenv ") } - -#[cfg(test)] -mod tests { - use super::*; - use std::fs; - - fn test_console() -> Arc<Mutex<Box<dyn IoInterface>>> { - Arc::new(Mutex::new(Box::new(mozart_core::console::Console::new( - 0, false, false, false, false, - )) as Box<dyn IoInterface>)) - } - - #[test] - fn test_is_php_callback_static_method() { - assert!(is_php_callback("MyClass::myMethod")); - } - - #[test] - fn test_is_php_callback_fqn_command() { - assert!(is_php_callback("Vendor\\MyCommand")); - } - - #[test] - fn test_is_php_callback_namespaced_listener() { - assert!(is_php_callback("App\\Listeners\\PostInstall")); - } - - #[test] - fn test_is_php_callback_shell_command() { - assert!(!is_php_callback("echo hello")); - } - - #[test] - fn test_is_php_callback_at_php() { - assert!(!is_php_callback("@php script.php")); - } - - #[test] - fn test_is_script_reference() { - assert!(is_script_reference("@test")); - assert!(!is_script_reference("@php foo")); - assert!(!is_script_reference("@putenv X=1")); - assert!(!is_script_reference("@composer install")); - } - - #[test] - fn test_is_putenv() { - assert!(is_putenv("@putenv FOO=bar")); - assert!(is_putenv("@putenv FOO")); - } - - #[test] - fn test_is_php_prefix() { - assert!(is_php_prefix("@php artisan migrate")); - } - - #[test] - fn test_is_composer_prefix() { - assert!(is_composer_prefix("@composer install")); - } - - #[test] - fn test_load_scripts_array_form() { - let dir = tempfile::tempdir().unwrap(); - fs::write( - dir.path().join("composer.json"), - r#"{"name": "test/pkg", "scripts": {"test": ["echo a", "echo b"]}}"#, - ) - .unwrap(); - - let (scripts, _) = load_scripts(dir.path()).unwrap(); - let listeners = scripts.get("test").unwrap(); - assert_eq!(listeners.len(), 2); - assert_eq!(listeners[0], "echo a"); - assert_eq!(listeners[1], "echo b"); - } - - #[test] - fn test_load_scripts_string_form() { - let dir = tempfile::tempdir().unwrap(); - fs::write( - dir.path().join("composer.json"), - r#"{"name": "test/pkg", "scripts": {"test": "echo a"}}"#, - ) - .unwrap(); - - let (scripts, _) = load_scripts(dir.path()).unwrap(); - let listeners = scripts.get("test").unwrap(); - assert_eq!(listeners.len(), 1); - assert_eq!(listeners[0], "echo a"); - } - - #[test] - fn test_load_scripts_with_descriptions() { - let dir = tempfile::tempdir().unwrap(); - fs::write( - dir.path().join("composer.json"), - r#"{"name": "test/pkg", "scripts": {"test": "phpunit"}, "scripts-descriptions": {"test": "Run tests"}}"#, - ) - .unwrap(); - - let (_, descriptions) = load_scripts(dir.path()).unwrap(); - assert_eq!( - descriptions.get("test").map(|s| s.as_str()), - Some("Run tests") - ); - } - - #[test] - fn test_load_scripts_empty() { - let dir = tempfile::tempdir().unwrap(); - fs::write(dir.path().join("composer.json"), r#"{"name": "test/pkg"}"#).unwrap(); - - let (scripts, descriptions) = load_scripts(dir.path()).unwrap(); - assert!(scripts.is_empty()); - assert!(descriptions.is_empty()); - } - - #[test] - fn test_load_scripts_mixed() { - let dir = tempfile::tempdir().unwrap(); - fs::write( - dir.path().join("composer.json"), - r#"{"name": "test/pkg", "scripts": {"test": "phpunit", "post-install-cmd": ["echo installed", "echo done"]}}"#, - ) - .unwrap(); - - let (scripts, _) = load_scripts(dir.path()).unwrap(); - let test_listeners = scripts.get("test").unwrap(); - assert_eq!(test_listeners.len(), 1); - let post_listeners = scripts.get("post-install-cmd").unwrap(); - assert_eq!(post_listeners.len(), 2); - } - - #[test] - fn test_list_scripts_output() { - let mut scripts = BTreeMap::new(); - scripts.insert("test".to_string(), vec!["phpunit".to_string()]); - scripts.insert("lint".to_string(), vec!["phpcs".to_string()]); - - let mut descriptions = BTreeMap::new(); - descriptions.insert("test".to_string(), "Run tests".to_string()); - - let result = list_scripts(&scripts, &descriptions, test_console()); - assert!(result.is_ok()); - } - - #[test] - fn test_list_scripts_empty_silent() { - let scripts: BTreeMap<String, Vec<String>> = BTreeMap::new(); - let descriptions: BTreeMap<String, String> = BTreeMap::new(); - let result = list_scripts(&scripts, &descriptions, test_console()); - assert!(result.is_ok()); - } - - #[test] - fn test_run_shell_command_success() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - let code = run_shell_command("echo hello", dir.path(), &bin_dir, None, &[]).unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_run_shell_command_failure() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - let code = run_shell_command("exit 1", dir.path(), &bin_dir, None, &[]).unwrap(); - assert_eq!(code, 1); - } - - #[test] - fn test_run_shell_command_with_args() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert("greet".to_string(), vec!["echo".to_string()]); - - let mut stack = vec![]; - let code = run_script( - "greet", - &["world".to_string()], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ) - .unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_run_putenv_set() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert( - "setup".to_string(), - vec!["@putenv MOZART_TEST_VAR=hello_world".to_string()], - ); - - let mut stack = vec![]; - run_script( - "setup", - &[], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ) - .unwrap(); - - assert_eq!(std::env::var("MOZART_TEST_VAR").unwrap(), "hello_world"); - } - - #[test] - fn test_run_putenv_unset() { - // SAFETY: test-only; no concurrent env access in this test - unsafe { std::env::set_var("MOZART_UNSET_VAR", "some_value") }; - - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert( - "cleanup".to_string(), - vec!["@putenv MOZART_UNSET_VAR".to_string()], - ); - - let mut stack = vec![]; - run_script( - "cleanup", - &[], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ) - .unwrap(); - - assert!(std::env::var("MOZART_UNSET_VAR").is_err()); - } - - #[test] - fn test_run_script_reference() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert("a".to_string(), vec!["@b".to_string()]); - scripts.insert("b".to_string(), vec!["echo from-b".to_string()]); - - let mut stack = vec![]; - let code = run_script( - "a", - &[], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ) - .unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_circular_reference_detected() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert("a".to_string(), vec!["@b".to_string()]); - scripts.insert("b".to_string(), vec!["@a".to_string()]); - - let mut stack = vec![]; - let result = run_script( - "a", - &[], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ); - assert!(result.is_err()); - let msg = result.unwrap_err().to_string(); - assert!(msg.contains("Circular script reference")); - } - - #[test] - fn test_php_callback_skipped_with_warning() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert( - "callback".to_string(), - vec!["MyClass::myMethod".to_string()], - ); - - let mut stack = vec![]; - let code = run_script( - "callback", - &[], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ) - .unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_no_additional_args_respected() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert( - "test".to_string(), - vec!["echo base @no_additional_args".to_string()], - ); - - let mut stack = vec![]; - let code = run_script( - "test", - &["extra-arg".to_string()], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ) - .unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_additional_args_placeholder() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let mut scripts = BTreeMap::new(); - scripts.insert( - "test".to_string(), - vec!["echo before @additional_args after".to_string()], - ); - - let mut stack = vec![]; - let code = run_script( - "test", - &["injected".to_string()], - &scripts, - dir.path(), - &bin_dir, - None, - true, - &mut stack, - 0, - test_console(), - ) - .unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_script_not_defined_error() { - let dir = tempfile::tempdir().unwrap(); - fs::write(dir.path().join("composer.json"), r#"{"name": "test/pkg"}"#).unwrap(); - - let (scripts, _) = load_scripts(dir.path()).unwrap(); - assert!(!scripts.contains_key("nonexistent")); - } - - #[test] - fn test_bin_dir_in_path() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - fs::create_dir_all(&bin_dir).unwrap(); - - let fake_bin = bin_dir.join("my-fake-tool"); - fs::write(&fake_bin, "#!/bin/sh\necho ran-fake-tool\n").unwrap(); - - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - fs::set_permissions(&fake_bin, fs::Permissions::from_mode(0o755)).unwrap(); - } - - let code = run_shell_command("my-fake-tool", dir.path(), &bin_dir, None, &[]).unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_timeout_kills_long_running() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let result = run_shell_command( - "sleep 10", - dir.path(), - &bin_dir, - Some(Duration::from_secs(1)), - &[], - ); - assert!(result.is_err()); - let msg = result.unwrap_err().to_string(); - assert!(msg.contains("timed out")); - } - - #[test] - fn test_composer_dev_mode_env() { - let dir = tempfile::tempdir().unwrap(); - let bin_dir = dir.path().join("vendor/bin"); - - let code = run_shell_command( - "test \"$COMPOSER_DEV_MODE\" = \"1\"", - dir.path(), - &bin_dir, - None, - &[("COMPOSER_DEV_MODE".to_string(), "1".to_string())], - ) - .unwrap(); - assert_eq!(code, 0); - - let code = run_shell_command( - "test \"$COMPOSER_DEV_MODE\" = \"0\"", - dir.path(), - &bin_dir, - None, - &[("COMPOSER_DEV_MODE".to_string(), "0".to_string())], - ) - .unwrap(); - assert_eq!(code, 0); - } - - #[test] - fn test_list_flag() { - let dir = tempfile::tempdir().unwrap(); - fs::write( - dir.path().join("composer.json"), - r#"{"name": "test/pkg", "scripts": {"test": "phpunit", "lint": "phpcs"}}"#, - ) - .unwrap(); - - let (scripts, descriptions) = load_scripts(dir.path()).unwrap(); - assert!(scripts.contains_key("test")); - assert!(scripts.contains_key("lint")); - - let result = list_scripts(&scripts, &descriptions, test_console()); - assert!(result.is_ok()); - } - - #[test] - fn test_internal_event_rejected() { - // Internal events are in script_events::ALL but not in USER_RUNNABLE. - assert!(script_events::ALL.contains(&"pre-package-install")); - assert!(script_events::ALL.contains(&"post-package-install")); - assert!(script_events::ALL.contains(&"pre-operations-exec")); - assert!(!script_events::USER_RUNNABLE.contains(&"pre-package-install")); - assert!(!script_events::USER_RUNNABLE.contains(&"post-package-install")); - assert!(!script_events::USER_RUNNABLE.contains(&"pre-operations-exec")); - // User-runnable events are in both. - assert!(script_events::USER_RUNNABLE.contains(&"pre-install-cmd")); - assert!(script_events::ALL.contains(&"pre-install-cmd")); - } -} |
