Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1454,11 +1454,15 @@ non-zero too, after printing its log.
`rocm uninstall` removes four categories of files that ROCm CLI manages on this
computer: its binaries, configuration, data, and caches. It lists what it will
do and asks `[y/N]` (default: no) before doing it. Use `--dry-run` to only list,
and `--yes` to skip the confirmation. It does not create the folders it reports
on: with `--keep-binaries`, on a computer where ROCm CLI never ran, it says there
is nothing to remove. Outside an interactive terminal, when there is something
to remove, it refuses with exit code 1 and removes nothing unless you pass
`--yes`. Each `--keep-*` flag skips the removal of one category:
and `--yes` to skip the confirmation. There is no `--force`; `--force-dev-binaries`
is a separate opt-in for development builds. At a terminal, a dry run that finds
something to remove ends by saying to run the same command again without
`--dry-run`; with nothing to remove, or when stdin or stdout is not a terminal
(its output is piped or its input is redirected), it says nothing about a next step. It does not create the folders it reports on:
with `--keep-binaries`, on a computer where ROCm CLI never ran, it says there is
nothing to remove. Outside an interactive terminal, when there is something to
remove, it refuses with exit code 1 and removes nothing unless you pass `--yes`.
Each `--keep-*` flag skips the removal of one category:

- `--keep-binaries` keeps the installed ROCm CLI binaries.
- `--keep-config` keeps your saved settings.
Expand Down
129 changes: 128 additions & 1 deletion apps/rocm/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,10 @@ rocm logs --search error timeout")]
/// PATH change the installer made (its marked shell-profile block, or the
/// user PATH entry it recorded adding on Windows), unless other programs
/// share the install folder.
#[command(after_help = "EXAMPLES:\n \
rocm uninstall --dry-run\n \
rocm uninstall\n \
rocm uninstall --keep-data --yes")]
Uninstall {
/// Do not ask for interactive confirmation. Without it, the command
/// asks `[y/N]` (default: no) in an interactive terminal; outside one,
Expand Down Expand Up @@ -668,6 +672,11 @@ rocm logs --search error timeout")]
/// Allow removing development binaries inside the current build tree.
#[arg(long)]
force_dev_binaries: bool,
/// Not an option; accepted only to say so. Without it clap answers a
/// mistyped `--force` (a likely guess for skipping the confirmation)
/// with `--force-dev-binaries`, which removes development binaries.
#[arg(long, hide = true, num_args = 0..=1, default_missing_value = "")]
force: Option<String>,
},
}

Expand Down Expand Up @@ -1412,7 +1421,7 @@ impl PermissionsModeArg {
#[cfg(test)]
mod tests {
use super::*;
use crate::{cli_command, device_policy_name, engine_inventory};
use crate::{cli_command, device_policy_name, engine_inventory, uninstall::reject_force};
use clap::{CommandFactory, Parser};
use rocm_engine_protocol::DevicePolicy;

Expand Down Expand Up @@ -2259,6 +2268,124 @@ mod tests {
.expect_err("an abbreviated subcommand name must not reach uninstall");
}

/// `--force` is a likely guess for "skip the confirmation", and without a
/// declared `--force` clap answers it, and every near miss of it, with
/// `--force-dev-binaries`, which removes the binaries of a development build.
/// The scenario `uninstall-mistyped-force-is-not-answered-with-a-destructive-flag`
/// proves the real binary's output; this pins the parse behind it.
#[test]
fn a_mistyped_force_never_reaches_the_dev_binaries_flag() {
// `--force` itself parses, so the handler can say what to use instead.
let cli = Cli::try_parse_from(["rocm", "uninstall", "--force"]).unwrap();
let Some(Command::Uninstall {
force,
force_dev_binaries,
..
}) = cli.command
else {
panic!("not an uninstall command");
};
assert!(force.is_some() && !force_dev_binaries);
let refusal = reject_force(force.is_some()).unwrap_err().to_string();
assert!(refusal.contains("`--force` is not an option"), "{refusal}");
assert!(refusal.contains("--yes"), "{refusal}");
assert!(!refusal.contains("force-dev-binaries"), "{refusal}");
assert!(reject_force(false).is_ok());
// `--force=x` is the same mistake, and gets the same answer rather than
// a usage line that lists every flag.
let cli = Cli::try_parse_from(["rocm", "uninstall", "--force=x"]).unwrap();
let Some(Command::Uninstall { force, .. }) = cli.command else {
panic!("not an uninstall command");
};
assert!(reject_force(force.is_some()).is_err());

// Near misses and the forms clap suggests from elsewhere in the command
// line never offer the destructive flag.
for args in [
vec!["uninstall", "--forc"],
vec!["uninstall", "--forcee"],
vec!["uninstall", "--keep-binaries", "--forc"],
vec!["uninstall", "-f"],
vec!["--force", "uninstall"],
] {
let error = cli_command()
.try_get_matches_from(std::iter::once("rocm").chain(args.iter().copied()))
.unwrap_err();
let rendered = error.render().to_string();
assert!(
!rendered.contains("force-dev-binaries"),
"{args:?} was answered with the destructive flag:\n{rendered}"
);
assert_eq!(error.exit_code(), 2, "{args:?}");
}

// The guard depends on clap offering the hidden `--force` as the
// suggestion for a near miss: its candidates are every declared long
// name, hidden or not. If a clap upgrade stopped suggesting hidden
// arguments, `--forc` would be answered with `--force-dev-binaries`
// again, so the near misses must name `--force` and not the
// destructive flag. The two checks together are independent of
// clap's wording around the suggestion.
for args in [
vec!["uninstall", "--forc"],
vec!["uninstall", "--keep-binaries", "--forc"],
] {
let rendered = cli_command()
.try_get_matches_from(std::iter::once("rocm").chain(args.iter().copied()))
.unwrap_err()
.render()
.to_string();
assert!(
rendered.contains("--force") && !rendered.contains("force-dev-binaries"),
"{args:?} must be steered to the declared `--force`, not the destructive flag:\n{rendered}"
);
}

// The whole flag misspelled is still met with the flag it was meant to be.
let error = cli_command()
.try_get_matches_from(["rocm", "uninstall", "--force-dev-binary"])
.unwrap_err()
.render()
.to_string();
assert!(error.contains("--force-dev-binaries"), "{error}");

// It stays out of the help.
let mut command = cli_command();
let help = command
.find_subcommand_mut("uninstall")
.unwrap()
.render_long_help()
.to_string();
assert!(!help.contains("Not an option"), "{help}");
}

/// The help leads with the safe preview, then the real run, as the tip a
/// person reads before running a command that deletes things.
#[test]
fn uninstall_help_shows_a_dry_run_before_the_real_run() {
let mut command = cli_command();
let help = command
.find_subcommand_mut("uninstall")
.expect("uninstall subcommand")
.render_long_help()
.to_string();
let examples = help.split_once("EXAMPLES:").map_or_else(
|| panic!("`rocm uninstall --help` has no examples:\n{help}"),
|(_, rest)| rest,
);
let rows: Vec<&str> = examples
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.collect();
assert_eq!(
rows.first().copied(),
Some("rocm uninstall --dry-run"),
"{examples}"
);
assert!(rows.contains(&"rocm uninstall"), "{examples}");
}

#[test]
fn update_rejects_zero_timeout_secs() {
let error = Cli::try_parse_from(["rocm", "update", "--json", "--timeout-secs", "0"])
Expand Down
1 change: 1 addition & 0 deletions apps/rocm/src/deletion_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ fn uninstall_case(
dry_run: true,
..options
},
crate::uninstall_plan::PlanAudience::Captured,
);
let planned: BTreeSet<PathBuf> = plan.actions.iter().map(|e| e.path.clone()).collect();
prop_assert_eq!(&listed_in_render(&rendered), &planned, "{}", rendered);
Expand Down
22 changes: 13 additions & 9 deletions apps/rocm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,19 @@ fn dispatch(cli: Cli) -> Result<()> {
keep_data,
keep_cache,
force_dev_binaries,
}) => uninstall(UninstallOptions {
yes,
dry_run,
keep_binaries,
keep_config,
keep_data,
keep_cache,
force_dev_binaries,
}),
force,
}) => {
uninstall::reject_force(force.is_some())?;
uninstall(UninstallOptions {
yes,
dry_run,
keep_binaries,
keep_config,
keep_data,
keep_cache,
force_dev_binaries,
})
}
Some(Command::Completions { shell }) => {
let mut cmd = completion_command();
clap_complete::generate(shell, &mut cmd, "rocm", &mut std::io::stdout());
Expand Down
30 changes: 26 additions & 4 deletions apps/rocm/src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use std::io::Write as _;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use rocm_core::{AppPaths, require_yes_or_interactive};
use rocm_core::{AppPaths, interactive_terminal, require_yes_or_interactive};

use crate::uninstall_plan::{
UninstallOptions, UninstallPlan, build_uninstall_plan, confirm_uninstall,
PlanAudience, UninstallOptions, UninstallPlan, build_uninstall_plan, confirm_uninstall,
is_rocm_install_entry_name, remove_path, render_uninstall_plan,
};

Expand Down Expand Up @@ -63,10 +63,31 @@ impl PathEdit {
}
}

/// `rocm uninstall` has no `--force`; the flag is declared (hidden) only so this
/// can answer it. Left undeclared, clap's nearest-name guess for `--force` is
/// `--force-dev-binaries`, which removes development binaries, and a near miss
/// such as `--forc` gets that same suggestion. Declared, those guesses land here
/// instead, before anything is planned or removed.
pub(crate) fn reject_force(force: bool) -> Result<()> {
if force {
bail!(
"`--force` is not an option of `rocm uninstall`; use `--yes` to skip the confirmation"
);
}
Ok(())
}

pub(crate) fn uninstall(options: UninstallOptions) -> Result<()> {
let paths = AppPaths::discover()?;
let plan = build_uninstall_plan(&paths, &options)?;
print!("{}", render_uninstall_plan(&plan, &options));
print!(
"{}",
render_uninstall_plan(
&plan,
&options,
PlanAudience::for_command(interactive_terminal())
)
);

if plan_is_empty(&plan) || options.dry_run {
return Ok(());
Expand Down Expand Up @@ -837,7 +858,8 @@ mod tests {
self_delete: Some(PathBuf::from("/opt/bin/rocm.exe")),
..UninstallPlan::default()
};
let output = render_uninstall_plan(&plan, &UninstallOptions::default());
let output =
render_uninstall_plan(&plan, &UninstallOptions::default(), PlanAudience::Captured);
for expected in [
"2 item(s) would be removed:",
" - install record: /opt/bin/.rocm-cli-path-edit",
Expand Down
Loading
Loading