From ba39f4b86971812714ce3c35822474152a112aa0 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 13:24:09 +0100 Subject: [PATCH] fix(cli): reject flags that need another flag in preview deploy and env create clap skips a `requires` rule when an argument that conflicts with the required one is present. `preview deploy ` accepted and ignored `--branch`, `--full-history` and `--allow-secret-files`, which need `--local`; `env create --promote-from --commit ` sent the commit to the daemon, which refused it after the confirmation prompt. The preview now also conflicts with the `--local` options, and `--promote-from` with `--commit`, so both are usage errors. Fixes PQD-166 --- apps/piquelctl/src/environments.rs | 5 +- apps/piquelctl/src/previews.rs | 5 +- apps/piquelctl/tests/cli/environments.rs | 88 ++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/apps/piquelctl/src/environments.rs b/apps/piquelctl/src/environments.rs index f6b1b19f8..bb121bba2 100644 --- a/apps/piquelctl/src/environments.rs +++ b/apps/piquelctl/src/environments.rs @@ -456,7 +456,10 @@ pub(crate) struct CreateArgs { /// Make the environment promoted: it never builds, and only receives /// releases promoted from this environment of the application, given by /// name or stable ID, with `env promote`. - #[arg(long, value_name = "ENV", conflicts_with = "branch")] + // It also conflicts with `--commit`: clap skips `--commit`'s + // `requires = "branch"` once an argument conflicting with `--branch` is + // present. + #[arg(long, value_name = "ENV", conflicts_with_all = ["branch", "commit"])] promote_from: Option, #[command(flatten)] change: ChangeFlags, diff --git a/apps/piquelctl/src/previews.rs b/apps/piquelctl/src/previews.rs index 4efbd3536..b08cd1ae5 100644 --- a/apps/piquelctl/src/previews.rs +++ b/apps/piquelctl/src/previews.rs @@ -124,7 +124,10 @@ pub(crate) struct DeployArgs { /// Application name or stable ID. application: String, /// Preview ID, slug, or branch. Omit it with `--local`. - #[arg(required_unless_present = "local", conflicts_with = "local")] + // The preview also conflicts with the `--local` options, the + // `SnapshotArgs` group: clap skips their `requires = "local"` once an + // argument conflicting with `--local` is present. + #[arg(required_unless_present = "local", conflicts_with_all = ["local", "SnapshotArgs"])] preview: Option, /// The preview's slot. Give it to address a preview with a slot by its /// branch, or to choose the preview `--local` deploys to. diff --git a/apps/piquelctl/tests/cli/environments.rs b/apps/piquelctl/tests/cli/environments.rs index 985f0d14d..8588ac42e 100644 --- a/apps/piquelctl/tests/cli/environments.rs +++ b/apps/piquelctl/tests/cli/environments.rs @@ -99,6 +99,94 @@ fn env_commands_target_the_named_environment() { let _ = server.finish(); } +/// Asserts `arguments` are a usage error naming `flag`, refused before the +/// daemon is contacted. +fn assert_usage_error(arguments: &[&str], flag: &str) { + let output = support::command() + .arg("--json") + .args(arguments) + .output() + .unwrap(); + assert_eq!(output.status.code(), Some(2), "{arguments:?}"); + let error = support::json_error(&output); + assert_eq!(error["code"], "usage", "{arguments:?}"); + assert!(error["message"].as_str().unwrap().contains(flag), "{error}"); +} + +#[test] +fn local_snapshot_flags_are_refused_with_a_preview() { + let deploy = [ + "preview", + "deploy", + "app-notes-01", + "notes-feat-login-0a1b2c", + ]; + for flag in [ + &["--branch", "feat"][..], + &["--full-history"], + &["--allow-secret-files"], + ] { + assert_usage_error(&[&deploy[..], flag].concat(), flag[0]); + } + + let server = start_server(false, 2, move |request| match request.path.as_str() { + "/api/v1/applications/app-notes-01" => Reply::json(notes_with_a_preview()), + "/api/v1/previews/env-preview-01/deploy" => { + assert_eq!(request.method, "POST"); + Reply::accepted(accepted("env-preview-01")) + } + path => panic!("unexpected path {path}"), + }); + let output = run(&server, &[&deploy[..], &["--no-wait"]].concat()); + assert_eq!( + assert_json_success(&output)["environment_id"], + "env-preview-01" + ); + let _ = server.finish(); +} + +#[test] +fn a_promoted_environment_refuses_a_pinned_commit() { + let commit = "a".repeat(40); + assert_usage_error( + &[ + "env", + "create", + "app-notes-01", + "qa", + "--promote-from", + "staging", + "--commit", + &commit, + ], + "--commit", + ); + + let server = request_server( + app_view("app-notes-01", "notes"), + "POST", + "/api/v1/applications/app-notes-01/environments", + json!({"name": "qa", "branch": "release", "commit": commit, "expected_generation": 1}), + Reply::json(environment("app-notes-01", "env-qa-01", "qa")), + ); + let output = run( + &server, + &[ + "env", + "create", + "app-notes-01", + "qa", + "--branch", + "release", + "--commit", + &commit, + "--yes", + ], + ); + assert_eq!(assert_json_success(&output)["name"], "qa"); + let _ = server.finish(); +} + #[test] fn deleting_an_application_with_several_environments_names_every_one() { let server = start_server(false, usize::MAX, move |request| {