Skip to main content

ubt_cli/
completions.rs

1use clap::CommandFactory;
2use clap_complete::Shell;
3use std::io::Write;
4
5use crate::cli::Cli;
6
7/// Generate shell completions for the given shell and write to the provided writer.
8pub fn generate_completions(shell: Shell, writer: &mut dyn Write) {
9    let mut cmd = Cli::command();
10    clap_complete::generate(shell, &mut cmd, "ubt", writer);
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16
17    fn completions_for(shell: Shell) -> String {
18        let mut buf = Vec::new();
19        generate_completions(shell, &mut buf);
20        String::from_utf8(buf).expect("completions should be valid UTF-8")
21    }
22
23    #[test]
24    fn bash_completions_non_empty() {
25        let output = completions_for(Shell::Bash);
26        assert!(!output.is_empty());
27    }
28
29    #[test]
30    fn bash_completions_has_marker() {
31        let output = completions_for(Shell::Bash);
32        assert!(output.contains("complete"));
33    }
34
35    #[test]
36    fn zsh_completions_non_empty() {
37        let output = completions_for(Shell::Zsh);
38        assert!(!output.is_empty());
39    }
40
41    #[test]
42    fn zsh_completions_has_compdef() {
43        let output = completions_for(Shell::Zsh);
44        assert!(output.contains("#compdef"));
45    }
46
47    #[test]
48    fn fish_completions_non_empty() {
49        let output = completions_for(Shell::Fish);
50        assert!(!output.is_empty());
51    }
52
53    #[test]
54    fn powershell_completions_non_empty() {
55        let output = completions_for(Shell::PowerShell);
56        assert!(!output.is_empty());
57    }
58}