From ff03be0711685396e97a16dc78ebac7073de1728 Mon Sep 17 00:00:00 2001 From: Henrique Moraes Date: Sat, 19 Dec 2020 18:13:10 -0300 Subject: [PATCH 1/5] Refactoring sliceutil Signed-off-by: Henrique Moraes --- pkg/cmd/delete_formula.go | 18 ++-- pkg/cmd/formula.go | 17 +++- pkg/slice/sliceutil/slice_util_test.go | 105 ------------------------ pkg/{slice => }/sliceutil/slice_util.go | 30 ------- pkg/sliceutil/slice_util_test.go | 40 +++++++++ 5 files changed, 68 insertions(+), 142 deletions(-) delete mode 100644 pkg/slice/sliceutil/slice_util_test.go rename pkg/{slice => }/sliceutil/slice_util.go (58%) create mode 100644 pkg/sliceutil/slice_util_test.go diff --git a/pkg/cmd/delete_formula.go b/pkg/cmd/delete_formula.go index d3d93622b..5257197fb 100644 --- a/pkg/cmd/delete_formula.go +++ b/pkg/cmd/delete_formula.go @@ -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" ) @@ -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 @@ -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) @@ -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 +} diff --git a/pkg/cmd/formula.go b/pkg/cmd/formula.go index ffc464441..0bfe8499a 100644 --- a/pkg/cmd/formula.go +++ b/pkg/cmd/formula.go @@ -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" ) @@ -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 } @@ -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 +} diff --git a/pkg/slice/sliceutil/slice_util_test.go b/pkg/slice/sliceutil/slice_util_test.go deleted file mode 100644 index d0b1b0512..000000000 --- a/pkg/slice/sliceutil/slice_util_test.go +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sliceutil - -import ( - "fmt" - "testing" - - "github.com/ZupIT/ritchie-cli/pkg/api" -) - -func TestContains(t *testing.T) { - tests := []struct { - in string - out bool - }{ - {"world", true}, - {"notfound", false}, - } - - 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) - } - }) - } -} - -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) - } - }) - } - -} diff --git a/pkg/slice/sliceutil/slice_util.go b/pkg/sliceutil/slice_util.go similarity index 58% rename from pkg/slice/sliceutil/slice_util.go rename to pkg/sliceutil/slice_util.go index 4c8fcf450..696b11988 100644 --- a/pkg/slice/sliceutil/slice_util.go +++ b/pkg/sliceutil/slice_util.go @@ -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 { @@ -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 -} diff --git a/pkg/sliceutil/slice_util_test.go b/pkg/sliceutil/slice_util_test.go new file mode 100644 index 000000000..e0d253b3f --- /dev/null +++ b/pkg/sliceutil/slice_util_test.go @@ -0,0 +1,40 @@ +/* + * Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sliceutil + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestContains(t *testing.T) { + tests := []struct { + in string + out bool + }{ + {"world", true}, + {"notfound", false}, + } + + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + got := Contains([]string{"world", "earth", "universe"}, tt.in) + assert.Equal(t, tt.out, got) + }) + } +} From c3efe1a93b231b6cf4f2132a7b887f0d8467041a Mon Sep 17 00:00:00 2001 From: Henrique Moraes Date: Sat, 19 Dec 2020 18:15:09 -0300 Subject: [PATCH 2/5] linter Signed-off-by: Henrique Moraes --- pkg/autocomplete/generator.go | 2 +- pkg/cmd/root.go | 2 +- pkg/env/setter.go | 2 +- pkg/formula/formula.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/autocomplete/generator.go b/pkg/autocomplete/generator.go index 5d0c2da22..ef46b46f6 100644 --- a/pkg/autocomplete/generator.go +++ b/pkg/autocomplete/generator.go @@ -26,7 +26,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/api" "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/sliceutil" ) const ( diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index a5b326b45..a893db9f2 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -32,7 +32,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/formula/tree" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/rtutorial" - "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" + "github.com/ZupIT/ritchie-cli/pkg/sliceutil" "github.com/ZupIT/ritchie-cli/pkg/stream" "github.com/ZupIT/ritchie-cli/pkg/version" ) diff --git a/pkg/env/setter.go b/pkg/env/setter.go index 884ad2370..159ca796c 100644 --- a/pkg/env/setter.go +++ b/pkg/env/setter.go @@ -21,7 +21,7 @@ import ( "path/filepath" "strings" - "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" + "github.com/ZupIT/ritchie-cli/pkg/sliceutil" "github.com/ZupIT/ritchie-cli/pkg/stream" ) diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index 9f19a2709..30a41c18f 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -26,7 +26,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/os/osutil" - "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" + "github.com/ZupIT/ritchie-cli/pkg/sliceutil" ) const ( From 6e7673f8a4b0c11e0c684eeed2277ea77241d3ea Mon Sep 17 00:00:00 2001 From: Henrique Moraes Date: Mon, 21 Dec 2020 15:21:11 -0300 Subject: [PATCH 3/5] Adding coveraage case Signed-off-by: Henrique Moraes --- pkg/cmd/formula_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/formula_test.go b/pkg/cmd/formula_test.go index 1567b56c0..41f570cd7 100644 --- a/pkg/cmd/formula_test.go +++ b/pkg/cmd/formula_test.go @@ -26,10 +26,15 @@ 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_mock": { Parent: "root", Usage: "mock", @@ -43,6 +48,7 @@ func TestFormulaCommand_Add(t *testing.T) { }, }, CommandsID: []api.CommandID{ + "root_help", "root_mock", "root_mock_test", }, From 10a453f2eb20bb1b812d744de61c4ec0c583b8d8 Mon Sep 17 00:00:00 2001 From: Henrique Moraes Date: Mon, 21 Dec 2020 15:23:59 -0300 Subject: [PATCH 4/5] One more coverage case Signed-off-by: Henrique Moraes --- pkg/cmd/formula_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/cmd/formula_test.go b/pkg/cmd/formula_test.go index 41f570cd7..2456a265e 100644 --- a/pkg/cmd/formula_test.go +++ b/pkg/cmd/formula_test.go @@ -35,6 +35,11 @@ func TestAddFormulaCommand(t *testing.T) { 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", @@ -49,6 +54,7 @@ func TestAddFormulaCommand(t *testing.T) { }, CommandsID: []api.CommandID{ "root_help", + "root_help_formula", "root_mock", "root_mock_test", }, From 89eba80039e95311cf7c2a92c664ac73a79a15da Mon Sep 17 00:00:00 2001 From: Henrique Moraes Date: Wed, 23 Dec 2020 07:51:09 -0300 Subject: [PATCH 5/5] Moving sliceutil inside slice Signed-off-by: Henrique Moraes --- pkg/autocomplete/generator.go | 2 +- pkg/cmd/root.go | 2 +- pkg/env/setter.go | 2 +- pkg/formula/formula.go | 2 +- pkg/{ => slice}/sliceutil/slice_util.go | 0 pkg/{ => slice}/sliceutil/slice_util_test.go | 0 6 files changed, 4 insertions(+), 4 deletions(-) rename pkg/{ => slice}/sliceutil/slice_util.go (100%) rename pkg/{ => slice}/sliceutil/slice_util_test.go (100%) diff --git a/pkg/autocomplete/generator.go b/pkg/autocomplete/generator.go index ef46b46f6..5d0c2da22 100644 --- a/pkg/autocomplete/generator.go +++ b/pkg/autocomplete/generator.go @@ -26,7 +26,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/formula" "github.com/ZupIT/ritchie-cli/pkg/prompt" - "github.com/ZupIT/ritchie-cli/pkg/sliceutil" + "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" ) const ( diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index a893db9f2..a5b326b45 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -32,7 +32,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/formula/tree" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/rtutorial" - "github.com/ZupIT/ritchie-cli/pkg/sliceutil" + "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" "github.com/ZupIT/ritchie-cli/pkg/stream" "github.com/ZupIT/ritchie-cli/pkg/version" ) diff --git a/pkg/env/setter.go b/pkg/env/setter.go index 159ca796c..884ad2370 100644 --- a/pkg/env/setter.go +++ b/pkg/env/setter.go @@ -21,7 +21,7 @@ import ( "path/filepath" "strings" - "github.com/ZupIT/ritchie-cli/pkg/sliceutil" + "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" "github.com/ZupIT/ritchie-cli/pkg/stream" ) diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index 30a41c18f..9f19a2709 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -26,7 +26,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/os/osutil" - "github.com/ZupIT/ritchie-cli/pkg/sliceutil" + "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" ) const ( diff --git a/pkg/sliceutil/slice_util.go b/pkg/slice/sliceutil/slice_util.go similarity index 100% rename from pkg/sliceutil/slice_util.go rename to pkg/slice/sliceutil/slice_util.go diff --git a/pkg/sliceutil/slice_util_test.go b/pkg/slice/sliceutil/slice_util_test.go similarity index 100% rename from pkg/sliceutil/slice_util_test.go rename to pkg/slice/sliceutil/slice_util_test.go