aferomock

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 5 Imported by: 0

README

Mock for spf13/afero

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

aferomock is a mock library for spf13/afero

Prerequisites

  • Go >= 1.17

Install

go get go.nhat.io/aferomock

Examples

package mypackage_test

import (
	"errors"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"go.nhat.io/aferomock"
)

func TestMyPackage(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		scenario      string
		mockFs        aferomock.FsMocker
		expectedError string
	}{
		{
			scenario: "no error",
			mockFs: aferomock.MockFs(func(fs *aferomock.Fs) {
				fs.MkdirAll("highway/to/hell", os.ModePerm).Return(nil)
			}),
		},
		{
			scenario: "error",
			mockFs: aferomock.MockFs(func(fs *aferomock.Fs) {
				fs.MkdirAll("highway/to/hell", os.ModePerm).Return(errors.New("mkdir error"))
			}),
			expectedError: "mkdir error",
		},
	}

	for _, tc := range testCases {
		tc := tc
		t.Run(tc.scenario, func(t *testing.T) {
			t.Parallel()

			err := tc.mockFs(t).MkdirAll("highway/to/hell")

			if tc.expectedError == "" {
				assert.NoError(t, err)
			} else {
				assert.EqualError(t, err, tc.expectedError)
			}
		})
	}
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package aferomock provides mock for spf13/afero.

Index

Constants

This section is empty.

Variables

View Source
var NoMockFileInfo = NopFileInfo

NoMockFileInfo is no mock FileInfo. Deprecated: use NopFileInfo instead.

View Source
var NoMockFs = NopFs

NoMockFs is no mock Fs. Deprecated: use NopFs instead.

View Source
var NopFile = MockFile()

NopFile is no mock File.

View Source
var NopFileInfo = MockFileInfo()

NopFileInfo is no mock FileInfo.

View Source
var NopFs = MockFs()

NopFs is no mock Fs.

Functions

This section is empty.

Types

type File added in v0.5.0

type File struct {
	mock.Mock
}

File is an autogenerated mock type for the File type

func NewFile added in v0.5.0

func NewFile(t interface {
	mock.TestingT
	Cleanup(func())
}) *File

NewFile creates a new instance of File. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*File) Close added in v0.5.0

func (_m *File) Close() error

Close provides a mock function with given fields:

func (*File) Name added in v0.5.0

func (_m *File) Name() string

Name provides a mock function with given fields:

func (*File) Read added in v0.5.0

func (_m *File) Read(p []byte) (int, error)

Read provides a mock function with given fields: p

func (*File) ReadAt added in v0.5.0

func (_m *File) ReadAt(p []byte, off int64) (int, error)

ReadAt provides a mock function with given fields: p, off

func (*File) Readdir added in v0.5.0

func (_m *File) Readdir(count int) ([]fs.FileInfo, error)

Readdir provides a mock function with given fields: count

func (*File) Readdirnames added in v0.5.0

func (_m *File) Readdirnames(n int) ([]string, error)

Readdirnames provides a mock function with given fields: n

func (*File) Seek added in v0.5.0

func (_m *File) Seek(offset int64, whence int) (int64, error)

Seek provides a mock function with given fields: offset, whence

func (*File) Stat added in v0.5.0

func (_m *File) Stat() (fs.FileInfo, error)

Stat provides a mock function with given fields:

func (*File) Sync added in v0.5.0

func (_m *File) Sync() error

Sync provides a mock function with given fields:

func (*File) Truncate added in v0.5.0

func (_m *File) Truncate(size int64) error

Truncate provides a mock function with given fields: size

func (*File) Write added in v0.5.0

func (_m *File) Write(p []byte) (int, error)

Write provides a mock function with given fields: p

func (*File) WriteAt added in v0.5.0

func (_m *File) WriteAt(p []byte, off int64) (int, error)

WriteAt provides a mock function with given fields: p, off

func (*File) WriteString added in v0.5.0

func (_m *File) WriteString(s string) (int, error)

WriteString provides a mock function with given fields: s

type FileCallbacks added in v0.6.0

type FileCallbacks struct {
	CloseFunc        func() error
	NameFunc         func() string
	ReadFunc         func(p []byte) (int, error)
	ReadAtFunc       func(p []byte, off int64) (int, error)
	ReaddirFunc      func(count int) ([]fs.FileInfo, error)
	ReaddirnamesFunc func(n int) ([]string, error)
	SeekFunc         func(offset int64, whence int) (int64, error)
	StatFunc         func() (fs.FileInfo, error)
	SyncFunc         func() error
	TruncateFunc     func(size int64) error
	WriteFunc        func(p []byte) (int, error)
	WriteAtFunc      func(p []byte, off int64) (int, error)
	WriteStringFunc  func(s string) (int, error)
}

FileCallbacks is a callback-based mock for afero.File.

func OverrideFile added in v0.6.0

func OverrideFile(file afero.File, c FileCallbacks) FileCallbacks

OverrideFile overrides the afero.File methods with the provided callbacks.

func (FileCallbacks) Close added in v0.6.0

func (f FileCallbacks) Close() error

Close satisfies the afero.File interface.

func (FileCallbacks) Name added in v0.6.0

func (f FileCallbacks) Name() string

Name satisfies the afero.File interface.

func (FileCallbacks) Read added in v0.6.0

func (f FileCallbacks) Read(p []byte) (int, error)

Read satisfies the afero.File interface.

func (FileCallbacks) ReadAt added in v0.6.0

func (f FileCallbacks) ReadAt(p []byte, off int64) (int, error)

ReadAt satisfies the afero.File interface.

func (FileCallbacks) Readdir added in v0.6.0

func (f FileCallbacks) Readdir(count int) ([]fs.FileInfo, error)

Readdir satisfies the afero.File interface.

func (FileCallbacks) Readdirnames added in v0.6.0

func (f FileCallbacks) Readdirnames(n int) ([]string, error)

Readdirnames satisfies the afero.File interface.

func (FileCallbacks) Seek added in v0.6.0

func (f FileCallbacks) Seek(offset int64, whence int) (int64, error)

Seek satisfies the afero.File interface.

func (FileCallbacks) Stat added in v0.6.0

func (f FileCallbacks) Stat() (fs.FileInfo, error)

Stat satisfies the afero.File interface.

func (FileCallbacks) Sync added in v0.6.0

func (f FileCallbacks) Sync() error

Sync satisfies the afero.File interface.

func (FileCallbacks) Truncate added in v0.6.0

func (f FileCallbacks) Truncate(size int64) error

Truncate satisfies the afero.File interface.

func (FileCallbacks) Write added in v0.6.0

func (f FileCallbacks) Write(p []byte) (int, error)

Write satisfies the afero.File interface.

func (FileCallbacks) WriteAt added in v0.6.0

func (f FileCallbacks) WriteAt(p []byte, off int64) (int, error)

WriteAt satisfies the afero.File interface.

func (FileCallbacks) WriteString added in v0.6.0

func (f FileCallbacks) WriteString(s string) (int, error)

WriteString satisfies the afero.File interface.

type FileInfo

type FileInfo struct {
	mock.Mock
}

FileInfo is an autogenerated mock type for the FileInfo type

func NewFileInfo

func NewFileInfo(t interface {
	mock.TestingT
	Cleanup(func())
}) *FileInfo

NewFileInfo creates a new instance of FileInfo. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*FileInfo) IsDir

func (_m *FileInfo) IsDir() bool

IsDir provides a mock function with given fields:

func (*FileInfo) ModTime

func (_m *FileInfo) ModTime() time.Time

ModTime provides a mock function with given fields:

func (*FileInfo) Mode

func (_m *FileInfo) Mode() fs.FileMode

Mode provides a mock function with given fields:

func (*FileInfo) Name

func (_m *FileInfo) Name() string

Name provides a mock function with given fields:

func (*FileInfo) Size

func (_m *FileInfo) Size() int64

Size provides a mock function with given fields:

func (*FileInfo) Sys

func (_m *FileInfo) Sys() interface{}

Sys provides a mock function with given fields:

type FileInfoCallbacks added in v0.7.0

type FileInfoCallbacks struct {
	NameFunc    func() string
	SizeFunc    func() int64
	ModeFunc    func() fs.FileMode
	ModTimeFunc func() time.Time
	IsDirFunc   func() bool
	SysFunc     func() interface{}
}

FileInfoCallbacks is a callback-based mock for fs.FileInfo.

func OverrideFileInfo added in v0.7.0

func OverrideFileInfo(fi fs.FileInfo, c FileInfoCallbacks) FileInfoCallbacks

OverrideFileInfo overrides the fs.FileInfo methods with the provided callbacks.

func (FileInfoCallbacks) IsDir added in v0.7.0

func (f FileInfoCallbacks) IsDir() bool

IsDir satisfies the fs.FileInfo interface.

func (FileInfoCallbacks) ModTime added in v0.7.0

func (f FileInfoCallbacks) ModTime() time.Time

ModTime satisfies the fs.FileInfo interface.

func (FileInfoCallbacks) Mode added in v0.7.0

func (f FileInfoCallbacks) Mode() fs.FileMode

Mode satisfies the fs.FileInfo interface.

func (FileInfoCallbacks) Name added in v0.7.0

func (f FileInfoCallbacks) Name() string

Name satisfies the fs.FileInfo interface.

func (FileInfoCallbacks) Size added in v0.7.0

func (f FileInfoCallbacks) Size() int64

Size satisfies the fs.FileInfo interface.

func (FileInfoCallbacks) Sys added in v0.7.0

func (f FileInfoCallbacks) Sys() interface{}

Sys satisfies the fs.FileInfo interface.

type FileInfoMocker

type FileInfoMocker func(tb testing.TB) *FileInfo

FileInfoMocker is FileInfo mocker.

func MockFileInfo

func MockFileInfo(mocks ...func(fi *FileInfo)) FileInfoMocker

MockFileInfo creates FileInfo mock with cleanup to ensure all the expectations are met.

type FileMocker added in v0.5.0

type FileMocker func(tb testing.TB) *File

FileMocker is File mocker.

func MockFile added in v0.5.0

func MockFile(mocks ...func(f *File)) FileMocker

MockFile creates File mock with cleanup to ensure all the expectations are met.

type Fs

type Fs struct {
	mock.Mock
}

Fs is an autogenerated mock type for the Fs type

func NewFs

func NewFs(t interface {
	mock.TestingT
	Cleanup(func())
}) *Fs

NewFs creates a new instance of Fs. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*Fs) Chmod

func (_m *Fs) Chmod(name string, mode fs.FileMode) error

Chmod provides a mock function with given fields: name, mode

func (*Fs) Chown

func (_m *Fs) Chown(name string, uid int, gid int) error

Chown provides a mock function with given fields: name, uid, gid

func (*Fs) Chtimes

func (_m *Fs) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes provides a mock function with given fields: name, atime, mtime

func (*Fs) Create

func (_m *Fs) Create(name string) (afero.File, error)

Create provides a mock function with given fields: name

func (*Fs) Mkdir

func (_m *Fs) Mkdir(name string, perm fs.FileMode) error

Mkdir provides a mock function with given fields: name, perm

func (*Fs) MkdirAll

func (_m *Fs) MkdirAll(path string, perm fs.FileMode) error

MkdirAll provides a mock function with given fields: path, perm

func (*Fs) Name

func (_m *Fs) Name() string

Name provides a mock function with given fields:

func (*Fs) Open

func (_m *Fs) Open(name string) (afero.File, error)

Open provides a mock function with given fields: name

func (*Fs) OpenFile

func (_m *Fs) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error)

OpenFile provides a mock function with given fields: name, flag, perm

func (*Fs) Remove

func (_m *Fs) Remove(name string) error

Remove provides a mock function with given fields: name

func (*Fs) RemoveAll

func (_m *Fs) RemoveAll(path string) error

RemoveAll provides a mock function with given fields: path

func (*Fs) Rename

func (_m *Fs) Rename(oldname string, newname string) error

Rename provides a mock function with given fields: oldname, newname

func (*Fs) Stat

func (_m *Fs) Stat(name string) (fs.FileInfo, error)

Stat provides a mock function with given fields: name

type FsCallbacks added in v0.6.0

type FsCallbacks struct {
	ChmodFunc     func(name string, mode fs.FileMode) error
	ChownFunc     func(name string, uid int, gid int) error
	ChtimesFunc   func(name string, atime time.Time, mtime time.Time) error
	CreateFunc    func(name string) (afero.File, error)
	MkdirFunc     func(name string, perm fs.FileMode) error
	MkdirAllFunc  func(path string, perm fs.FileMode) error
	NameFunc      func() string
	OpenFunc      func(name string) (afero.File, error)
	OpenFileFunc  func(name string, flag int, perm fs.FileMode) (afero.File, error)
	RemoveFunc    func(name string) error
	RemoveAllFunc func(path string) error
	RenameFunc    func(oldname string, newname string) error
	StatFunc      func(name string) (fs.FileInfo, error)
}

FsCallbacks is a callback-based mock for afero.Fs.

func OverrideFs added in v0.6.0

func OverrideFs(fs afero.Fs, c FsCallbacks) FsCallbacks

OverrideFs overrides a afero.Fs with custom callbacks.

func WrapFs added in v0.5.0

func WrapFs(fs afero.Fs, callbacks WrappedFs) FsCallbacks

WrapFs wraps a afero.Fs with custom callbacks. Deprecated: Use OverrideFs instead.

func (FsCallbacks) Chmod added in v0.6.0

func (fs FsCallbacks) Chmod(name string, mode fs.FileMode) error

Chmod satisfies the afero.Fs interface.

func (FsCallbacks) Chown added in v0.6.0

func (fs FsCallbacks) Chown(name string, uid int, gid int) error

Chown satisfies the afero.Fs interface.

func (FsCallbacks) Chtimes added in v0.6.0

func (fs FsCallbacks) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes satisfies the afero.Fs interface.

func (FsCallbacks) Create added in v0.6.0

func (fs FsCallbacks) Create(name string) (afero.File, error)

Create satisfies the afero.Fs interface.

func (FsCallbacks) Mkdir added in v0.6.0

func (fs FsCallbacks) Mkdir(name string, perm fs.FileMode) error

Mkdir satisfies the afero.Fs interface.

func (FsCallbacks) MkdirAll added in v0.6.0

func (fs FsCallbacks) MkdirAll(path string, perm fs.FileMode) error

MkdirAll satisfies the afero.Fs interface.

func (FsCallbacks) Name added in v0.6.0

func (fs FsCallbacks) Name() string

Name satisfies the afero.Fs interface.

func (FsCallbacks) Open added in v0.6.0

func (fs FsCallbacks) Open(name string) (afero.File, error)

Open satisfies the afero.Fs interface.

func (FsCallbacks) OpenFile added in v0.6.0

func (fs FsCallbacks) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error)

OpenFile satisfies the afero.Fs interface.

func (FsCallbacks) Remove added in v0.6.0

func (fs FsCallbacks) Remove(name string) error

Remove satisfies the afero.Fs interface.

func (FsCallbacks) RemoveAll added in v0.6.0

func (fs FsCallbacks) RemoveAll(path string) error

RemoveAll satisfies the afero.Fs interface.

func (FsCallbacks) Rename added in v0.6.0

func (fs FsCallbacks) Rename(oldname string, newname string) error

Rename satisfies the afero.Fs interface.

func (FsCallbacks) Stat added in v0.6.0

func (fs FsCallbacks) Stat(name string) (fs.FileInfo, error)

Stat satisfies the afero.Fs interface.

type FsMocker

type FsMocker func(tb testing.TB) *Fs

FsMocker is Fs mocker.

func MockFs

func MockFs(mocks ...func(fs *Fs)) FsMocker

MockFs creates Fs mock with cleanup to ensure all the expectations are met.

type WrappedFs added in v0.5.0

type WrappedFs = FsCallbacks

WrappedFs is a type alias for FsCallbacks. Deprecated: Use FsCallbacks instead.

Jump to

Keyboard shortcuts

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