Skip to content

Commit

Permalink
Wip multi args completion and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clementblaise committed Feb 2, 2021
1 parent b389b53 commit 9f3e66f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
23 changes: 18 additions & 5 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@ import (
"strings"
)

func extractMultitpleCompletion(toComplete string) (remainder string, current string) {
toCompletes := strings.Split(toComplete, ",")

if len(toCompletes) == 1 {
return utils.EmptyString, toCompletes[0]
}

remainder = fmt.Sprintf("%s%s", strings.Join(toCompletes[:len(toCompletes)-1], ","), ",")
current = toCompletes[len(toCompletes)-1]
return
}

func filterCompletion(toComplete string, path string) ([]string, cobra.ShellCompDirective) {
// logrus.SetLevel(logrus.PanicLevel)
logrus.SetLevel(logrus.PanicLevel)

key, op, value := ansible.ParseFilter(toComplete)
remainder, current := extractMultitpleCompletion(toComplete)
cobra.CompDebug(fmt.Sprintf("extract muitple: remainder:%s current:%s\n", remainder, current), true)

cobra.CompDebug(fmt.Sprintf("key:%s op:%s value:%s", key, op, value), true)
key, op, value := ansible.ParseFilter(current)
cobra.CompDebug(fmt.Sprintf("parse filter: key:%s op:%s value:%s\n", key, op, value), true)

k8s := ansible.Projects.LoadFromPath(path)

availableKeys := k8s.InventoryKeys()

blank := key == utils.EmptyString && op == utils.EmptyString && value == utils.EmptyString
if blank {
return availableKeys, cobra.ShellCompDirectiveDefault
return utils.AppendPrefixOnSlice(remainder, availableKeys), cobra.ShellCompDirectiveDefault
}

writingKey := key != utils.EmptyString && op == utils.EmptyString && value == utils.EmptyString
Expand Down
54 changes: 54 additions & 0 deletions cmd/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,47 @@ const (
ProjectSimpleLevelPath = "../testdata/projectSimpleLevel"
)

func TestExtractMultitpleCompletion(t *testing.T) {
testCases := map[string]struct {
toComplete string
expectsRemainder string
expectsCurrent string
}{"empty string should return empty strings": {
toComplete: "",
expectsRemainder: "",
expectsCurrent: "",
}, "'first' should return no remainder and 'first' as current": {
toComplete: "first",
expectsRemainder: "",
expectsCurrent: "first",
}, "'first,' should return 'first,' as remainder and empty as current": {
toComplete: "first,",
expectsRemainder: "first,",
expectsCurrent: "",
}, "'first,second' should return 'first,' as remainder and second as current": {
toComplete: "first,second",
expectsRemainder: "first,",
expectsCurrent: "second",
}, "'first,second,' should return 'first,second,' as remainder and empty as current": {
toComplete: "first,second,",
expectsRemainder: "first,second,",
expectsCurrent: "",
}, "'first,second,third' should return 'first,second,' as remainder and third as current": {
toComplete: "first,second,third",
expectsRemainder: "first,second,",
expectsCurrent: "third",
}}

for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
actualRemainder, actualCurrent := extractMultitpleCompletion(testCase.toComplete)
assert.Equal(t, testCase.expectsRemainder, actualRemainder)
assert.Equal(t, testCase.expectsCurrent, actualCurrent)
})
}

}

func TestFilterCompletion(t *testing.T) {
testCases := map[string]struct {
toComplete string
Expand Down Expand Up @@ -68,10 +109,23 @@ func TestFilterCompletion(t *testing.T) {
path: ProjectMultiLevelPath,
expect: nil,
},
"multi-level with 'customer==customer1,' should return to complete": {
toComplete: "customer==customer1",
path: ProjectMultiLevelPath,
expect: []string{"customer==customer1"},
},
"multi-level with 'customer==customer1,' should return all vars": {
toComplete: "customer==customer1,",
path: ProjectMultiLevelPath,
expect: []string{"customer", "env", "os", "platform"},
},
}

for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {

fmt.Println()

actual, _ := filterCompletion(testCase.toComplete, testCase.path)
assert.Equal(t, testCase.expect, actual)
})
Expand Down
16 changes: 16 additions & 0 deletions internal/utils/collections.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package utils

import "fmt"

// Index returns the first index of the target string `t`, or
// -1 if no match is found.
func Index(vs []string, t string) int {
Expand Down Expand Up @@ -74,3 +76,17 @@ func MapHasAllTrue(a map[string]bool) bool {

return true
}

func AppendPrefixOnSlice(prefix string, slice []string) (result []string) {
for _, s := range slice {
result = append(result, fmt.Sprintf("%s%s", prefix, s))
}
return
}

func AppendSuffixOnSlice(suffix string, slice []string) (result []string) {
for _, s := range slice {
result = append(result, fmt.Sprintf("%s%s", s, suffix))
}
return
}

0 comments on commit 9f3e66f

Please sign in to comment.