Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Tests on slice #808

Merged
merged 5 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pkg/cmd/delete_formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)
Expand Down Expand Up @@ -200,7 +199,7 @@ func (d deleteFormulaCmd) readFormulas(dir string, currentFormula string) ([]str
return nil, err
}

dirs = sliceutil.Remove(dirs, docsDir)
dirs = removeFromArray(dirs, docsDir)

var groups []string
var formulaOptions []string
Expand All @@ -220,7 +219,7 @@ func (d deleteFormulaCmd) readFormulas(dir string, currentFormula string) ([]str
if response == currentFormula {
return groups, nil
}
dirs = sliceutil.Remove(dirs, srcDir)
dirs = removeFromArray(dirs, srcDir)
}

selected, err := d.inList.List(questionSelectFormulaGroup, dirs)
Expand Down Expand Up @@ -376,8 +375,17 @@ func isFormula(dirs []string) bool {
}

func hasFormulaInDir(dirs []string) bool {
dirs = sliceutil.Remove(dirs, docsDir)
dirs = sliceutil.Remove(dirs, srcDir)
dirs = removeFromArray(dirs, docsDir)
dirs = removeFromArray(dirs, srcDir)

return len(dirs) > 0
}

func removeFromArray(ss []string, r string) []string {
for i, s := range ss {
if s == r {
return append(ss[:i], ss[i+1:]...)
}
}
return ss
}
17 changes: 15 additions & 2 deletions pkg/cmd/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/input"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)

Expand Down Expand Up @@ -116,7 +115,7 @@ func (f FormulaCommand) Add(root *cobra.Command) error {
for _, id := range tree.CommandsID {
cmd := tree.Commands[id]

if sliceutil.ContainsCmd(f.coreCmds, cmd) {
if containsCmd(f.coreCmds, cmd) {
continue
}

Expand Down Expand Up @@ -297,3 +296,17 @@ func path(cmd api.Command) string {
path := strings.ReplaceAll(strings.Replace(cmd.Parent, "root", "", 1), "_", string(os.PathSeparator))
return filepath.Join(path, cmd.Usage)
}

func containsCmd(aa api.Commands, c api.Command) bool {
for _, v := range aa {
if c.Parent == v.Parent && c.Usage == v.Usage {
return true
}

coreCmd := fmt.Sprintf("%s_%s", v.Parent, v.Usage)
if c.Parent == coreCmd { // Ensures that no core commands will be added
return true
}
}
return false
}
14 changes: 13 additions & 1 deletion pkg/cmd/formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/formula"
)

func TestFormulaCommand_Add(t *testing.T) {
func TestAddFormulaCommand(t *testing.T) {
treeMock := treeMock{
tree: formula.Tree{
Commands: api.Commands{
"root_help": {
Parent: "root",
Usage: "help",
Help: "help core command",
},
"root_help_formula": {
Parent: "root_help",
Usage: "formula",
Help: "child of core command",
},
"root_mock": {
Parent: "root",
Usage: "mock",
Expand All @@ -43,6 +53,8 @@ func TestFormulaCommand_Add(t *testing.T) {
},
},
CommandsID: []api.CommandID{
"root_help",
"root_help_formula",
"root_mock",
"root_mock_test",
},
Expand Down
30 changes: 0 additions & 30 deletions pkg/slice/sliceutil/slice_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@

package sliceutil

import (
"fmt"

"github.com/ZupIT/ritchie-cli/pkg/api"
)

// Contains tells whether a contains s.
func Contains(aa []string, s string) bool {
for _, v := range aa {
Expand All @@ -31,27 +25,3 @@ func Contains(aa []string, s string) bool {
}
return false
}

// ContainsCmd tells whether a contains c.
func ContainsCmd(aa api.Commands, c api.Command) bool {
for _, v := range aa {
if c.Parent == v.Parent && c.Usage == v.Usage {
return true
}

coreCmd := fmt.Sprintf("%s_%s", v.Parent, v.Usage)
if c.Parent == coreCmd { // Ensures that no core commands will be added
return true
}
}
return false
}

func Remove(ss []string, r string) []string {
for i, s := range ss {
if s == r {
return append(ss[:i], ss[i+1:]...)
}
}
return ss
}
69 changes: 2 additions & 67 deletions pkg/slice/sliceutil/slice_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package sliceutil

import (
"fmt"
"testing"

"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/stretchr/testify/assert"
)

func TestContains(t *testing.T) {
Expand All @@ -35,71 +34,7 @@ func TestContains(t *testing.T) {
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
got := Contains([]string{"world", "earth", "universe"}, tt.in)
if got != tt.out {
t.Errorf("Contains got %v, want %v", got, tt.out)
}
assert.Equal(t, tt.out, got)
})
}
}

func TestContainsCmd(t *testing.T) {
tests := []struct {
in api.Command
out bool
}{
{api.Command{Parent: "root_set", Usage: "credential"}, true},
{api.Command{Parent: "root", Usage: "notfound"}, false},
{api.Command{Parent: "root", Usage: "add"}, true},
}

for _, tt := range tests {
path := fmt.Sprintf("%s_%s", tt.in.Parent, tt.in.Usage)
t.Run(path, func(t *testing.T) {
got := ContainsCmd(api.CoreCmds, tt.in)
if got != tt.out {
t.Errorf("ContainsCmd got %v, want %v", got, tt.out)
}
})
}
}

func TestRemove(t *testing.T) {
type in struct {
slice []string
remove string
}

tests := []struct {
name string
in in
out int
}{
{
name: "success",
in: in{
slice: []string{"test_1", "test_2", "test_3"},
remove: "test_2",
},
out: 2,
},
{
name: "not remove any",
in: in{
slice: []string{"test_1", "test_2", "test_3"},
remove: "test_0",
},
out: 3,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Remove(tt.in.slice, tt.in.remove)

if tt.out != len(got) {
t.Errorf("Remove(%s) got %v, want %v", tt.name, len(got), tt.out)
}
})
}

}