Skip to content

Commit

Permalink
IMPROVEMENT-47: Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grafviktor committed Sep 30, 2024
1 parent 30611e9 commit b0cd223
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/model/host/host_nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package host
import (
"testing"

"github.com/grafviktor/goto/internal/model/ssh"
"github.com/stretchr/testify/require"

"github.com/grafviktor/goto/internal/model/ssh"
)

func TestIsUserDefinedSSHCommand(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion internal/model/host/host_win_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"os"
"testing"

"github.com/grafviktor/goto/internal/model/ssh"
"github.com/stretchr/testify/require"

"github.com/grafviktor/goto/internal/model/ssh"
)

// The only difference between Unix and Windows version tests is that Windows
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/component/hostlist/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (k *keyMap) FullHelp() []key.Binding {
k.clone,
k.edit,
k.remove,
k.toggleLayout,
k.copyID,
k.toggleLayout,
}
}
140 changes: 140 additions & 0 deletions internal/ui/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/grafviktor/goto/internal/constant"
"github.com/grafviktor/goto/internal/model/ssh"
"github.com/grafviktor/goto/internal/state"
"github.com/grafviktor/goto/internal/test"
"github.com/grafviktor/goto/internal/ui/message"
Expand Down Expand Up @@ -84,6 +86,8 @@ func TestDispatchProcess_Foreground(t *testing.T) {
// callbackFn.Call(argVals)
}

// This test is failing in a real Windows environment with error 'exec: "echo": executable file not found in %PATH%'.
// Low priority though as it works in gitlab tests for Windows platform. Requires investigation.
func TestDispatchProcess_Background_OK(t *testing.T) {
// Create a model
model := New(context.TODO(), test.NewMockStorage(true), MockAppState(), &test.MockLogger{})
Expand Down Expand Up @@ -120,6 +124,142 @@ func TestDispatchProcess_Background_Fail(t *testing.T) {
require.IsType(t, message.RunProcessErrorOccurred{}, result)
}

func TestHandleProcessSuccess_SSH_load_config(t *testing.T) {
model := New(context.TODO(), test.NewMockStorage(true), MockAppState(), &test.MockLogger{})
given := message.RunProcessSuccess{
ProcessType: constant.ProcessTypeSSHLoadConfig,
StdOut: "hostname localhost\r\nport 2222\r\nidentityfile /tmp\r\nuser root",
}

expected := message.HostSSHConfigLoaded{
HostID: 0,
Config: ssh.Config{
Hostname: "localhost",
IdentityFile: "/tmp",
Port: "2222",
User: "root",
},
}

actual := model.handleProcessSuccess(given)()
require.Equal(t, expected, actual)
}

func TestHandleProcessSuccess_SSH_copy_ID(t *testing.T) {
type expected struct {
modelMessage string
viewState int
}

tests := []struct {
name string
msg message.RunProcessSuccess
expected expected
}{
{
name: "Handle SSH copy ID when process output contains an error",
msg: message.RunProcessSuccess{
ProcessType: constant.ProcessTypeSSHCopyID,
StdOut: "normal output",
StdErr: "foo ERROR bar",
},
expected: expected{
modelMessage: "foo ERROR bar",
viewState: (int)(state.ViewMessage),
},
},
{
name: "Handle SSH copy ID when process output contains a warning message",
msg: message.RunProcessSuccess{
ProcessType: constant.ProcessTypeSSHCopyID,
StdOut: "normal output",
StdErr: "foo WARNING bar",
},
expected: expected{
modelMessage: "foo WARNING bar",
viewState: (int)(state.ViewMessage),
},
},
{
name: "Handle SSH copy ID when it ended successfully",
msg: message.RunProcessSuccess{
ProcessType: constant.ProcessTypeSSHCopyID,
StdOut: "normal output",
StdErr: "foo SOMETHING bar",
},
expected: expected{
modelMessage: "normal output",
viewState: (int)(state.ViewMessage),
},
},
{
name: "Unsupported process",
msg: message.RunProcessSuccess{
ProcessType: constant.ProcessTypeSSHConnect,
StdOut: "normal output",
StdErr: "foo SOMETHING bar",
},
expected: expected{
modelMessage: "",
viewState: (int)(state.ViewHostList),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
model := New(context.TODO(), test.NewMockStorage(true), MockAppState(), &test.MockLogger{})
model.handleProcessSuccess(tt.msg)
require.Equal(t, tt.expected.modelMessage, model.viewMessageContent)
require.Equal(t, tt.expected.viewState, (int)(model.appState.CurrentView))
})
}
}

func TestHandleProcessError(t *testing.T) {
type expected struct {
modelMessage string
viewState int
}

tests := []struct {
name string
msg message.RunProcessErrorOccurred
expected expected
}{
{
name: "Handle process error stderr and stdout are populated",
msg: message.RunProcessErrorOccurred{
StdOut: "normal output",
StdErr: "error output",
},
expected: expected{
modelMessage: "error output\nDetails: normal output",
viewState: (int)(state.ViewMessage),
},
},
{
name: "Handle process error only stderr is populated",
msg: message.RunProcessErrorOccurred{
StdErr: "error output",
},
expected: expected{
modelMessage: "error output",
viewState: (int)(state.ViewMessage),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
model := New(context.TODO(), test.NewMockStorage(true), MockAppState(), &test.MockLogger{})
model.handleProcessError(tt.msg)
require.Equal(t, tt.expected.modelMessage, model.viewMessageContent)
require.Equal(t, tt.expected.viewState, (int)(model.appState.CurrentView))
})
}
}

// ---------------------------------

func MockAppState() *state.ApplicationState {
Expand Down

0 comments on commit b0cd223

Please sign in to comment.