From 54589f81b590ef814b243ebd06b35da91794c412 Mon Sep 17 00:00:00 2001 From: Rajat Varyani Date: Thu, 7 Feb 2019 09:44:19 +0530 Subject: [PATCH 1/3] [Rajat] Sort proc command in alphabetical order by name --- cmd/list/lister.go | 6 +++--- cmd/list/lister_test.go | 8 ++++++-- cmd/root.go | 5 +++-- cmd/root_test.go | 5 +++-- main.go | 5 +++-- utility/sort/sort.go | 30 +++++++++++++++++++++++++++++ utility/sort/sort_mock.go | 14 ++++++++++++++ utility/sort/sort_test.go | 40 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 utility/sort/sort.go create mode 100644 utility/sort/sort_mock.go create mode 100644 utility/sort/sort_test.go diff --git a/cmd/list/lister.go b/cmd/list/lister.go index 3a2752fe..71dccfb4 100644 --- a/cmd/list/lister.go +++ b/cmd/list/lister.go @@ -7,9 +7,10 @@ import ( "github.com/gojektech/proctor/daemon" "github.com/gojektech/proctor/io" "github.com/spf13/cobra" + "github.com/gojektech/proctor/utility/sort" ) -func NewCmd(printer io.Printer, proctorDClient daemon.Client) *cobra.Command { +func NewCmd(printer io.Printer, proctorDClient daemon.Client, s sort.Sorter) *cobra.Command { return &cobra.Command{ Use: "list", Short: "List procs available for execution", @@ -21,9 +22,8 @@ func NewCmd(printer io.Printer, proctorDClient daemon.Client) *cobra.Command { printer.Println(err.Error(), color.FgRed) return } - printer.Println("List of Procs:\n", color.FgGreen) - + s.Sort(procList) for _, proc := range procList { printer.Println(fmt.Sprintf("%-40s %-100s", proc.Name, proc.Description), color.Reset) } diff --git a/cmd/list/lister_test.go b/cmd/list/lister_test.go index 9982c457..2eb67642 100644 --- a/cmd/list/lister_test.go +++ b/cmd/list/lister_test.go @@ -12,6 +12,7 @@ import ( "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" + "github.com/gojektech/proctor/utility/sort" ) type ListCmdTestSuite struct { @@ -19,12 +20,14 @@ type ListCmdTestSuite struct { mockPrinter *io.MockPrinter mockProctorDClient *daemon.MockClient testListCmd *cobra.Command + sorter *sort.MockSorter } func (s *ListCmdTestSuite) SetupTest() { s.mockPrinter = &io.MockPrinter{} s.mockProctorDClient = &daemon.MockClient{} - s.testListCmd = NewCmd(s.mockPrinter, s.mockProctorDClient) + s.sorter = &sort.MockSorter{} + s.testListCmd = NewCmd(s.mockPrinter, s.mockProctorDClient, s.sorter) } func (s *ListCmdTestSuite) TestListCmdUsage() { @@ -54,11 +57,12 @@ func (s *ListCmdTestSuite) TestListCmdRun() { s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", procOne.Name, procOne.Description), color.Reset).Once() s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", procTwo.Name, procTwo.Description), color.Reset).Once() s.mockPrinter.On("Println", "\nFor detailed information of any proc, run:\nproctor describe ", color.FgGreen).Once() - + s.sorter.On("Sort",procList).Once() s.testListCmd.Run(&cobra.Command{}, []string{}) s.mockProctorDClient.AssertExpectations(s.T()) s.mockPrinter.AssertExpectations(s.T()) + s.sorter.AssertExpectations(s.T()) } func (s *ListCmdTestSuite) TestListCmdRunProctorDClientFailure() { diff --git a/cmd/root.go b/cmd/root.go index c166041f..9d5620e6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,6 +18,7 @@ import ( "github.com/gojektech/proctor/io" "github.com/spf13/cobra" + "github.com/gojektech/proctor/utility/sort" ) var ( @@ -28,7 +29,7 @@ var ( } ) -func Execute(printer io.Printer, proctorDClient daemon.Client) { +func Execute(printer io.Printer, proctorDClient daemon.Client, sorter sort.Sorter) { versionCmd := version.NewCmd(printer) rootCmd.AddCommand(versionCmd) @@ -39,7 +40,7 @@ func Execute(printer io.Printer, proctorDClient daemon.Client) { executionCmd := execution.NewCmd(printer, proctorDClient, os.Exit) rootCmd.AddCommand(executionCmd) - listCmd := list.NewCmd(printer, proctorDClient) + listCmd := list.NewCmd(printer, proctorDClient, sorter) rootCmd.AddCommand(listCmd) configCmd := config.NewCmd(printer) diff --git a/cmd/root_test.go b/cmd/root_test.go index b8ad5807..57726eae 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -7,10 +7,11 @@ import ( "github.com/gojektech/proctor/io" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" + "github.com/gojektech/proctor/utility/sort" ) func TestRootCmdUsage(t *testing.T) { - Execute(&io.MockPrinter{}, &daemon.MockClient{}) + Execute(&io.MockPrinter{}, &daemon.MockClient{}, &sort.MockSorter{}) assert.Equal(t, "proctor", rootCmd.Use) assert.Equal(t, "A command-line interface to run procs", rootCmd.Short) @@ -27,7 +28,7 @@ func contains(commands []*cobra.Command, commandName string) bool { } func TestRootCmdSubCommands(t *testing.T) { - Execute(&io.MockPrinter{}, &daemon.MockClient{}) + Execute(&io.MockPrinter{}, &daemon.MockClient{}, &sort.MockSorter{}) assert.True(t, contains(rootCmd.Commands(), "describe")) assert.True(t, contains(rootCmd.Commands(), "execute")) diff --git a/main.go b/main.go index 2c377f31..80c42af6 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,13 @@ import ( "github.com/gojektech/proctor/config" "github.com/gojektech/proctor/daemon" "github.com/gojektech/proctor/io" + "github.com/gojektech/proctor/utility/sort" ) func main() { printer := io.GetPrinter() proctorConfigLoader := config.NewLoader() proctorDClient := daemon.NewClient(printer, proctorConfigLoader) - - cmd.Execute(printer, proctorDClient) + sorter := sort.GetSorter() + cmd.Execute(printer, proctorDClient, sorter) } diff --git a/utility/sort/sort.go b/utility/sort/sort.go new file mode 100644 index 00000000..b8852b01 --- /dev/null +++ b/utility/sort/sort.go @@ -0,0 +1,30 @@ +package sort + +import ( + "github.com/gojektech/proctor/proctord/jobs/metadata" + "sort" +) + +type Sorter interface { + Sort(procList []metadata.Metadata) +} + +var sorterInstance Sorter + +type commandSorter struct{} + +func GetSorter() Sorter { + if sorterInstance == nil { + sorterInstance = &commandSorter{} + } + return sorterInstance +} + +func (c *commandSorter) Sort(procList []metadata.Metadata) { + sort.Slice(procList, func(i, j int) bool { + if procList[i].Name < procList[j].Name { + return true + } + return false + }) +} diff --git a/utility/sort/sort_mock.go b/utility/sort/sort_mock.go new file mode 100644 index 00000000..b16a527c --- /dev/null +++ b/utility/sort/sort_mock.go @@ -0,0 +1,14 @@ +package sort + +import ( + "github.com/stretchr/testify/mock" + "github.com/gojektech/proctor/proctord/jobs/metadata" +) + +type MockSorter struct { + mock.Mock +} + +func (m *MockSorter) Sort(md []metadata.Metadata) { + m.Called(md) +} diff --git a/utility/sort/sort_test.go b/utility/sort/sort_test.go new file mode 100644 index 00000000..7e4cc450 --- /dev/null +++ b/utility/sort/sort_test.go @@ -0,0 +1,40 @@ +package sort + +import "github.com/stretchr/testify/suite" +import ( + "github.com/gojektech/proctor/proctord/jobs/metadata" + "github.com/stretchr/testify/assert" + "testing" +) + +type SortTestSuite struct { + suite.Suite + sorter *commandSorter +} + +func (s *SortTestSuite) setupTest() { + s.sorter = new(commandSorter) +} + +func (s *SortTestSuite) TestSorting() { + procOne := metadata.Metadata{ + Name: "one", + Description: "proc one description", + } + + procTwo := metadata.Metadata{ + Name: "two", + Description: "proc two description", + } + + procList := []metadata.Metadata{procTwo, procOne} + expectedProcList := []metadata.Metadata{procOne, procTwo} + + s.sorter.Sort(procList) + + assert.Equal(s.T(), expectedProcList, procList) +} + +func TestSortTestSuite(t *testing.T) { + suite.Run(t, new(SortTestSuite)) +} From 145aee1f081ce2070c11194531a4eb95fc2c8753 Mon Sep 17 00:00:00 2001 From: Rajat Varyani Date: Thu, 7 Feb 2019 20:03:31 +0530 Subject: [PATCH 2/3] [Rajat] Restructure sort package --- cmd/list/lister.go | 5 +++-- cmd/list/lister_test.go | 7 +------ cmd/root.go | 5 ++--- cmd/root_test.go | 5 ++--- main.go | 5 ++--- utility/sort/sort.go | 17 +---------------- utility/sort/sort_mock.go | 14 -------------- utility/sort/sort_test.go | 33 +++++++++------------------------ 8 files changed, 20 insertions(+), 71 deletions(-) delete mode 100644 utility/sort/sort_mock.go diff --git a/cmd/list/lister.go b/cmd/list/lister.go index 71dccfb4..769588c7 100644 --- a/cmd/list/lister.go +++ b/cmd/list/lister.go @@ -10,7 +10,7 @@ import ( "github.com/gojektech/proctor/utility/sort" ) -func NewCmd(printer io.Printer, proctorDClient daemon.Client, s sort.Sorter) *cobra.Command { +func NewCmd(printer io.Printer, proctorDClient daemon.Client) *cobra.Command { return &cobra.Command{ Use: "list", Short: "List procs available for execution", @@ -23,7 +23,8 @@ func NewCmd(printer io.Printer, proctorDClient daemon.Client, s sort.Sorter) *co return } printer.Println("List of Procs:\n", color.FgGreen) - s.Sort(procList) + sort.Procs(procList) + for _, proc := range procList { printer.Println(fmt.Sprintf("%-40s %-100s", proc.Name, proc.Description), color.Reset) } diff --git a/cmd/list/lister_test.go b/cmd/list/lister_test.go index 2eb67642..8add6586 100644 --- a/cmd/list/lister_test.go +++ b/cmd/list/lister_test.go @@ -12,7 +12,6 @@ import ( "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - "github.com/gojektech/proctor/utility/sort" ) type ListCmdTestSuite struct { @@ -20,14 +19,12 @@ type ListCmdTestSuite struct { mockPrinter *io.MockPrinter mockProctorDClient *daemon.MockClient testListCmd *cobra.Command - sorter *sort.MockSorter } func (s *ListCmdTestSuite) SetupTest() { s.mockPrinter = &io.MockPrinter{} s.mockProctorDClient = &daemon.MockClient{} - s.sorter = &sort.MockSorter{} - s.testListCmd = NewCmd(s.mockPrinter, s.mockProctorDClient, s.sorter) + s.testListCmd = NewCmd(s.mockPrinter, s.mockProctorDClient) } func (s *ListCmdTestSuite) TestListCmdUsage() { @@ -57,12 +54,10 @@ func (s *ListCmdTestSuite) TestListCmdRun() { s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", procOne.Name, procOne.Description), color.Reset).Once() s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", procTwo.Name, procTwo.Description), color.Reset).Once() s.mockPrinter.On("Println", "\nFor detailed information of any proc, run:\nproctor describe ", color.FgGreen).Once() - s.sorter.On("Sort",procList).Once() s.testListCmd.Run(&cobra.Command{}, []string{}) s.mockProctorDClient.AssertExpectations(s.T()) s.mockPrinter.AssertExpectations(s.T()) - s.sorter.AssertExpectations(s.T()) } func (s *ListCmdTestSuite) TestListCmdRunProctorDClientFailure() { diff --git a/cmd/root.go b/cmd/root.go index 9d5620e6..c166041f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,7 +18,6 @@ import ( "github.com/gojektech/proctor/io" "github.com/spf13/cobra" - "github.com/gojektech/proctor/utility/sort" ) var ( @@ -29,7 +28,7 @@ var ( } ) -func Execute(printer io.Printer, proctorDClient daemon.Client, sorter sort.Sorter) { +func Execute(printer io.Printer, proctorDClient daemon.Client) { versionCmd := version.NewCmd(printer) rootCmd.AddCommand(versionCmd) @@ -40,7 +39,7 @@ func Execute(printer io.Printer, proctorDClient daemon.Client, sorter sort.Sorte executionCmd := execution.NewCmd(printer, proctorDClient, os.Exit) rootCmd.AddCommand(executionCmd) - listCmd := list.NewCmd(printer, proctorDClient, sorter) + listCmd := list.NewCmd(printer, proctorDClient) rootCmd.AddCommand(listCmd) configCmd := config.NewCmd(printer) diff --git a/cmd/root_test.go b/cmd/root_test.go index 57726eae..b8ad5807 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -7,11 +7,10 @@ import ( "github.com/gojektech/proctor/io" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" - "github.com/gojektech/proctor/utility/sort" ) func TestRootCmdUsage(t *testing.T) { - Execute(&io.MockPrinter{}, &daemon.MockClient{}, &sort.MockSorter{}) + Execute(&io.MockPrinter{}, &daemon.MockClient{}) assert.Equal(t, "proctor", rootCmd.Use) assert.Equal(t, "A command-line interface to run procs", rootCmd.Short) @@ -28,7 +27,7 @@ func contains(commands []*cobra.Command, commandName string) bool { } func TestRootCmdSubCommands(t *testing.T) { - Execute(&io.MockPrinter{}, &daemon.MockClient{}, &sort.MockSorter{}) + Execute(&io.MockPrinter{}, &daemon.MockClient{}) assert.True(t, contains(rootCmd.Commands(), "describe")) assert.True(t, contains(rootCmd.Commands(), "execute")) diff --git a/main.go b/main.go index 80c42af6..2c377f31 100644 --- a/main.go +++ b/main.go @@ -5,13 +5,12 @@ import ( "github.com/gojektech/proctor/config" "github.com/gojektech/proctor/daemon" "github.com/gojektech/proctor/io" - "github.com/gojektech/proctor/utility/sort" ) func main() { printer := io.GetPrinter() proctorConfigLoader := config.NewLoader() proctorDClient := daemon.NewClient(printer, proctorConfigLoader) - sorter := sort.GetSorter() - cmd.Execute(printer, proctorDClient, sorter) + + cmd.Execute(printer, proctorDClient) } diff --git a/utility/sort/sort.go b/utility/sort/sort.go index b8852b01..20541344 100644 --- a/utility/sort/sort.go +++ b/utility/sort/sort.go @@ -5,22 +5,7 @@ import ( "sort" ) -type Sorter interface { - Sort(procList []metadata.Metadata) -} - -var sorterInstance Sorter - -type commandSorter struct{} - -func GetSorter() Sorter { - if sorterInstance == nil { - sorterInstance = &commandSorter{} - } - return sorterInstance -} - -func (c *commandSorter) Sort(procList []metadata.Metadata) { +func Procs(procList []metadata.Metadata) { sort.Slice(procList, func(i, j int) bool { if procList[i].Name < procList[j].Name { return true diff --git a/utility/sort/sort_mock.go b/utility/sort/sort_mock.go deleted file mode 100644 index b16a527c..00000000 --- a/utility/sort/sort_mock.go +++ /dev/null @@ -1,14 +0,0 @@ -package sort - -import ( - "github.com/stretchr/testify/mock" - "github.com/gojektech/proctor/proctord/jobs/metadata" -) - -type MockSorter struct { - mock.Mock -} - -func (m *MockSorter) Sort(md []metadata.Metadata) { - m.Called(md) -} diff --git a/utility/sort/sort_test.go b/utility/sort/sort_test.go index 7e4cc450..9fa83ed1 100644 --- a/utility/sort/sort_test.go +++ b/utility/sort/sort_test.go @@ -1,40 +1,25 @@ package sort -import "github.com/stretchr/testify/suite" import ( "github.com/gojektech/proctor/proctord/jobs/metadata" "github.com/stretchr/testify/assert" "testing" ) -type SortTestSuite struct { - suite.Suite - sorter *commandSorter -} - -func (s *SortTestSuite) setupTest() { - s.sorter = new(commandSorter) -} - -func (s *SortTestSuite) TestSorting() { +func TestSorting(t *testing.T) { procOne := metadata.Metadata{ - Name: "one", - Description: "proc one description", - } + Name: "one"} procTwo := metadata.Metadata{ - Name: "two", - Description: "proc two description", - } + Name: "two"} - procList := []metadata.Metadata{procTwo, procOne} - expectedProcList := []metadata.Metadata{procOne, procTwo} + procThree := metadata.Metadata{ + Name: "three"} - s.sorter.Sort(procList) + procList := []metadata.Metadata{procThree, procTwo, procOne} + expectedProcList := []metadata.Metadata{procOne, procThree, procTwo} - assert.Equal(s.T(), expectedProcList, procList) -} + Procs(procList) -func TestSortTestSuite(t *testing.T) { - suite.Run(t, new(SortTestSuite)) + assert.Equal(t, expectedProcList, procList) } From a148b3f9247741d0e8956b87fe9e58f3f49fd23e Mon Sep 17 00:00:00 2001 From: Rajat Varyani Date: Mon, 11 Feb 2019 12:33:07 +0530 Subject: [PATCH 3/3] [Rajat] Refactor sort func --- utility/sort/sort.go | 5 +---- utility/sort/sort_test.go | 12 +++--------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/utility/sort/sort.go b/utility/sort/sort.go index 20541344..cbc5cb97 100644 --- a/utility/sort/sort.go +++ b/utility/sort/sort.go @@ -7,9 +7,6 @@ import ( func Procs(procList []metadata.Metadata) { sort.Slice(procList, func(i, j int) bool { - if procList[i].Name < procList[j].Name { - return true - } - return false + return procList[i].Name < procList[j].Name }) } diff --git a/utility/sort/sort_test.go b/utility/sort/sort_test.go index 9fa83ed1..481fc597 100644 --- a/utility/sort/sort_test.go +++ b/utility/sort/sort_test.go @@ -7,15 +7,9 @@ import ( ) func TestSorting(t *testing.T) { - procOne := metadata.Metadata{ - Name: "one"} - - procTwo := metadata.Metadata{ - Name: "two"} - - procThree := metadata.Metadata{ - Name: "three"} - + procOne := metadata.Metadata{Name: "one"} + procTwo := metadata.Metadata{Name: "two"} + procThree := metadata.Metadata{Name: "three"} procList := []metadata.Metadata{procThree, procTwo, procOne} expectedProcList := []metadata.Metadata{procOne, procThree, procTwo}