use anyhow::Result;
use clap::Args;
use clap_complete::Shell;
use std::io;
#[derive(Args, Debug)]
pub struct CompletionsArgs {
#[arg(value_enum)]
pub shell: Shell,
}
pub fn run(args: CompletionsArgs, _paths: crate::Paths) -> Result<i32> {
let mut cmd = crate::build_cli();
let bin_name = cmd.get_name().to_string();
clap_complete::generate(args.shell, &mut cmd, bin_name, &mut io::stdout().lock());
Ok(crate::exit_codes::OK)
}
#[cfg(test)]
mod tests {
use super::*;
use clap::{CommandFactory, Parser};
fn emit(shell: Shell) -> Vec<u8> {
let mut cmd = <crate::Cli as CommandFactory>::command();
let bin = cmd.get_name().to_string();
let mut buf: Vec<u8> = Vec::new();
clap_complete::generate(shell, &mut cmd, bin, &mut buf);
buf
}
#[test]
fn bash_output_contains_subcommands_and_bin_name() {
let script = String::from_utf8(emit(Shell::Bash)).expect("utf8");
assert!(script.contains("linkmarks"), "bin name missing");
assert!(
script.contains("init") || script.contains("list") || script.contains("tui"),
"expected at least one subcommand literal"
);
}
#[test]
fn zsh_output_is_a_compdef_script() {
let script = String::from_utf8(emit(Shell::Zsh)).expect("utf8");
assert!(
script.contains("#compdef"),
"zsh script missing #compdef footer"
);
}
#[test]
fn fish_output_uses_complete_command() {
let script = String::from_utf8(emit(Shell::Fish)).expect("utf8");
assert!(
script.contains("complete -c linkmarks"),
"fish script missing `complete -c linkmarks` registration"
);
}
#[test]
fn powershell_output_uses_register_argumentcompleter() {
let script = String::from_utf8(emit(Shell::PowerShell)).expect("utf8");
assert!(
script.contains("Register-ArgumentCompleter"),
"powershell script missing Register-ArgumentCompleter"
);
}
#[test]
fn elvish_output_uses_edit_completion_arg_completer() {
let script = String::from_utf8(emit(Shell::Elvish)).expect("utf8");
assert!(
script.contains("edit:completion:arg-completer"),
"elvish script missing completion binding"
);
}
#[test]
fn all_supported_shells_appear_in_subcommand_help() {
let cli = crate::Cli::try_parse_from(["linkmarks", "completions", "--help"]);
let help = format!("{:?}", cli).to_lowercase();
assert!(help.contains("bash"));
assert!(help.contains("zsh"));
assert!(help.contains("fish"));
assert!(help.contains("powershell"));
assert!(help.contains("elvish"));
}
}