2026-03-02 22:12:54 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCompletionCmd(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
shell string
|
|
|
|
|
}{
|
|
|
|
|
{"bash"},
|
|
|
|
|
{"zsh"},
|
|
|
|
|
{"fish"},
|
|
|
|
|
{"powershell"},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.shell, func(t *testing.T) {
|
|
|
|
|
oldStdout := os.Stdout
|
|
|
|
|
defer func() { os.Stdout = oldStdout }()
|
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
os.Stdout = w
|
2026-03-02 22:19:47 +00:00
|
|
|
defer func() { _ = w.Close() }()
|
2026-03-02 22:12:54 +00:00
|
|
|
cmd := &cobra.Command{}
|
|
|
|
|
completionCmd.Run(cmd, []string{tt.shell})
|
2026-03-02 22:19:47 +00:00
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
|
t.Errorf("close: %v", err)
|
|
|
|
|
}
|
2026-03-02 22:12:54 +00:00
|
|
|
b, err := io.ReadAll(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(b) == 0 {
|
|
|
|
|
t.Error("expected non-empty completion script")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|