Add a test case to verify that the command produces the expected autocompletion scripts for each shell type.

Signed-off-by: Umair0343 <umairshahidpti@gmail.com>
This commit is contained in:
Umair0343
2023-11-25 22:46:30 +05:00
parent 524b6f2b1d
commit bca14ea369
+49
View File
@@ -3,6 +3,7 @@ package completion
import (
"io"
"os"
"strings"
"testing"
"github.com/spf13/cobra"
@@ -105,3 +106,51 @@ func TestGetCompletionCmd_RunNotExpectedOutputs(t *testing.T) {
})
}
}
func TestGetCompletionCmd_RunProducesExpectedOutput(t *testing.T) {
tests := []struct {
name string
args []string
wantPrefix string
}{
{
name: "Bash completion",
args: []string{"bash"},
wantPrefix: "# bash completion for",
},
{
name: "Zsh completion",
args: []string{"zsh"},
wantPrefix: "#compdef",
},
{
name: "Fish completion",
args: []string{"fish"},
wantPrefix: "complete -c",
},
{
name: "PowerShell completion",
args: []string{"powershell"},
wantPrefix: "Register-ArgumentCompleter -Native -CommandName",
},
}
completionCmd := GetCompletionCmd()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Redirect stdout to a buffer
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
completionCmd.Run(&cobra.Command{}, tt.args)
w.Close()
got, _ := io.ReadAll(r)
os.Stdout = rescueStdout
assert.True(t, strings.HasPrefix(string(got), tt.wantPrefix))
})
}
}