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

fix/missing unit tests #32

Merged
merged 16 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 12 additions & 2 deletions internal/commands/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ import (
"fmt"
)

var CommandErrorTemplate = "command %s exited with %s, output: \n%s"

type CommandError struct {
Arguments []string
Output string
Err error
Output string
}

func NewCommandError(arguments []string, output string, err error) CommandError {
return CommandError{
Arguments: arguments,
Err: err,
Output: output,
}
}

func (e CommandError) Error() string {
return fmt.Sprintf("command %s exited with %s, output: \n%s", e.Arguments, e.Err.Error(), e.Output)
return fmt.Sprintf(CommandErrorTemplate, e.Arguments, e.Err.Error(), e.Output)
}
35 changes: 35 additions & 0 deletions internal/commands/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package commands

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCommandError_Error(t *testing.T) {
t.Run("ValidateMessage", func(t *testing.T) {
var args = []string{"some", "command", "arguments"}
var err = fmt.Errorf("some-error")
var output = "some-output"

var want = fmt.Sprintf(CommandErrorTemplate, args, err, output)

var cmdError = CommandError{Arguments: args, Err: err, Output: output}.Error()
assert.Equal(t, want, cmdError, `want: "%s", got: "%s"`, want, cmdError)
})
}

func TestNewCommandError(t *testing.T) {
t.Run("ValidateState", func(t *testing.T) {
var args = []string{"some", "command", "arguments"}
var err = fmt.Errorf("some-error")
var output = "some-output"

var cmdError = NewCommandError(args, output, err)

assert.Equal(t, args, cmdError.Arguments, `want: "%s", got: "%s"`, args, cmdError.Arguments)
assert.Equal(t, err, cmdError.Err, `want: "%s", got: "%s"`, err, cmdError.Err)
assert.Equal(t, output, cmdError.Output, `want: "%s", got: "%s"`, output, cmdError.Output)
})
}
2 changes: 1 addition & 1 deletion internal/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c ExecCommander) Output(name string, arg ...string) (string, error) {
var output = buffer.String()

if err != nil {
return "", CommandError{Arguments: command.Args, Err: err, Output: output}
return "", NewCommandError(command.Args, output, err)
}

return output, err
Expand Down
87 changes: 87 additions & 0 deletions internal/fakes/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fakes

import "fmt"

// FakeGitAPI a git.API interface fake implementation.
type FakeGitAPI struct {
Config map[string]string
LocalTags []string
PushedTags []string
}

// NewFakeGitAPI creates a new FakeGitAPI.
// Returns the new FakeGitAPI.
func NewFakeGitAPI() *FakeGitAPI {
return &FakeGitAPI{
Config: map[string]string{},
LocalTags: []string{},
PushedTags: []string{},
}
}

// CreateAnnotatedTag creates a fake tag.
func (fake *FakeGitAPI) CreateAnnotatedTag(tag string) (err error) {
fake.LocalTags = append(fake.LocalTags, tag)
return err
}

// FetchTags does nothing.
func (fake *FakeGitAPI) FetchTags() (err error) {
return err
}

// FetchUnshallow does nothing.
func (fake *FakeGitAPI) FetchUnshallow() (err error) {
return err
}

// GetConfig returns a fake config.
func (fake *FakeGitAPI) GetConfig(key string) (value string, err error) {
var config, exists = fake.Config[key]

if exists {
return config, nil
}

return "", fmt.Errorf("config does not exist")
}

// GetLatestAnnotatedTag returns a fake tag.
func (fake *FakeGitAPI) GetLatestAnnotatedTag() (tag string, err error) {
if len(fake.LocalTags) == 0 {
return tag, fmt.Errorf("no tags found")
}
return fake.LocalTags[len(fake.LocalTags)-1], nil
}

// GetLatestCommitMessage does nothing.
func (fake *FakeGitAPI) GetLatestCommitMessage() (message string, err error) {
return message, err
}

// GetMergedBranchName does nothing.
func (fake *FakeGitAPI) GetMergedBranchName() (name string, err error) {
return name, err
}

// PushTag pushes a fake tag.
func (fake *FakeGitAPI) PushTag(tag string) (err error) {

fake.PushedTags = append(fake.PushedTags, tag)
return err
}

// SetConfig sets a fake config.
func (fake *FakeGitAPI) SetConfig(key string, value string) (err error) {
fake.Config[key] = value
return err
}

// SetConfigIfNotSet sets a fake config if it does not exist.
func (fake *FakeGitAPI) SetConfigIfNotSet(key string, value string) (err error) {
if _, err = fake.GetConfig(key); err != nil {
err = fake.SetConfig(key, value)
}

return err
}
22 changes: 1 addition & 21 deletions internal/mocks/mocks.go → internal/mocks/commander.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mocks

import (
"github.com/stretchr/testify/mock"
)
import "github.com/stretchr/testify/mock"

// MockCommander a commander interface mock implementation.
type MockCommander struct {
Expand All @@ -28,21 +26,3 @@ func (mock *MockCommander) Run(name string, arg ...string) error {
args := mock.Called(name, arg)
return args.Error(0)
}

// MockSemverMode a semver mode interface mock implementation.
type MockSemverMode struct {
mock.Mock
}

// NewMockSemverMode creates a new MockSemverMode.
// Returns the new MockSemverMode.
func NewMockSemverMode() *MockSemverMode {
return &MockSemverMode{}
}

// Increment mock increments a version.
// Returns an incremented mock version.
func (mock *MockSemverMode) Increment(targetVersion string) (nextVersion string, err error) {
args := mock.Called(targetVersion)
return args.String(0), args.Error(1)
}
86 changes: 86 additions & 0 deletions internal/mocks/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package mocks

import (
"github.com/stretchr/testify/mock"
)

// MockGitAPI a git.API interface mock implementation.
type MockGitAPI struct {
mock.Mock
}

// NewMockGitAPI creates a new MockGitAPI.
// Returns the new MockGitAPI.
func NewMockGitAPI() *MockGitAPI {
return &MockGitAPI{}
}

// CreateAnnotatedTag mocks creating a tag.
// Returns a mocked error.
func (mock *MockGitAPI) CreateAnnotatedTag(tag string) (err error) {
args := mock.Called(tag)
return args.Error(0)
}

// FetchTags mocks fetching tags.
// Returns a mocked error.
func (mock *MockGitAPI) FetchTags() (err error) {
args := mock.Called()
return args.Error(0)
}

// FetchUnshallow mocks changing to an unshallow repo.
// Returns a mocked error.
func (mock *MockGitAPI) FetchUnshallow() (err error) {
args := mock.Called()
return args.Error(0)
}

// GetConfig mocks getting a config.
// Returns a mocked config or a mocked error.
func (mock *MockGitAPI) GetConfig(key string) (value string, err error) {
args := mock.Called(key)
return args.String(0), args.Error(1)
}

// GetLatestAnnotatedTag mocks getting the latest annotated tag.
// Returns a mocked tag or a mocked error.
func (mock *MockGitAPI) GetLatestAnnotatedTag() (tag string, err error) {
args := mock.Called()
return args.String(0), args.Error(1)
}

// GetLatestCommitMessage mocks getting the latest commit message.
// Returns a mocked commit message or a mocked error.
func (mock *MockGitAPI) GetLatestCommitMessage() (message string, err error) {
args := mock.Called()
return args.String(0), args.Error(1)
}

// GetMergedBranchName mocks getting a merged branch name.
// Returns a mocked merged branch name or a mocked error.
func (mock *MockGitAPI) GetMergedBranchName() (name string, err error) {
args := mock.Called()
return args.String(0), args.Error(1)
}

// PushTag pushes a fake tag.
// Returns a mocked error.
func (mock *MockGitAPI) PushTag(tag string) (err error) {
args := mock.Called(tag)
return args.Error(0)
}

// SetConfig mocks setting a config.
// Returns a mocked error.
func (mock *MockGitAPI) SetConfig(key string, value string) (err error) {
args := mock.Called(key, value)
return args.Error(0)
}

// SetConfigIfNotSet mocks setting a config if not set.
// Returns a mocked error.
func (mock *MockGitAPI) SetConfigIfNotSet(key string, value string) (err error) {
args := mock.Called(key, value)
return args.Error(0)
}
21 changes: 21 additions & 0 deletions internal/mocks/mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mocks

import "github.com/stretchr/testify/mock"

// MockMode a semver mode interface mock implementation.
type MockMode struct {
mock.Mock
}

// NewMockMode creates a new MockMode.
// Returns the new MockMode.
func NewMockMode() *MockMode {
return &MockMode{}
}

// Increment mock increments a version.
// Returns an incremented mock version.
func (mock *MockMode) Increment(targetVersion string) (nextVersion string, err error) {
args := mock.Called(targetVersion)
return args.String(0), args.Error(1)
}
18 changes: 9 additions & 9 deletions internal/util/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package util

import "strings"

// Contains returns true if a string, split by delimiters, contains another string.
func Contains(target string, value string, delimiters string) bool {
var slice = SplitByDelimiterString(target, delimiters)
return SliceContainsString(slice, value)
}

// SplitByDelimiterString splits a string by multiple delimiters.
// Returns the resulting slice of strings.
func SplitByDelimiterString(target string, delimiters string) []string {
Expand All @@ -18,17 +24,11 @@ func SplitByDelimiterString(target string, delimiters string) []string {
}

// SliceContainsString returns true if a string equals an element in the slice.
func SliceContainsString(container []string, value string) bool {
for _, contained := range container {
if contained == value {
func SliceContainsString(slice []string, value string) bool {
for _, element := range slice {
if element == value {
return true
}
}
return false
}

// Contains returns true if a string, split by delimiters, contains another string.
func Contains(target string, value string, delimiters string) bool {
var slice = SplitByDelimiterString(target, delimiters)
return SliceContainsString(slice, value)
}
Loading