go-mocks

module
v0.2.17 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: MIT

README

go-mocks (Ha Ha!)

Mock implementations for Go standard library interfaces and those from github.com/pdutton/go-interfaces using go.uber.org/mock.

Installation

go get github.com/pdutton/go-mocks

Usage

Import the mock package you need in your tests:

import (
    "testing"
    mock_io "github.com/pdutton/go-mocks/io/mock_io"
    "go.uber.org/mock/gomock"
)

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

    mockReader := mock_io.NewMockReader(ctrl)
    mockReader.EXPECT().Read(gomock.Any()).Return(10, nil)

    // Use mockReader in your test
}

Available Mocks

This repository provides mocks for the following packages:

  • encoding/json - JSON, Decoder, Encoder
  • io - Reader, Writer, Closer, Seeker, and more
  • io/fs - FS, File, DirEntry, FileInfo, and more
  • net - Conn, Listener, Dialer, Resolver, and more
  • net/http/client - Client, Request, Response
  • net/http/server - Server
  • os - File, FileInfo, Process
  • os/exec - Cmd, Exec
  • os/signal - Signal
  • path - Path
  • path/filepath - FilePath, DirEntry, FileInfo
  • sync - Locker, Mutex, RWMutex, WaitGroup, and more

Generating Mocks

All mocks are auto-generated using mockgen. To regenerate:

# Install mockgen (if not already installed)
make mockgen

# Generate all mocks
make

# Generate specific package mock
make io/mock_io/all.go
make net/http/client/mock_client/all.go

Adding New Mocks

To add mocks for a new package:

  1. Add the target to the all: phony target in the Makefile
  2. Create a new phony target following the pattern:
    .PHONY: <path>/mock_<package>/all.go
    <path>/mock_<package>/all.go:
        $(MOCKGEN) -destination $@ -package mock_<package> github.com/pdutton/go-interfaces/<path> Interface1,Interface2,...
    
  3. Run make <path>/mock_<package>/all.go to generate

Dependencies

Important Notes

  • Never edit generated files: All files in mock_*/all.go are auto-generated. Changes should be made to the Makefile and regenerated.
  • Version alignment: When updating go-interfaces dependency, regenerate all mocks with make all

License

See LICENSE file for details.

Directories

Path Synopsis
encoding
json/mock_json
Package mock_json is a generated GoMock package.
Package mock_json is a generated GoMock package.
internal
testutil
Package testutil provides shared testing utilities for mock tests.
Package testutil provides shared testing utilities for mock tests.
io
fs/mock_fs
Package mock_fs is a generated GoMock package.
Package mock_fs is a generated GoMock package.
mock_io
Package mock_io is a generated GoMock package.
Package mock_io is a generated GoMock package.
net
http/client/mock_client
Package mock_http is a generated GoMock package.
Package mock_http is a generated GoMock package.
http/server/mock_server
Package mock_http is a generated GoMock package.
Package mock_http is a generated GoMock package.
mock_net
Package mock_net is a generated GoMock package.
Package mock_net is a generated GoMock package.
os
exec/mock_exec
Package mock_exec is a generated GoMock package.
Package mock_exec is a generated GoMock package.
mock_os
Package mock_os is a generated GoMock package.
Package mock_os is a generated GoMock package.
signal/mock_signal
Package mock_signal is a generated GoMock package.
Package mock_signal is a generated GoMock package.
path
filepath/mock_filepath
Package mock_filepath is a generated GoMock package.
Package mock_filepath is a generated GoMock package.
mock_path
Package mock_path is a generated GoMock package.
Package mock_path is a generated GoMock package.
sync
mock_sync
Package mock_sync is a generated GoMock package.
Package mock_sync is a generated GoMock package.

Jump to

Keyboard shortcuts

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