Skip to content

Commit

Permalink
feature: add unit tests for the context selection
Browse files Browse the repository at this point in the history
  • Loading branch information
orbatschow committed Apr 22, 2023
1 parent b362260 commit 5a9b1a2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 82 deletions.
42 changes: 1 addition & 41 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package context
import (
"fmt"
"os"
"sort"

"github.com/orbatschow/kontext/pkg/config"
"github.com/orbatschow/kontext/pkg/kubeconfig"
"github.com/orbatschow/kontext/pkg/logger"
"github.com/orbatschow/kontext/pkg/state"
"github.com/pterm/pterm"
"github.com/samber/lo"
"k8s.io/client-go/tools/clientcmd/api"
)

Expand Down Expand Up @@ -91,7 +89,7 @@ func (c *Client) Set(contextName string) error {
}

if len(contextName) == 0 {
printer, err := c.selectContext()
printer, err := c.buildInteractiveSelectPrinter()
if err != nil {
return err
}
Expand All @@ -114,44 +112,6 @@ func (c *Client) Set(contextName string) error {
return nil
}

// start an interactive context selection
func (c *Client) selectContext() (*pterm.InteractiveSelectPrinter, error) {
// compute all selection options
var keys []string
for k := range c.APIConfig.Contexts {
keys = append(keys, k)
}

// get the active group
group, ok := lo.Find(c.Config.Group.Items, func(item config.GroupItem) bool {
return item.Name == c.State.Group.Active
})
if !ok {
return nil, fmt.Errorf("could not find active group: '%s'", c.State.Group.Active)
}

// sort the selection
switch group.Context.Selection.Sort {
case SortAsc:
sort.Strings(keys)
case SortDesc:
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
default:
sort.Strings(keys)
}

// set the default selection option
if len(group.Context.Selection.Default) > 0 {
return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys).
WithDefaultOption(group.Context.Selection.Default), nil
}
return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys), nil
}

func (c *Client) Print(contexts map[string]*api.Context) error {
table := pterm.TableData{
{"Active", "Name", "Cluster", "AuthInfo"},
Expand Down
8 changes: 6 additions & 2 deletions pkg/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ func (c *Client) Set(groupName string) error {
groupName = string(history[len(history)-2])
}

var err error
if len(groupName) == 0 {
groupName, err = c.buildInteractiveSelectPrinter().Show()
printer, err := c.buildInteractiveSelectPrinter()
if err != nil {
return err
}

groupName, err = printer.Show()
if err != nil {
return err
}
Expand Down
19 changes: 15 additions & 4 deletions pkg/group/selection.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package group

import (
"fmt"
"sort"

"github.com/orbatschow/kontext/pkg/config"
"github.com/pterm/pterm"
"github.com/samber/lo"
)

// start an interactive context selection
func (c *Client) buildInteractiveSelectPrinter() *pterm.InteractiveSelectPrinter {
// start an interactive group selection
func (c *Client) buildInteractiveSelectPrinter() (*pterm.InteractiveSelectPrinter, error) {
// compute all selection options
var keys []string
for _, value := range c.Config.Group.Items {
Expand All @@ -24,12 +27,20 @@ func (c *Client) buildInteractiveSelectPrinter() *pterm.InteractiveSelectPrinter

// set the default selection option
if len(c.Config.Group.Selection.Default) > 0 {
// get the default selection group
_, ok := lo.Find(c.Config.Group.Items, func(item config.GroupItem) bool {
return item.Name == c.Config.Group.Selection.Default
})
if !ok {
return nil, fmt.Errorf("could not find default selection group: '%s'", c.State.Group.Active)
}

return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys).
WithDefaultOption(c.Config.Group.Selection.Default)
WithDefaultOption(c.Config.Group.Selection.Default), nil
}
return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys)
WithOptions(keys), nil
}
99 changes: 64 additions & 35 deletions pkg/group/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
State *state.State
}
tests := []struct {
name string
args args
want *pterm.InteractiveSelectPrinter
name string
args args
want *pterm.InteractiveSelectPrinter
wantErr bool
}{
{
name: "should return a printer, that sorts the given group as they are given",
Expand All @@ -27,13 +28,13 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
Group: config.Group{
Items: []config.GroupItem{
{
Name: "b",
Name: "group-b",
},
{
Name: "c",
Name: "group-c",
},
{
Name: "a",
Name: "group-a",
},
},
},
Expand All @@ -46,9 +47,9 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
},
DefaultText: "Please select an option",
Options: []string{
"b",
"c",
"a",
"group-b",
"group-c",
"group-a",
},
OptionStyle: &pterm.Style{
pterm.FgDefault,
Expand All @@ -61,24 +62,25 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
pterm.FgLightMagenta,
},
},
wantErr: false,
},
{
name: "should return a printer, that sorts the given group ascending",
args: args{
Config: &config.Config{
Group: config.Group{
Selection: config.Selection{
Sort: "asc",
Sort: SortAsc,
},
Items: []config.GroupItem{
{
Name: "c",
Name: "group-c",
},
{
Name: "b",
Name: "group-b",
},
{
Name: "a",
Name: "group-a",
},
},
},
Expand All @@ -91,9 +93,9 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
},
DefaultText: "Please select an option",
Options: []string{
"a",
"b",
"c",
"group-a",
"group-b",
"group-c",
},
OptionStyle: &pterm.Style{
pterm.FgDefault,
Expand All @@ -106,24 +108,26 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
pterm.FgLightMagenta,
},
},
wantErr: false,
},

{
name: "should return a printer, that sorts the given group descending",
args: args{
Config: &config.Config{
Group: config.Group{
Selection: config.Selection{
Sort: "desc",
Sort: SortDesc,
},
Items: []config.GroupItem{
{
Name: "a",
Name: "group-a",
},
{
Name: "b",
Name: "group-b",
},
{
Name: "c",
Name: "group-c",
},
},
},
Expand All @@ -136,9 +140,9 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
},
DefaultText: "Please select an option",
Options: []string{
"c",
"b",
"a",
"group-c",
"group-b",
"group-a",
},
OptionStyle: &pterm.Style{
pterm.FgDefault,
Expand All @@ -151,25 +155,26 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
pterm.FgLightMagenta,
},
},
wantErr: false,
},
{
name: "should return a printer, that sorts the given group descending and set a default",
args: args{
Config: &config.Config{
Group: config.Group{
Selection: config.Selection{
Sort: "desc",
Default: "c",
Sort: SortDesc,
Default: "group-c",
},
Items: []config.GroupItem{
{
Name: "a",
Name: "group-a",
},
{
Name: "b",
Name: "group-b",
},
{
Name: "c",
Name: "group-c",
},
},
},
Expand All @@ -182,21 +187,38 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
},
DefaultText: "Please select an option",
Options: []string{
"c",
"b",
"a",
"group-c",
"group-b",
"group-a",
},
OptionStyle: &pterm.Style{
pterm.FgDefault,
pterm.BgDefault,
},
DefaultOption: "c",
DefaultOption: "group-c",
MaxHeight: MaxSelectHeight,
Selector: ">",
SelectorStyle: &pterm.Style{
pterm.FgLightMagenta,
},
},
wantErr: false,
},
{
name: "should throw an error, as the default selection group does not exist",
args: args{
Config: &config.Config{
Group: config.Group{
Selection: config.Selection{
Default: "dev",
},
Items: []config.GroupItem{},
},
},
State: &state.State{},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
Expand All @@ -206,12 +228,19 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
State: tt.args.State,
}

got := client.buildInteractiveSelectPrinter()
got, err := client.buildInteractiveSelectPrinter()
if !tt.wantErr && err != nil {
t.Errorf("unexpected error, err: '%v'", err)
}

if tt.wantErr && err == nil {
t.Errorf("expected error, got: '%v'", err)
}

options := cmpopts.IgnoreUnexported(pterm.InteractiveSelectPrinter{})
if !cmp.Equal(&tt.want, &got, options) {
if !tt.wantErr && !cmp.Equal(&tt.want, &got, options) {
diff := cmp.Diff(tt.want, got, options)
t.Errorf("group.Set() state mismatch (-want +got):\n%s", diff)
t.Errorf("group.buildInteractiveSelectPrinter() mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down

0 comments on commit 5a9b1a2

Please sign in to comment.