From 7e83ca9f2cd8e9f008863cfdf647251ba372331a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Blaise?= Date: Wed, 27 Jan 2021 11:59:36 +0100 Subject: [PATCH] Add TestTagsCompletion and TestPlaybookCompletion --- cmd/shared.go | 9 +++- cmd/shared_test.go | 122 +++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 72 deletions(-) diff --git a/cmd/shared.go b/cmd/shared.go index ea6c49b..3733409 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -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 } diff --git a/cmd/shared_test.go b/cmd/shared_test.go index 864a40c..90a737d 100644 --- a/cmd/shared_test.go +++ b/cmd/shared_test.go @@ -1,7 +1,9 @@ package cmd import ( + "fmt" "github.com/stretchr/testify/assert" + "path/filepath" "testing" ) @@ -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) + }) + } + +}