aferomock

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 6 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 = MockFileInfo()

NoMockFileInfo is no mock FileInfo.

View Source
var NoMockFs = MockFs()

NoMockFs is no mock Fs.

Functions

This section is empty.

Types

type FileInfo

type FileInfo struct {
	mock.Mock
}

FileInfo is a os.FileInfo.

func NewFileInfo

func NewFileInfo(mocks ...func(i *FileInfo)) *FileInfo

NewFileInfo mocks os.FileInfo interface.

func (*FileInfo) IsDir

func (f *FileInfo) IsDir() bool

IsDir satisfies os.FileInfo.

func (*FileInfo) ModTime

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

ModTime satisfies os.FileInfo.

func (*FileInfo) Mode

func (f *FileInfo) Mode() os.FileMode

Mode satisfies os.FileInfo.

func (*FileInfo) Name

func (f *FileInfo) Name() string

Name satisfies os.FileInfo.

func (*FileInfo) Size

func (f *FileInfo) Size() int64

Size satisfies os.FileInfo.

func (*FileInfo) Sys

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

Sys satisfies os.FileInfo.

type FileInfoMocker

type FileInfoMocker func(tb testing.TB) *FileInfo

FileInfoMocker is FileInfo mocker.

func MockFileInfo

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

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

type Fs

type Fs struct {
	mock.Mock
}

Fs is a afero.Fs.

func NewFs

func NewFs(mocks ...func(fs *Fs)) *Fs

NewFs mocks afero.Fs interface.

func (*Fs) Chmod

func (f *Fs) Chmod(name string, mode os.FileMode) error

Chmod satisfies afero.Fs.

func (*Fs) Chown

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

Chown satisfies afero.Fs.

func (*Fs) Chtimes

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

Chtimes satisfies afero.Fs.

func (*Fs) Create

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

Create satisfies afero.Fs.

func (*Fs) Mkdir

func (f *Fs) Mkdir(name string, perm os.FileMode) error

Mkdir satisfies afero.Fs.

func (*Fs) MkdirAll

func (f *Fs) MkdirAll(path string, perm os.FileMode) error

MkdirAll satisfies afero.Fs.

func (*Fs) Name

func (f *Fs) Name() string

Name satisfies afero.Fs.

func (*Fs) Open

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

Open satisfies afero.Fs.

func (*Fs) OpenFile

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

OpenFile satisfies afero.Fs.

func (*Fs) Remove

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

Remove satisfies afero.Fs.

func (*Fs) RemoveAll

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

RemoveAll satisfies afero.Fs.

func (*Fs) Rename

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

Rename satisfies afero.Fs.

func (*Fs) Stat

func (f *Fs) Stat(name string) (os.FileInfo, error)

Stat satisfies afero.Fs.

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.

Jump to

Keyboard shortcuts

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