Documentation ¶
Overview ¶
Package fakefs contains fake implementations of interfaces from package io/fs from the standard library.
It is recommended to fill all methods that shouldn't be called with:
panic("not implemented")
in the body of the test, so that if the method is called the panic backtrace points to the method definition in the test. See the package example.
Example ¶
package main import ( "fmt" "io" "io/fs" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/testutil/fakefs" ) func main() { const testMsg = "test message" isClosed := false fakeFile := &fakefs.File{ // Use OnClose to record the closure of the file. OnClose: func() (err error) { isClosed = true return nil }, // Use OnRead to return the fake data. OnRead: func(b []byte) (n int, err error) { copy(b, testMsg) return len(testMsg), io.EOF }, // Use OnStat with a panic to signal that Stat is expected to not be // called. OnStat: func() (fi fs.FileInfo, err error) { panic("not implemented") }, } fakeFS := &fakefs.FS{ OnOpen: func(_ string) (f fs.File, err error) { return fakeFile, nil }, } // The function that is expected to call Read and Close. testedFunction := func(fsys fs.FS) (b []byte, err error) { f, err := fsys.Open("my_file.txt") if err != nil { return nil, fmt.Errorf("opening: %w", err) } defer func() { err = errors.WithDeferred(err, f.Close()) }() b = make([]byte, len(testMsg)*2) n, err := f.Read(b) if err != nil && !errors.Is(err, io.EOF) { return nil, fmt.Errorf("reading: %w", err) } return b[:n], nil } // A simulation of a successful test. gotData, gotErr := testedFunction(fakeFS) fmt.Printf("read: %v %q\n", gotErr, gotData) fmt.Printf("closed: %t\n", isClosed) }
Output: read: <nil> "test message" closed: true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type File ¶
type File struct { OnClose func() error OnRead func(b []byte) (n int, err error) OnStat func() (fi fs.FileInfo, err error) }
File is the fs.File for tests.
type GlobFS ¶
type GlobFS struct { OnOpen func(name string) (f fs.File, err error) OnGlob func(pattern string) (paths []string, err error) }
GlobFS is the fs.GlobFS for tests.
Click to show internal directories.
Click to hide internal directories.