Archived
feat(cli): add autocompletion command for shell completion scripts
Adds `barn autocompletion` command to generate shell completion scripts: - `barn autocompletion --bash` generates bash completion - `barn autocompletion --zsh` generates zsh completion (using bashcompinit) Usage: barn autocompletion --bash > /usr/local/etc/bash_completion.d/barn barn autocompletion --zsh > ~/.zsh/completions/_barn Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e8ea91268a
commit
028770a2da
3 files changed
+167
-1
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package com.samsonmedia.barn.cli;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import picocli.CommandLine;
|
||||
|
||||
/**
|
||||
* Tests for AutocompletionCommand.
|
||||
*/
|
||||
class AutocompletionCommandTest {
|
||||
|
||||
private CommandLine commandLine;
|
||||
private StringWriter out;
|
||||
private StringWriter err;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
commandLine = new CommandLine(new BarnCommand());
|
||||
out = new StringWriter();
|
||||
err = new StringWriter();
|
||||
commandLine.setOut(new PrintWriter(out));
|
||||
commandLine.setErr(new PrintWriter(err));
|
||||
}
|
||||
|
||||
@Nested
|
||||
class BashCompletion {
|
||||
|
||||
@Test
|
||||
void shouldGenerateBashScript() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--bash");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("#!/usr/bin/env bash");
|
||||
assertThat(out.toString()).contains("barn Bash Completion");
|
||||
assertThat(out.toString()).contains("COMPREPLY");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldIncludeSubcommands() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--bash");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
String output = out.toString();
|
||||
assertThat(output).contains("run");
|
||||
assertThat(output).contains("status");
|
||||
assertThat(output).contains("describe");
|
||||
assertThat(output).contains("service");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ZshCompletion {
|
||||
|
||||
@Test
|
||||
void shouldGenerateZshScript() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--zsh");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("#compdef barn");
|
||||
assertThat(out.toString()).contains("bashcompinit");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldIncludeBashCompletionForCompatibility() {
|
||||
int exitCode = commandLine.execute("autocompletion", "--zsh");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("COMPREPLY");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class NoShellSpecified {
|
||||
|
||||
@Test
|
||||
void shouldShowUsage() {
|
||||
int exitCode = commandLine.execute("autocompletion");
|
||||
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(out.toString()).contains("Usage:");
|
||||
assertThat(out.toString()).contains("--bash");
|
||||
assertThat(out.toString()).contains("--zsh");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user