Skip to content

Commit

Permalink
Add TestTagsCompletion and TestPlaybookCompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
clementblaise committed Jan 27, 2021
1 parent eda61ca commit 7e83ca9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 72 deletions.
9 changes: 7 additions & 2 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,18 @@ func playbookCompletion(toComplete string, path string) ([]string, cobra.ShellCo
func tagsCompletion(toComplete string, path string, playbookPath string) ([]string, cobra.ShellCompDirective) {
logrus.SetLevel(logrus.PanicLevel)
var _ = regexp.MustCompile("([\\w-.\\/]+)([,]|)")

if len(playbookPath) == 0 {
return nil, cobra.ShellCompDirectiveDefault
}
project := ansible.Projects.LoadFromPath(path)

//TODO unmanaged error
playbook, _ := project.PlaybookPath(playbookPath)
playbook, err := project.PlaybookPath(playbookPath)

if err != nil {
cobra.CompDebug(err.Error(), true)
return nil, cobra.ShellCompDirectiveDefault
}

return playbook.AllTags().List(), cobra.ShellCompDirectiveDefault
}
122 changes: 52 additions & 70 deletions cmd/shared_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"fmt"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -77,73 +79,53 @@ func TestFilterCompletion(t *testing.T) {

}

//
//func TestTagsCompletion(t *testing.T) {
// testCases := map[string]struct {
// toComplete string
// path string
// playbook string
// expect []string
// }{ "multi-level with black should return all vars": {
// toComplete: "",
// path: ProjectMultiLevelPath,
// playbook: "test.yml",
// expect: []string{"customer", "env", "os", "platform"},
// },
// "multi-level with 'c' should should return customer with operators": {
// toComplete: "c",
// path: ProjectMultiLevelPath,
// playbook: "test.yml",
// expect: []string{"customer==", "customer!=", "customer^=", "customer~=", "customer$="},
// },
// }
//
// for testName, testCase := range testCases {
// t.Run(testName, func(t *testing.T) {
// actual, _ := tagsCompletion(testCase.toComplete, testCase.path, testCase.playbook)
// assert.Equal(t, testCase.expect, actual)
// })
// }
//
//}
//
//func TestPlaybookCompletion(t *testing.T) {
// testCases := map[string]struct {
// toComplete string
// path string
// expect []string
// }{"multi-level with black should return all vars": {
// toComplete: "",
// path: ProjectMultiLevelPath,
// expect: []string{"customer", "env", "os", "platform"},
// },
// "multi-level with 'c' should should return customer with operators": {
// toComplete: "c",
// path: ProjectMultiLevelPath,
// expect: []string{"customer==", "customer!=", "customer^=", "customer~=", "customer$="},
// },
// "multi-level with 'p' should return all vars": {
// toComplete: "p",
// path: ProjectMultiLevelPath,
// expect: []string{"platform==", "platform!=", "platform^=", "platform~=", "platform$="},
// },
// "multi-level with 'customer' should return var with operators": {
// toComplete: "",
// path: ProjectMultiLevelPath,
// expect: []string{"customer", "env", "os", "platform"},
// },
// "multi-level with 'customer==' should return var with operators": {
// toComplete: "",
// path: ProjectMultiLevelPath,
// expect: []string{"customer", "env", "os", "platform"},
// },
// }
//
// for testName, testCase := range testCases {
// t.Run(testName, func(t *testing.T) {
// actual, _ := playbookCompletion(testCase.toComplete, testCase.path)
// assert.Equal(t, testCase.expect, actual)
// })
// }
//
//}
func TestTagsCompletion(t *testing.T) {
testCases := map[string]struct {
toComplete string
path string
playbook string
expect []string
}{"multi-level with black should return all vars": {
toComplete: "",
path: ProjectMultiLevelPath,
playbook: "test.yml",
expect: []string{"existing-role", "playtag1", "role-1", "test1-tag", "test2-tag"},
},
}

for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
playbook, _ := filepath.Abs(fmt.Sprintf("%s/%s", testCase.path, testCase.playbook))
actual, _ := tagsCompletion(testCase.toComplete, testCase.path, playbook)
assert.Equal(t, testCase.expect, actual)
})
}

}

func TestPlaybookCompletion(t *testing.T) {
testCases := map[string]struct {
toComplete string
path string
expects []string
}{"multi-level with black should return all vars": {
toComplete: "",
path: ProjectMultiLevelPath,
expects: []string{"test.yml"},
},
}

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

var playbooks []string
for _, expect := range testCase.expects {
playbook, _ := filepath.Abs(fmt.Sprintf("%s/%s", testCase.path, expect))
playbooks = append(playbooks, playbook)
}
actual, _ := playbookCompletion(testCase.toComplete, testCase.path)
assert.Equal(t, playbooks, actual)
})
}

}

0 comments on commit 7e83ca9

Please sign in to comment.