From 924aa3b1fa71c6e1862f073e7b3effa44163cf01 Mon Sep 17 00:00:00 2001 From: Alan Li Date: Mon, 10 Aug 2026 11:37:57 -0400 Subject: [PATCH] fix(cli): exit with non-zero status when configure receives an invalid API URL Previously, `hindsight configure --api-url ` printed an error but returned exit code 0 because the validation block called `print_error!` followed by `return Ok(())`. Scripts and CI pipelines relying on the exit code could not detect the failure. Replace the silent success with `anyhow::bail!` so the top-level error handler prints the message and exits with status 1, matching the behavior of every other CLI error path. Add two integration tests: - configure rejects URLs missing http(s):// with non-zero exit - configure accepts both http:// and https:// URLs --- hindsight-cli/src/main.rs | 5 ++-- hindsight-cli/tests/cli_profile.rs | 37 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/hindsight-cli/src/main.rs b/hindsight-cli/src/main.rs index 2d9c15d521..07860d26f1 100644 --- a/hindsight-cli/src/main.rs +++ b/hindsight-cli/src/main.rs @@ -2199,11 +2199,10 @@ fn handle_configure( // Validate the URL if !new_api_url.starts_with("http://") && !new_api_url.starts_with("https://") { - ui::print_error(&format!( + anyhow::bail!( "Invalid API URL: {}. Must start with http:// or https://", new_api_url - )); - return Ok(()); + ); } // Use provided api_key, or keep existing one if not provided diff --git a/hindsight-cli/tests/cli_profile.rs b/hindsight-cli/tests/cli_profile.rs index 35388e24df..3f19b32c2a 100644 --- a/hindsight-cli/tests/cli_profile.rs +++ b/hindsight-cli/tests/cli_profile.rs @@ -319,3 +319,40 @@ fn hindsight_profile_env_var_is_honored() { + &String::from_utf8_lossy(&out.stdout); assert!(err.contains("from-env"), "expected profile URL in output:\n{}", err); } + +#[test] +fn configure_rejects_invalid_url_with_nonzero_exit() { + let home = unique_tempdir("bad-url"); + let out = run_with_home( + &home, + &[ + "configure", + "--api-url", + "not-a-valid-url", + "--api-key", + "test-key", + ], + ); + + assert!( + !out.status.success(), + "configure with invalid URL should exit non-zero, got success" + ); + assert!( + stderr(&out).contains("Invalid API URL"), + "expected error message on stderr, got:\n{}", + stderr(&out) + ); +} + +#[test] +fn configure_accepts_valid_http_and_https_urls() { + for url in ["http://localhost:8080", "https://api.example.com"] { + let home = unique_tempdir("valid-url"); + let out = run_with_home( + &home, + &["configure", "--api-url", url, "--api-key", "test-key"], + ); + assert_success(&out); + } +}