Skip to content

Commit

Permalink
WIP unit-test dynamic completion
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Riccardi <[email protected]>
  • Loading branch information
thomas-riccardi committed Oct 29, 2022
1 parent 855832d commit 75eb5ae
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/kubectl-argo-rollouts/cmd/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package get
import (
"bytes"
"fmt"
"sort"
"strings"
"testing"
"time"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"k8s.io/cli-runtime/pkg/genericclioptions"

Expand Down Expand Up @@ -561,3 +563,48 @@ NAME KIND STATUS
`, "\n")
assertStdout(t, expectedOut, o.IOStreams)
}

func TestCompletionGetRollout(t *testing.T) {
rolloutObjs := testdata.NewCanaryRollout()

tf, o := options.NewFakeArgoRolloutsOptions(rolloutObjs.AllObjects()...)
o.RESTClientGetter = tf.WithNamespace(rolloutObjs.Rollouts[0].Namespace)
defer tf.Cleanup()
cmd := NewCmdGetRollout(o)
cmd.PersistentPreRunE = o.PersistentPreRunE

comps, directive := cmd.ValidArgsFunction(cmd, []string{}, "")
checkCompletion(t, comps, []string{"canary-demo", "canary-demo-pingpong", "canary-demo-weights", "canary-demo-weights-na", "canary-demo-workloadRef", "canary-demo-workloadRef-deploy"}, directive, cobra.ShellCompDirectiveNoFileComp)

comps, directive = cmd.ValidArgsFunction(cmd, []string{}, "cana")
checkCompletion(t, comps, []string{"canary-demo", "canary-demo-pingpong", "canary-demo-weights", "canary-demo-weights-na", "canary-demo-workloadRef", "canary-demo-workloadRef-deploy"}, directive, cobra.ShellCompDirectiveNoFileComp)

comps, directive = cmd.ValidArgsFunction(cmd, []string{}, "canary-demo")
checkCompletion(t, comps, []string{"canary-demo", "canary-demo-pingpong", "canary-demo-weights", "canary-demo-weights-na", "canary-demo-workloadRef", "canary-demo-workloadRef-deploy"}, directive, cobra.ShellCompDirectiveNoFileComp)

comps, directive = cmd.ValidArgsFunction(cmd, []string{}, "canary-demo-p")
checkCompletion(t, comps, []string{"canary-demo-pingpong"}, directive, cobra.ShellCompDirectiveNoFileComp)

comps, directive = cmd.ValidArgsFunction(cmd, []string{}, "seagull")
checkCompletion(t, comps, []string{}, directive, cobra.ShellCompDirectiveNoFileComp)
}

func checkCompletion(t *testing.T, comps, expectedComps []string, directive, expectedDirective cobra.ShellCompDirective) {
if e, d := expectedDirective, directive; e != d {
t.Errorf("expected directive\n%v\nbut got\n%v", e, d)
}

sort.Strings(comps)
sort.Strings(expectedComps)

if len(expectedComps) != len(comps) {
t.Fatalf("expected completions\n%v\nbut got\n%v", expectedComps, comps)
}

for i := range comps {
if expectedComps[i] != comps[i] {
t.Errorf("expected completions\n%v\nbut got\n%v", expectedComps, comps)
break
}
}
}

0 comments on commit 75eb5ae

Please sign in to comment.