mock

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 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) CreateTag

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

CreateTag mock base method.

func (*MockVersionControlSystem) EXPECT

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

func (*MockVersionControlSystem) GetBranchName

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

GetBranchName mock base method.

func (*MockVersionControlSystem) GetCommitAuthor

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

GetCommitAuthor mock base method.

func (*MockVersionControlSystem) GetCommitAuthorEmail

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

GetCommitAuthorEmail mock base method.

func (*MockVersionControlSystem) GetCommitDate

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

GetCommitDate mock base method.

func (*MockVersionControlSystem) GetCommitsSinceTag

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

GetCommitsSinceTag mock base method.

func (*MockVersionControlSystem) GetLastTag

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

GetLastTag mock base method.

func (*MockVersionControlSystem) GetLastTagCommit

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

GetLastTagCommit mock base method.

func (*MockVersionControlSystem) GetRepositoryRoot

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

GetRepositoryRoot mock base method.

func (*MockVersionControlSystem) GetUncommittedChanges

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

GetUncommittedChanges mock base method.

func (*MockVersionControlSystem) GetVCSIdentifier

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

GetVCSIdentifier mock base method.

func (*MockVersionControlSystem) IsRepository

func (m *MockVersionControlSystem) IsRepository() bool

IsRepository mock base method.

func (*MockVersionControlSystem) IsWorkingDirectoryClean

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

IsWorkingDirectoryClean mock base method.

func (*MockVersionControlSystem) Name

func (m *MockVersionControlSystem) Name() string

Name mock base method.

func (*MockVersionControlSystem) TagExists

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

TagExists mock base method.

type MockVersionControlSystemMockRecorder

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

MockVersionControlSystemMockRecorder is the mock recorder for MockVersionControlSystem.

func (*MockVersionControlSystemMockRecorder) CreateTag

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

CreateTag indicates an expected call of CreateTag.

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) GetCommitsSinceTag

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

GetCommitsSinceTag indicates an expected call of GetCommitsSinceTag.

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) 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) 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