Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX-60: Isolate ssh-related functions in a separate module. #62

Merged
merged 20 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5bf51d3
BUGFIX-60: Isolate ssh-related functions in a separate module.
grafviktor Mar 5, 2024
44783f5
BUGFIX-60: Move run process handlers into the main module
grafviktor Mar 7, 2024
9130bb1
BUGFIX-60: When leave host field in edit form, re-read ssh/config fil…
grafviktor Mar 8, 2024
040a0a7
BUGFIX-60: Load ssh config when leaving address field. In progress.
grafviktor Mar 22, 2024
e803760
BUGFIX-60: Run load ssh config command output in the background
grafviktor Mar 23, 2024
681602f
BUGFIX-60: Load host config when edit hostname. In progress
grafviktor Mar 25, 2024
7244c7b
BUGFIX-60: Load host config after hostname change
grafviktor Mar 26, 2024
f83ddc0
BUGFIX-60: Debounce read ssh config message
grafviktor Mar 27, 2024
22657e8
BUGFIX-60: Update failing unit tests
grafviktor Apr 1, 2024
6bb9a8a
BUGIFX-60: Increase unit test coverage
grafviktor Apr 4, 2024
ccefb5a
BUGFIX-60: Improve unit test utility functions
grafviktor Apr 7, 2024
c1082a4
BUGFIX-60: Add codecove token reference to gitflow task
grafviktor Apr 9, 2024
edb2591
BUGFIX-60: Unskip failing unit tests
grafviktor Apr 10, 2024
d45addd
BUGFIX-60: Increase test coverage
grafviktor Apr 15, 2024
2152f5c
BUGFIX-60: Increase test coverage. Move mock objects to 'internal/tes…
grafviktor Apr 15, 2024
4f6ac6f
BUGFIX-60: Add clean task to Makefile
grafviktor Apr 16, 2024
9e1bec0
BUGFIX-60: Move input component into a eparate folder
grafviktor Apr 16, 2024
526ddc0
BUGFIX-60: Fix ssh config regex for windows systems
grafviktor Apr 16, 2024
f03f4d2
BUGFIX-60: Fix linting errors
grafviktor Apr 16, 2024
a002cd1
BUGFIX-60: Increase uit test coverage
grafviktor Apr 16, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ jobs:
file: ./unit.txt
flags: unit,${{ matrix.os }},go-${{ matrix.go-version }}
name: unit
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ run:
# - ".*\\.my\\.go$"
# - lib/bad.go
skip-dirs:
- internal/mock
- internal/test

# output configuration options
output:
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ lint:
## audit: tidy dependencies and format, vet and test all code
.PHONY: audit
audit:
@echo 'If something is not right, make sure these utilities are installed:'
@echo ' * If gofumpt is not installed, then: go install mvdan.cc/gofumpt@latest'
@echo ' * If goimports is not installed, then: go install golang.org/x/tools/cmd/goimports@latest'
@echo ' * For golangci-lint: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.57.2'
@echo 'Tidying and verifying module dependencies...'
go mod tidy
go mod verify
Expand Down Expand Up @@ -92,3 +96,10 @@ dist:
@cp $(DIST_PATH)/gg-mac $(DIST_PATH)/gg-lin $(DIST_PATH)/gg-win.exe $(DIST_PATH)/goto-$(BUILD_VERSION)
@cd $(DIST_PATH) && zip -r goto-$(BUILD_VERSION).zip goto-$(BUILD_VERSION)
@rm -r $(DIST_PATH)/goto-$(BUILD_VERSION)

## clean: remove ./dist folder with all its contents.
.PHONY: clean
clean:
@echo 'Cleaning'
@-rm -r $(DIST_PATH) 2>/dev/null
@echo 'Done'
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ignore:
- "cmd/*"
- "internal/mock/*"
- "internal/test/*"
4 changes: 2 additions & 2 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
)

func Test_LoggerConstructor(t *testing.T) {
func TestLoggerConstructor(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "testlog")
if err != nil {
Expand Down Expand Up @@ -66,7 +66,7 @@ func Test_LoggerConstructor(t *testing.T) {
}
}

func Test_LoggerMethods(t *testing.T) {
func TestLoggerMethods(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "testlog")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/model/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)

func Test_NewHost(t *testing.T) {
func TestNewHost(t *testing.T) {
// Create a new host using the NewHost function
expectedHost := Host{
ID: 1,
Expand All @@ -28,7 +28,7 @@ func Test_NewHost(t *testing.T) {
}
}

func Test_CloneHost(t *testing.T) {
func TestCloneHost(t *testing.T) {
// Create a host to clone
originalHost := Host{
ID: 1,
Expand Down
14 changes: 9 additions & 5 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"

"gopkg.in/yaml.v2"

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

type view int
Expand All @@ -16,7 +18,7 @@ const (
ViewHostList view = iota
// ViewEditItem mode is active when we edit existing or add a new host.
ViewEditItem
// ViewErrorMessage mode is active when there was an error when attenpted to connect to a remote host.
// ViewErrorMessage mode is active when there was an error when attempted to connect to a remote host.
ViewErrorMessage
)

Expand All @@ -37,10 +39,11 @@ type ApplicationState struct {
Selected int `yaml:"selected"`
appStateFilePath string
logger iLogger
CurrentView view `yaml:"-"`
Err error `yaml:"-"`
Width int `yaml:"-"`
Height int `yaml:"-"`
CurrentView view `yaml:"-"`
Err error `yaml:"-"`
Width int `yaml:"-"`
Height int `yaml:"-"`
HostSSHConfig *ssh.Config `yaml:"-"`
}

// Get - reads application state from disk.
Expand All @@ -50,6 +53,7 @@ func Get(appHomePath string, lg iLogger) *ApplicationState {
appState = &ApplicationState{
appStateFilePath: path.Join(appHomePath, stateFile),
logger: lg,
HostSSHConfig: ssh.DefaultConfig(),
}

// If we cannot read previously created application state, that's fine - we can continue execution.
Expand Down
8 changes: 4 additions & 4 deletions internal/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"

"github.com/grafviktor/goto/internal/mock"
"github.com/grafviktor/goto/internal/test"
)

// That's a wrapper function for state.Get which is required to overcome sync.Once restrictions
func stateGet(tempDir string, mockLogger *mock.MockLogger) *ApplicationState {
func stateGet(tempDir string, mockLogger *test.MockLogger) *ApplicationState {
appState := Get(tempDir, mockLogger)

// We need this hack because state.Get function utilizes `sync.once`. That means, if all unit tests
Expand All @@ -34,7 +34,7 @@ func Test_GetApplicationState(t *testing.T) {
defer os.RemoveAll(tempDir)

// Create a mock logger for testing
mockLogger := &mock.MockLogger{}
mockLogger := &test.MockLogger{}

// Call the Get function with the temporary directory and mock logger
appState := stateGet(tempDir, mockLogger)
Expand All @@ -57,7 +57,7 @@ func Test_PersistApplicationState(t *testing.T) {
defer os.RemoveAll(tempDir)

// Create a mock logger for testing
mockLogger := &mock.MockLogger{}
mockLogger := &test.MockLogger{}

// Call the Get function with the temporary directory and mock logger
appState := stateGet(tempDir, mockLogger)
Expand Down
2 changes: 1 addition & 1 deletion internal/mock/logger.go → internal/test/mock_logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mock
package test

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion internal/mock/storage.go → internal/test/mock_storage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mock
package test

import (
"errors"
Expand Down
30 changes: 30 additions & 0 deletions internal/test/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Package test contains utility methods for unit tests.
package test

import (
"reflect"

tea "github.com/charmbracelet/bubbletea"
)

// CmdToMessage - should only be used in unit tests.
func CmdToMessage(cmd tea.Cmd, messages *[]tea.Msg) {
message := cmd()
valueOf := reflect.ValueOf(message)

// Slice of messages is returned by tea.BatchMsg or tea.sequenceMsg
if valueOf.Kind() == reflect.Slice {
for i := 0; i < valueOf.Len(); i++ {
if valueOf.Index(i).Kind() == reflect.Func {
// If it's a function, then it's probably inner tea.Cmd
innerCmd := valueOf.Index(i).Interface().(tea.Cmd)
CmdToMessage(innerCmd, messages)
} else {
// Otherwise it's a slice of real messages
*messages = append(*messages, message)
}
}
} else {
*messages = append(*messages, message)
}
}
Loading
Loading