goshims

module
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: Apache-2.0

README

GO Shims

why?

Have you ever wanted to fake out go system libary calls? In most cases you create an interface and then provide a mock/fake implementation and a shim that calls the real calls. That's great if you only have to do it once. What happens when it becomes a pattern and these little utilities end up duplicated everywhere...that's a problem. This repo is the solution.

how was it made?

As of this commit on maxbrunsfeld/counterfeiter, counterfeiter now has the ability to auto-generate interfaces/shims for system libaries. That's cool! Instead of generating those on the fly all the time, we collected some popular ones here for your convience.

how to use it?

In your struct for your class add a varible referencing the interface:

package abroker
import (
	"code.cloudfoundry.org/goshims/ioutilshim"
	"code.cloudfoundry.org/goshims/osshim"
)

type broker struct {
	os          osshim.Os
	ioutil      ioutilshim.Ioutil
}

func New(
	os osshim.Os, ioutil ioutilshim.Ioutil,
) *broker {
	theBroker := broker{
		os:          os,
		ioutil:      ioutil,
	}
	return &theBroker
}

func (b *broker) Serialize(state interface{}) error {
	stateFile := "/tmp/abrokerstate.json"

	stateData, err := json.Marshal(state)
	if err != nil {
		return err
	}

	err = b.ioutil.WriteFile(stateFile, stateData, os.ModePerm)
	if err != nil {
		return err
	}
	return nil
}

In the factory method to construct that class, dependency inject the right version of the implemenation. For example, your test code would use the fakes:

package abroker_test
import(
	"github.com/something/abroker"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"code.cloudfoundry.org/goshims/osshim/os_fake"
	"code.cloudfoundry.org/goshims/ioutilshim/ioutil_fake"
)

var (
	fakeOs             *os_fake.FakeOs
	fakeIoutil         *ioutil_fake.FakeIoutil
... 
BeforeEach(func() {
	fakeOs = &os_fake.FakeOs{}
	fakeIoutil = &ioutil_fake.FakeIoutil{}
...

It("Should error if write state fails", func(){
	fakeIoutil.WriteFileReturns(errors.New("Error writing file."))
	broker = abroker.New(fakeOs, fakeIoutil)
	err := broker.Serialize(someData)
	Expect(err).To(HaveOccurred())
})

In your production code you would use the real implementation:

pacakge main
import(
	"github.com/something/abroker"
	"code.cloudfoundry.org/goshims/ioutilshim"
	"code.cloudfoundry.org/goshims/osshim"
)
...

func main() {
	broker := abroker.New(&osshim.OsShim{}, &ioutilshim.IoutilShim{})

...

what's included

Let's just look at the details of one of the packages: osshim

It is an interface for faking out your os, just in case your code interacts with the file system heavily and you want to be able to induce failures.

That batteries are included! The Os implementation in the base directory calls through to go's os package, The Os implementation in the fakes directory calls to a counterfeiter fake for use in test.

The other packages behave the same and are aptly named.

enjoy!

Feel free to PR more packages and we'll be happy to include them. Otherwise, we hope you find this as usefull as we do.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.
bufio_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
This file was generated by counterfeiter with command: counterfeiter -p os/exec BUT THEN WE MODIFIED IT SO MAKE SURE TO COPY THOSE MODIFICATIONS FORWARDS
This file was generated by counterfeiter with command: counterfeiter -p os/exec BUT THEN WE MODIFIED IT SO MAKE SURE TO COPY THOSE MODIFICATIONS FORWARDS
exec_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/local-volume-release/src/code.cloudfoundry.org/goshims/filepathshim path/filepath
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/local-volume-release/src/code.cloudfoundry.org/goshims/filepathshim path/filepath
filepath_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
Code generated by counterfeiter.
Code generated by counterfeiter.
grpc_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
http_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/local-volume-release/src/code.cloudfoundry.org/goshims/ioutilshim io/ioutil BUT THEN WE MODIFIED IT SO MAKE SURE TO COPY THOSE MODIFICATIONS FORWARDS
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/local-volume-release/src/code.cloudfoundry.org/goshims/ioutilshim io/ioutil BUT THEN WE MODIFIED IT SO MAKE SURE TO COPY THOSE MODIFICATIONS FORWARDS
ioutil_fake
This file was generated by counterfeiter
This file was generated by counterfeiter
ldap_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
mysql_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/local-volume-release/src/code.cloudfoundry.org/goshims/osshim os BUT THEN WE MODIFIED IT SO MAKE SURE TO COPY THOSE MODIFICATIONS FORWARDS
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/local-volume-release/src/code.cloudfoundry.org/goshims/osshim os BUT THEN WE MODIFIED IT SO MAKE SURE TO COPY THOSE MODIFICATIONS FORWARDS
os_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/nfs-volume-release/src/code.cloudfoundry.org/goshims/sqlshim database/sql
This file was generated by counterfeiter with command: counterfeiter -p -o /Users/pivotal/workspace/nfs-volume-release/src/code.cloudfoundry.org/goshims/sqlshim database/sql
sql_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
Code generated by counterfeiter.
Code generated by counterfeiter.
syscall_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
time_fake
Code generated by counterfeiter.
Code generated by counterfeiter.
This file was generated by counterfeiter with command: counterfeiter -p os/user
This file was generated by counterfeiter with command: counterfeiter -p os/user
user_fake
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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