I\'m trying to test one of my package to reach 100%. However, I can\'t find how I can do this without being \"against the system\" (functions pointers, etc.).
I trie
I attempted to do same thing, just to try it. I achieved it by referencing all system file calls as interfaces and having method accept an interface. Then if no interface was provided the system method was used. I am brand new to Go, so I'm not sure if it violates best practices or not.
import "io/ioutil"
type ReadFile func (string) ([]byte, error)
type FileLoader interface {
LoadPath(path string) []byte
}
// Initializes a LocalFileSystemLoader with default ioutil.ReadFile
// as the method to read file. Optionally allows caller to provide
// their own ReadFile for testing.
func NewLocalFileSystemLoader(rffn ReadFile) *localFileSystemLoader{
var rf ReadFile = ioutil.ReadFile
if rffn != nil {
rf = rffn
}
return &localFileSystemLoader{
ReadFileFn: rf}
}
type localFileSystemLoader struct {
ReadFileFn ReadFile
}
func (lfsl *localFileSystemLoader) LoadPath(path string) []byte {
dat, err := lfsl.ReadFileFn(path)
if err != nil {
panic(err)
}
return dat
}