diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-20 14:05:55 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-20 14:05:55 +0900 |
| commit | e77a37c5447a4d122629ef8fbc9bef309668aa89 (patch) | |
| tree | 3a25db8f65a1a20d19c0962157651984dbbfeb95 /crates/shirabe | |
| parent | fc39608de4226286c34aed0c0803edd15a8623a8 (diff) | |
| download | php-shirabe-e77a37c5447a4d122629ef8fbc9bef309668aa89.tar.gz php-shirabe-e77a37c5447a4d122629ef8fbc9bef309668aa89.tar.zst php-shirabe-e77a37c5447a4d122629ef8fbc9bef309668aa89.zip | |
test(cli): add more smoke tests
Diffstat (limited to 'crates/shirabe')
| -rw-r--r-- | crates/shirabe/Cargo.toml | 3 | ||||
| -rw-r--r-- | crates/shirabe/tests/cli.rs | 105 |
2 files changed, 108 insertions, 0 deletions
diff --git a/crates/shirabe/Cargo.toml b/crates/shirabe/Cargo.toml index aed4c1c..7b95ae5 100644 --- a/crates/shirabe/Cargo.toml +++ b/crates/shirabe/Cargo.toml @@ -21,5 +21,8 @@ sha1.workspace = true tokio.workspace = true url.workspace = true +[dev-dependencies] +tempfile.workspace = true + [lints] workspace = true diff --git a/crates/shirabe/tests/cli.rs b/crates/shirabe/tests/cli.rs index 09b9746..549e5a3 100644 --- a/crates/shirabe/tests/cli.rs +++ b/crates/shirabe/tests/cli.rs @@ -80,3 +80,108 @@ fn each_command_help() { let failed: Vec<&&str> = COMMANDS.iter().filter(|c| !run(&[c, "--help"])).collect(); assert!(failed.is_empty(), "`<cmd> --help` failed for: {failed:?}"); } + +/// Runs the CLI with `args` from inside an empty temporary directory. Returns true if the call did +/// not panic (any exit code, including non-zero or an `Err` return, counts as success). +fn run_no_panic(args: &[&str]) -> bool { + QUIET_PANIC.call_once(|| std::panic::set_hook(Box::new(|_| {}))); + let _guard = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + + let original = std::env::current_dir().ok(); + let dir = tempfile::tempdir().expect("create temp dir"); + std::env::set_current_dir(dir.path()).expect("chdir to temp dir"); + + // SAFETY: all environment access here happens while holding `SERIAL`, so no other thread + // touches the environment or working directory concurrently. + unsafe { + std::env::remove_var("COLUMNS"); + std::env::remove_var("LINES"); + } + + let mut argv = vec!["composer".to_string()]; + argv.extend(args.iter().map(|s| s.to_string())); + let result = catch_unwind(AssertUnwindSafe(|| shirabe::run(argv))); + + if let Some(orig) = original { + let _ = std::env::set_current_dir(orig); + } + + result.is_ok() +} + +macro_rules! run_no_panic_tests { + ($( $(#[$attr:meta])* $name:ident => $cmd:expr ),* $(,)?) => { + $( + $(#[$attr])* + #[test] + fn $name() { + assert!(run_no_panic(&[$cmd]), "`{}` panicked", $cmd); + } + )* + }; +} + +run_no_panic_tests! { + run_about => "about", + #[ignore = "currently panics"] + run_archive => "archive", + #[ignore = "currently panics"] + run_audit => "audit", + #[ignore = "currently panics"] + run_browse => "browse", + #[ignore = "currently panics"] + run_bump => "bump", + #[ignore = "currently panics"] + run_check_platform_reqs => "check-platform-reqs", + #[ignore = "currently panics"] + run_clear_cache => "clear-cache", + #[ignore = "currently panics"] + run_config => "config", + #[ignore = "currently panics"] + run_create_project => "create-project", + #[ignore = "currently panics"] + run_depends => "depends", + #[ignore = "currently panics"] + run_diagnose => "diagnose", + #[ignore = "currently panics"] + run_dump_autoload => "dump-autoload", + #[ignore = "currently panics"] + run_exec => "exec", + #[ignore = "currently panics"] + run_fund => "fund", + #[ignore = "stack overflow aborts the process (uncatchable)"] + run_global => "global", + #[ignore = "currently panics"] + run_init => "init", + #[ignore = "currently panics"] + run_install => "install", + #[ignore = "currently panics"] + run_licenses => "licenses", + #[ignore = "currently panics"] + run_outdated => "outdated", + #[ignore = "currently panics"] + run_prohibits => "prohibits", + #[ignore = "currently panics"] + run_reinstall => "reinstall", + #[ignore = "currently panics"] + run_remove => "remove", + #[ignore = "currently panics"] + run_repository => "repository", + #[ignore = "currently panics"] + run_require => "require", + #[ignore = "currently panics"] + run_run_script => "run-script", + #[ignore = "currently panics"] + run_search => "search", + run_self_update => "self-update", + #[ignore = "currently panics"] + run_show => "show", + #[ignore = "currently panics"] + run_status => "status", + #[ignore = "currently panics"] + run_suggests => "suggests", + #[ignore = "currently panics"] + run_update => "update", + #[ignore = "currently panics"] + run_validate => "validate", +} |
