diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 4c8656bae18..eb8228b40a4 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -156,7 +156,11 @@ pub trait CommandExt: Sized { ) -> Self { self.arg_targets_lib_bin_example(lib, bin, bins, example, examples) ._arg(flag("tests", tests).help_heading(heading::TARGET_SELECTION)) - ._arg(optional_multi_opt("test", "NAME", test).help_heading(heading::TARGET_SELECTION)) + ._arg( + optional_multi_opt("test", "NAME", test) + .help_heading(heading::TARGET_SELECTION) + .add(clap_complete::ArgValueCandidates::new(get_test_candidates)), + ) ._arg(flag("benches", benches).help_heading(heading::TARGET_SELECTION)) ._arg( optional_multi_opt("bench", "NAME", bench).help_heading(heading::TARGET_SELECTION), @@ -1041,6 +1045,17 @@ pub fn lockfile_path( return Ok(Some(path)); } +fn get_test_candidates() -> Vec { + get_targets_from_metadata() + .unwrap_or_default() + .into_iter() + .filter_map(|target| match target.kind() { + TargetKind::Test => Some(clap_complete::CompletionCandidate::new(target.name())), + _ => None, + }) + .collect::>() +} + fn get_bin_candidates() -> Vec { get_targets_from_metadata() .unwrap_or_default()