mock

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

README

VCS Mock for Testing

This directory contains a mock implementation of the VersionControlSystem interface using gomock.

Overview

The mock VCS allows you to test code that depends on version control operations without requiring an actual VCS repository. This is particularly useful for:

  • Unit testing components that interact with VCS
  • Testing error scenarios that are difficult to reproduce with real VCS
  • Testing in CI/CD environments where VCS setup might be complex
  • Isolating tests from external dependencies

Files

  • mock_vcs.go - Generated mock implementation of the VersionControlSystem interface
  • README.md - This documentation file

Usage

Basic Setup
import (
    "testing"
    "github.com/golang/mock/gomock"
    "versionator/internal/vcs/mock"
)

func TestYourFunction(t *testing.T) {
    // Create a new gomock controller
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

    // Create a mock VCS instance
    mockVCS := mocks.NewMockVersionControlSystem(ctrl)

    // Set up expectations
    mockVCS.EXPECT().Name().Return("mock-git").AnyTimes()
    mockVCS.EXPECT().IsRepository().Return(true).Times(1)

    // Use the mock in your test
    // ... your test code here
}
Common Test Scenarios
Testing Repository Detection
// Mock a VCS that detects it's in a repository
mockVCS.EXPECT().IsRepository().Return(true)

// Mock a VCS that's not in a repository
mockVCS.EXPECT().IsRepository().Return(false)
Testing Repository Root Detection
// Mock successful repository root detection
mockVCS.EXPECT().GetRepositoryRoot().Return("/path/to/repo", nil)

// Mock error when not in a repository
mockVCS.EXPECT().GetRepositoryRoot().Return("", errors.New("not in a repository"))
Testing Working Directory Status
// Mock clean working directory
mockVCS.EXPECT().IsWorkingDirectoryClean().Return(true, nil)

// Mock dirty working directory
mockVCS.EXPECT().IsWorkingDirectoryClean().Return(false, nil)

// Mock error checking working directory
mockVCS.EXPECT().IsWorkingDirectoryClean().Return(false, errors.New("failed to check status"))
Testing VCS Identifier (Hash) Generation
// Mock successful hash generation
mockVCS.EXPECT().GetVCSIdentifier(7).Return("abc1234", nil)

// Mock error in hash generation
mockVCS.EXPECT().GetVCSIdentifier(7).Return("", errors.New("failed to get hash"))
Testing Tag Operations
// Mock successful tag creation
mockVCS.EXPECT().CreateTag("v1.0.0", "Release version 1.0.0").Return(nil)

// Mock tag creation error
mockVCS.EXPECT().CreateTag("v1.0.0", "Release").Return(errors.New("tag already exists"))

// Mock tag existence check
mockVCS.EXPECT().TagExists("v1.0.0").Return(true, nil)
mockVCS.EXPECT().TagExists("v2.0.0").Return(false, nil)
Testing with VCS Registry

You can also test the VCS registry functionality by temporarily replacing the global registry:

func TestWithRegistry(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

    // Save original registry and restore after test
    originalRegistry := vcs.GetRegistry() // Note: This would need to be exposed
    defer func() {
        // Restore original registry
    }()

    // Create mock VCS
    mockVCS := mocks.NewMockVersionControlSystem(ctrl)
    mockVCS.EXPECT().Name().Return("mock").AnyTimes()
    mockVCS.EXPECT().IsRepository().Return(true).AnyTimes()

    // Register mock VCS
    vcs.RegisterVCS(mockVCS)

    // Test code that uses vcs.GetActiveVCS() or vcs.GetVCS("mock")
    activeVCS := vcs.GetActiveVCS()
    // ... assertions
}
Advanced Expectations
Using Times() for Call Count Verification
// Expect method to be called exactly once
mockVCS.EXPECT().Name().Return("mock").Times(1)

// Expect method to be called at least once
mockVCS.EXPECT().IsRepository().Return(true).MinTimes(1)

// Expect method to be called any number of times
mockVCS.EXPECT().GetRepositoryRoot().Return("/repo", nil).AnyTimes()
Using InOrder() for Sequential Calls
gomock.InOrder(
    mockVCS.EXPECT().IsRepository().Return(true),
    mockVCS.EXPECT().GetRepositoryRoot().Return("/repo", nil),
    mockVCS.EXPECT().IsWorkingDirectoryClean().Return(true, nil),
)

Running Tests

To run the VCS tests including the mock tests:

go test ./internal/vcs -v

To run all tests in the project:

go test ./... -v

Regenerating the Mock

If the VersionControlSystem interface changes, you can regenerate the mock using:

mockgen -source=internal/vcs/vcs.go -destination=internal/vcs/mock/mock_vcs.go -package=mock

Or manually update the mock file to match the interface changes.

Best Practices

  1. Always use ctrl.Finish() - This ensures all expected calls were made
  2. Set realistic expectations - Mock behavior should match real VCS behavior
  3. Test both success and error scenarios - Use mocks to test error handling
  4. Use AnyTimes() sparingly - Prefer specific call counts when possible
  5. Clean up after tests - Restore original state when modifying global registries

Examples

See vcs_test.go for comprehensive examples of how to use the mock VCS in various testing scenarios.

Documentation

Overview

Package mock is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockVersionControlSystem

type MockVersionControlSystem struct {
	// contains filtered or unexported fields
}

MockVersionControlSystem is a mock of VersionControlSystem interface.

func NewMockVersionControlSystem

func NewMockVersionControlSystem(ctrl *gomock.Controller) *MockVersionControlSystem

NewMockVersionControlSystem creates a new mock instance.

func (*MockVersionControlSystem) AmendCommit added in v0.1.0

func (m *MockVersionControlSystem) AmendCommit(files []string) error

AmendCommit mocks base method.

func (*MockVersionControlSystem) BranchExists added in v0.0.12

func (m *MockVersionControlSystem) BranchExists(branchName string) (bool, error)

BranchExists mocks base method.

func (*MockVersionControlSystem) CommitFiles added in v0.1.0

func (m *MockVersionControlSystem) CommitFiles(files []string, message string) error

CommitFiles mocks base method.

func (*MockVersionControlSystem) CreateBranch added in v0.0.12

func (m *MockVersionControlSystem) CreateBranch(branchName string) error

CreateBranch mocks base method.

func (*MockVersionControlSystem) CreateTag

func (m *MockVersionControlSystem) CreateTag(tagName, message string) error

CreateTag mocks base method.

func (*MockVersionControlSystem) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockVersionControlSystem) GetBranchCommit added in v0.2.1

func (m *MockVersionControlSystem) GetBranchCommit(branchName string) (string, error)

GetBranchCommit mocks base method.

func (*MockVersionControlSystem) GetBranchName

func (m *MockVersionControlSystem) GetBranchName() (string, error)

GetBranchName mocks base method.

func (*MockVersionControlSystem) GetCommitAuthor

func (m *MockVersionControlSystem) GetCommitAuthor() (string, error)

GetCommitAuthor mocks base method.

func (*MockVersionControlSystem) GetCommitAuthorEmail

func (m *MockVersionControlSystem) GetCommitAuthorEmail() (string, error)

GetCommitAuthorEmail mocks base method.

func (*MockVersionControlSystem) GetCommitDate

func (m *MockVersionControlSystem) GetCommitDate() (time.Time, error)

GetCommitDate mocks base method.

func (*MockVersionControlSystem) GetCommitMessagesSinceTag added in v0.1.0

func (m *MockVersionControlSystem) GetCommitMessagesSinceTag() ([]string, error)

GetCommitMessagesSinceTag mocks base method.

func (*MockVersionControlSystem) GetCommitsSinceTag

func (m *MockVersionControlSystem) GetCommitsSinceTag() (int, error)

GetCommitsSinceTag mocks base method.

func (*MockVersionControlSystem) GetDirtyFiles added in v0.1.0

func (m *MockVersionControlSystem) GetDirtyFiles() ([]string, error)

GetDirtyFiles mocks base method.

func (*MockVersionControlSystem) GetHooksPath added in v0.1.0

func (m *MockVersionControlSystem) GetHooksPath() (string, error)

GetHooksPath mocks base method.

func (*MockVersionControlSystem) GetLastTag

func (m *MockVersionControlSystem) GetLastTag() (string, error)

GetLastTag mocks base method.

func (*MockVersionControlSystem) GetLastTagCommit

func (m *MockVersionControlSystem) GetLastTagCommit() (string, error)

GetLastTagCommit mocks base method.

func (*MockVersionControlSystem) GetRepositoryRoot

func (m *MockVersionControlSystem) GetRepositoryRoot() (string, error)

GetRepositoryRoot mocks base method.

func (*MockVersionControlSystem) GetTagCommit added in v0.2.1

func (m *MockVersionControlSystem) GetTagCommit(tagName string) (string, error)

GetTagCommit mocks base method.

func (*MockVersionControlSystem) GetUncommittedChanges

func (m *MockVersionControlSystem) GetUncommittedChanges() (int, error)

GetUncommittedChanges mocks base method.

func (*MockVersionControlSystem) GetVCSIdentifier

func (m *MockVersionControlSystem) GetVCSIdentifier(length int) (string, error)

GetVCSIdentifier mocks base method.

func (*MockVersionControlSystem) IsRepository

func (m *MockVersionControlSystem) IsRepository() bool

IsRepository mocks base method.

func (*MockVersionControlSystem) IsWorkingDirectoryClean

func (m *MockVersionControlSystem) IsWorkingDirectoryClean() (bool, error)

IsWorkingDirectoryClean mocks base method.

func (*MockVersionControlSystem) Name

func (m *MockVersionControlSystem) Name() string

Name mocks base method.

func (*MockVersionControlSystem) PushBranch added in v0.1.0

func (m *MockVersionControlSystem) PushBranch(branchName string) error

PushBranch mocks base method.

func (*MockVersionControlSystem) PushTag added in v0.1.0

func (m *MockVersionControlSystem) PushTag(tagName string) error

PushTag mocks base method.

func (*MockVersionControlSystem) TagExists

func (m *MockVersionControlSystem) TagExists(tagName string) (bool, error)

TagExists mocks base method.

type MockVersionControlSystemMockRecorder

type MockVersionControlSystemMockRecorder struct {
	// contains filtered or unexported fields
}

MockVersionControlSystemMockRecorder is the mock recorder for MockVersionControlSystem.

func (*MockVersionControlSystemMockRecorder) AmendCommit added in v0.1.0

func (mr *MockVersionControlSystemMockRecorder) AmendCommit(files interface{}) *gomock.Call

AmendCommit indicates an expected call of AmendCommit.

func (*MockVersionControlSystemMockRecorder) BranchExists added in v0.0.12

func (mr *MockVersionControlSystemMockRecorder) BranchExists(branchName interface{}) *gomock.Call

BranchExists indicates an expected call of BranchExists.

func (*MockVersionControlSystemMockRecorder) CommitFiles added in v0.1.0

func (mr *MockVersionControlSystemMockRecorder) CommitFiles(files, message interface{}) *gomock.Call

CommitFiles indicates an expected call of CommitFiles.

func (*MockVersionControlSystemMockRecorder) CreateBranch added in v0.0.12

func (mr *MockVersionControlSystemMockRecorder) CreateBranch(branchName interface{}) *gomock.Call

CreateBranch indicates an expected call of CreateBranch.

func (*MockVersionControlSystemMockRecorder) CreateTag

func (mr *MockVersionControlSystemMockRecorder) CreateTag(tagName, message interface{}) *gomock.Call

CreateTag indicates an expected call of CreateTag.

func (*MockVersionControlSystemMockRecorder) GetBranchCommit added in v0.2.1

func (mr *MockVersionControlSystemMockRecorder) GetBranchCommit(branchName interface{}) *gomock.Call

GetBranchCommit indicates an expected call of GetBranchCommit.

func (*MockVersionControlSystemMockRecorder) GetBranchName

func (mr *MockVersionControlSystemMockRecorder) GetBranchName() *gomock.Call

GetBranchName indicates an expected call of GetBranchName.

func (*MockVersionControlSystemMockRecorder) GetCommitAuthor

func (mr *MockVersionControlSystemMockRecorder) GetCommitAuthor() *gomock.Call

GetCommitAuthor indicates an expected call of GetCommitAuthor.

func (*MockVersionControlSystemMockRecorder) GetCommitAuthorEmail

func (mr *MockVersionControlSystemMockRecorder) GetCommitAuthorEmail() *gomock.Call

GetCommitAuthorEmail indicates an expected call of GetCommitAuthorEmail.

func (*MockVersionControlSystemMockRecorder) GetCommitDate

func (mr *MockVersionControlSystemMockRecorder) GetCommitDate() *gomock.Call

GetCommitDate indicates an expected call of GetCommitDate.

func (*MockVersionControlSystemMockRecorder) GetCommitMessagesSinceTag added in v0.1.0

func (mr *MockVersionControlSystemMockRecorder) GetCommitMessagesSinceTag() *gomock.Call

GetCommitMessagesSinceTag indicates an expected call of GetCommitMessagesSinceTag.

func (*MockVersionControlSystemMockRecorder) GetCommitsSinceTag

func (mr *MockVersionControlSystemMockRecorder) GetCommitsSinceTag() *gomock.Call

GetCommitsSinceTag indicates an expected call of GetCommitsSinceTag.

func (*MockVersionControlSystemMockRecorder) GetDirtyFiles added in v0.1.0

func (mr *MockVersionControlSystemMockRecorder) GetDirtyFiles() *gomock.Call

GetDirtyFiles indicates an expected call of GetDirtyFiles.

func (*MockVersionControlSystemMockRecorder) GetHooksPath added in v0.1.0

func (mr *MockVersionControlSystemMockRecorder) GetHooksPath() *gomock.Call

GetHooksPath indicates an expected call of GetHooksPath.

func (*MockVersionControlSystemMockRecorder) GetLastTag

GetLastTag indicates an expected call of GetLastTag.

func (*MockVersionControlSystemMockRecorder) GetLastTagCommit

func (mr *MockVersionControlSystemMockRecorder) GetLastTagCommit() *gomock.Call

GetLastTagCommit indicates an expected call of GetLastTagCommit.

func (*MockVersionControlSystemMockRecorder) GetRepositoryRoot

func (mr *MockVersionControlSystemMockRecorder) GetRepositoryRoot() *gomock.Call

GetRepositoryRoot indicates an expected call of GetRepositoryRoot.

func (*MockVersionControlSystemMockRecorder) GetTagCommit added in v0.2.1

func (mr *MockVersionControlSystemMockRecorder) GetTagCommit(tagName interface{}) *gomock.Call

GetTagCommit indicates an expected call of GetTagCommit.

func (*MockVersionControlSystemMockRecorder) GetUncommittedChanges

func (mr *MockVersionControlSystemMockRecorder) GetUncommittedChanges() *gomock.Call

GetUncommittedChanges indicates an expected call of GetUncommittedChanges.

func (*MockVersionControlSystemMockRecorder) GetVCSIdentifier

func (mr *MockVersionControlSystemMockRecorder) GetVCSIdentifier(length interface{}) *gomock.Call

GetVCSIdentifier indicates an expected call of GetVCSIdentifier.

func (*MockVersionControlSystemMockRecorder) IsRepository

func (mr *MockVersionControlSystemMockRecorder) IsRepository() *gomock.Call

IsRepository indicates an expected call of IsRepository.

func (*MockVersionControlSystemMockRecorder) IsWorkingDirectoryClean

func (mr *MockVersionControlSystemMockRecorder) IsWorkingDirectoryClean() *gomock.Call

IsWorkingDirectoryClean indicates an expected call of IsWorkingDirectoryClean.

func (*MockVersionControlSystemMockRecorder) Name

Name indicates an expected call of Name.

func (*MockVersionControlSystemMockRecorder) PushBranch added in v0.1.0

func (mr *MockVersionControlSystemMockRecorder) PushBranch(branchName interface{}) *gomock.Call

PushBranch indicates an expected call of PushBranch.

func (*MockVersionControlSystemMockRecorder) PushTag added in v0.1.0

func (mr *MockVersionControlSystemMockRecorder) PushTag(tagName interface{}) *gomock.Call

PushTag indicates an expected call of PushTag.

func (*MockVersionControlSystemMockRecorder) TagExists

func (mr *MockVersionControlSystemMockRecorder) TagExists(tagName interface{}) *gomock.Call

TagExists indicates an expected call of TagExists.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL