I am having a function that opens a file and writes to it using the os.OpenFile
function.
What I basically want to do is mock the File that is getting returned by it in order to write tests. Because I want to better understand Go, I'd like to do it without using third party packages.
Here is what I have tried:
My Package
package logger
import (
"fmt"
"time"
"sync"
"os"
"strings"
"path/filepath"
"io"
)
const timestampFormat = "2006-01-02 15:04:05.999999999"
type FileOpener interface {
OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
}
type RotateWriter struct {
fileOpener FileOpener
lock sync.Mutex
filename string
fp *os.File
}
type defaultFileOpener struct{}
func (fo defaultFileOpener) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
return os.OpenFile(name, flag, perm)
}
func CreateRotateWriter(filename string, fileOpener FileOpener) (RotateWriter) {
if (fileOpener == nil) {
return RotateWriter{filename: filename, fileOpener: defaultFileOpener{}}
}
return RotateWriter{filename: filename, fileOpener: fileOpener}
}
func (writer RotateWriter) Write(bytes []byte) (int, error) {
writer.lock.Lock()
defer writer.lock.Unlock()
extension := filepath.Ext(writer.filename)
filename := strings.TrimSuffix(writer.filename, extension)
// There is a specific constants that are used for formatting dates.
// For example 2006 is the YYYYY, 01 is MM and 02 is DD
// Check https://golang.org/src/time/format.go line 88 for reference
fullFilename := filename + time.Now().UTC().Format("-2006-01-02") + extension
// Open the file for the first time if we don't already did so
if writer.fp == nil {
fp, err := os.OpenFile(fullFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return 0, err
}
writer.fp = fp;
}
// We are now pointing to a different file. Close the previous one and open a new one
if fullFilename != writer.fp.Name() {
writer.fp.Close()
fp, err := os.OpenFile(fullFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return 0, err
}
writer.fp = fp;
}
return writer.fp.Write([]byte("[" + time.Now().UTC().Format(timestampFormat) + "] " + string(bytes)))
}
What I was hoping to do in my test package is something like this
type file interface {
io.Closer
io.Reader
io.ReaderAt
io.Seeker
Stat() (os.FileInfo, error)
}
type fileType struct{
fd int
name string
contents string // Where I'll keep the in-memory contents maybe
}
type defaultFileOpener struct{
}
func (fo defaultFileOpener) OpenFile(name string, flag int, perm os.FileMode) (*file, error){
return &fileType{1, name, ""}, nil //Cannot use &fileType{1, name, ""}(type *fileType) as type *file
}
func (f fileType) Write(bytes []byte) (int, error){
f.contents += string(bytes)
return len(bytes), nil
}
I'm most probably misunderstand something, is it even possible to create my own File type in go?
From the snippet it's not clear whether or not the *fileType
implements all of the methods of the file
interface, but if it doesn't you should first make sure that it does. Because if it doesn't you'll not be able to use it in your tests as you might intend.
Your file opener's OpenFile
method should have the interface as its return type, not the pointer to the interface. That is:
func (fo defaultFileOpener) OpenFile(name string, flag int, perm os.FileMode) (file, error) {
This is because *file
is not the same type as file
, and a value of a type that implements the file
interface (e.g. yours *fileType
) cannot be used where the pointer to that interface is expected.
And returning a pointer to an interface is almost never what you actually want, maybe it would make sense to do that if you wanted switch the interface value for another using pointer indirection, but that does not seem to be the case here. If you scan through the standard library you'll have a hard time finding functions/methods that return pointers to interface types...
But let's say that that's what you want to do then you have to return a pointer to a value of the interface type, not a pointer to a type that implements the interface.
E.g.
var f file = &fileType{} // f is of type file
return &f, nil // &f is of type *file
Keep in mind that f
's type has to be file
for the return to work, if it's just *fileType
it will not compile.
var f = &fileType{} // f is of type *fileType
return &f, nil // &f is of type **fileType