From 1daf54ecbf960c42d612ed4ad2764045a606fb30 Mon Sep 17 00:00:00 2001 From: Golden-Pigeon Date: Fri, 22 May 2026 02:39:02 +0300 Subject: feat: show named session command hints refs #199 --- src/update.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 10 deletions(-) (limited to 'src/update.rs') diff --git a/src/update.rs b/src/update.rs index 98943424..f37a9265 100644 --- a/src/update.rs +++ b/src/update.rs @@ -462,8 +462,9 @@ fn prompt_to_stop_server_before_update( if !io::stdin().is_terminal() { if requires_stop { return Err(format!( - "a herdr server is running and updating to v{} requires stopping it; run `herdr server stop`, then run `herdr update` again", - release.version + "a herdr server is running and updating to v{} requires stopping it; run `{}`, then run `herdr update` again", + release.version, + crate::session::local_stop_command() )); } @@ -533,10 +534,10 @@ fn plan_running_server_update( ) -> Result, String> { let Some(server) = read_running_server_info()? else { if client_protocol_server_is_running() { - return Err( - "a herdr server is listening, but its status API is unavailable; try `herdr server stop`, or stop the old server process manually, then run `herdr update` again" - .to_string(), - ); + return Err(format!( + "a herdr server is listening, but its status API is unavailable; try `{}`, or stop the old server process manually, then run `herdr update` again", + crate::session::local_stop_command() + )); } return Ok(None); }; @@ -560,10 +561,10 @@ fn stop_running_server_for_update( prompt_to_stop_server_before_update(&plan.server, release, plan.requires_stop)?; if !stop_server { if plan.requires_stop { - return Err( - "update cancelled; stop the running herdr server with `herdr server stop`, then run `herdr update` again" - .to_string(), - ); + return Err(format!( + "update cancelled; stop the running herdr server with `{}`, then run `herdr update` again", + crate::session::local_stop_command() + )); } return Ok(false); } @@ -1185,8 +1186,14 @@ mod tests { atomic::{AtomicBool, Ordering}, Arc, }; + use std::sync::{Mutex, OnceLock}; use std::thread; + fn env_lock() -> &'static Mutex<()> { + static LOCK: OnceLock> = OnceLock::new(); + LOCK.get_or_init(|| Mutex::new(())) + } + fn unique_test_socket_path(name: &str) -> std::path::PathBuf { let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -1355,6 +1362,40 @@ mod tests { assert!(update_requires_server_stop(&server, &unknown_release)); } + #[test] + fn noninteractive_update_requires_stop_names_session_stop_command() { + let _guard = env_lock().lock().unwrap(); + assert!( + !io::stdin().is_terminal(), + "this test relies on noninteractive test stdin" + ); + std::env::set_var(crate::session::SESSION_ENV_VAR, "work"); + crate::session::clear_explicit_session_for_test(); + let server = crate::api::RuntimeStatus { + version: Some("0.5.5".to_string()), + protocol: Some(2), + }; + let release = ReleaseInfo { + version: Version::parse("0.5.6").unwrap(), + target_protocol: Some(3), + download_url: "https://example.com/herdr".to_string(), + notes_body: "### Changed\n- One".to_string(), + }; + + let err = prompt_to_stop_server_before_update(&server, &release, true).unwrap_err(); + + assert!( + err.contains("run `herdr session stop work`"), + "unexpected error: {err}" + ); + assert!( + err.contains("then run `herdr update` again"), + "unexpected error: {err}" + ); + std::env::remove_var(crate::session::SESSION_ENV_VAR); + crate::session::clear_explicit_session_for_test(); + } + #[test] fn client_protocol_server_is_running_at_detects_live_socket() { let socket_path = unique_test_socket_path("client-live"); -- cgit v1.2.3