Skip to content

Commit

Permalink
IMPROVEMENT-24: Move mocks into a separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
grafviktor committed Dec 31, 2023
1 parent 790c038 commit 70b616b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
go-version: ${{ matrix.go-version }}
cache: true
- name: Test
run: go test -coverpkg=./internal/... -race -vet=off -count=1 -coverprofile unit.txt -covermode atomic ./...
run: go test -coverpkg=$(go list ./internal/...|grep -v mock) -race -vet=off -count=1 -coverprofile unit.txt -covermode atomic ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ run:
# skip-files:
# - ".*\\.my\\.go$"
# - lib/bad.go

skip-dirs:
- internal/mock

# output configuration options
output:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ audit:
.PHONY: test
test:
@echo 'Running unit tests'
go test -coverpkg=./internal/... -race -vet=off -count=1 -coverprofile unit.txt -covermode atomic ./...
go test -coverpkg=$(go list ./internal/...|grep -v mock) -race -vet=off -count=1 -coverprofile unit.txt -covermode atomic ./...

## unit-test-report: display unit coverage report in html format
.PHONY: unit-test-report
Expand Down
15 changes: 15 additions & 0 deletions internal/mock/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mock

type MockLogger struct{}

func (l *MockLogger) Debug(format string, args ...any) {
}

func (l *MockLogger) Info(format string, args ...any) {
}

func (l *MockLogger) Error(format string, args ...any) {
}

func (l *MockLogger) Close() {
}
65 changes: 65 additions & 0 deletions internal/mock/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package mock

import (
"errors"

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

// =============================================== Storage

func NewMockStorage(shouldFail bool) *mockStorage {
hosts := []model.Host{
model.NewHost(0, "Mock Host", "", "localhost", "root", "id_rsa", "2222"),
model.NewHost(0, "Mock Host", "", "localhost", "root", "id_rsa", "2222"),
model.NewHost(0, "Mock Host", "", "localhost", "root", "id_rsa", "2222"),
}

return &mockStorage{
shouldFail: shouldFail,
Hosts: hosts,
}
}

type mockStorage struct {
shouldFail bool
Hosts []model.Host
}

// Delete implements storage.HostStorage.
func (ms *mockStorage) Delete(id int) error {
if ms.shouldFail {
return errors.New("mock error")
}

ms.Hosts = append(ms.Hosts[:id], ms.Hosts[id+1:]...)

return nil
}

// Get implements storage.HostStorage.
func (ms *mockStorage) Get(hostID int) (model.Host, error) {
if ms.shouldFail {
return model.Host{}, errors.New("mock error")
}

return model.Host{}, nil
}

// GetAll implements storage.HostStorage.
func (ms *mockStorage) GetAll() ([]model.Host, error) {
if ms.shouldFail {
return ms.Hosts, errors.New("mock error")
}

return ms.Hosts, nil
}

// Save implements storage.HostStorage.
func (ms *mockStorage) Save(m model.Host) (model.Host, error) {
if ms.shouldFail {
return m, errors.New("mock error")
}

return m, nil
}

0 comments on commit 70b616b

Please sign in to comment.