grokkit/cmd/completion_test.go

45 lines
768 B
Go
Raw Permalink Normal View History

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
defer func() { _ = w.Close() }()
cmd := &cobra.Command{}
completionCmd.Run(cmd, []string{tt.shell})
if err := w.Close(); err != nil {
t.Errorf("close: %v", err)
}
b, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if len(b) == 0 {
t.Error("expected non-empty completion script")
}
})
}
}