fakefs

package
v0.30.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: Unlicense Imports: 1 Imported by: 0

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 FS

type FS struct {
	OnOpen func(name string) (fs.File, error)
}

FS is the fs.FS for tests.

func (*FS) Open

func (fsys *FS) Open(name string) (f fs.File, err error)

Open implements the fs.FS interface for *FS.

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.

func (*File) Close

func (f *File) Close() (err error)

Close implements the fs.File interface for *File.

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)

Read implements the fs.File interface for *File.

func (*File) Stat

func (f *File) Stat() (fi fs.FileInfo, err error)

Stat implements the fs.File interface for *File.

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.

func (*GlobFS) Glob

func (fsys *GlobFS) Glob(pattern string) (paths []string, err error)

Glob implements the fs.GlobFS interface for *GlobFS.

func (*GlobFS) Open

func (fsys *GlobFS) Open(name string) (f fs.File, err error)

Open implements the fs.GlobFS interface for *GlobFS.

type StatFS

type StatFS struct {
	OnOpen func(name string) (f fs.File, err error)
	OnStat func(name string) (fi fs.FileInfo, err error)
}

StatFS is the fs.StatFS for tests.

func (*StatFS) Open

func (fsys *StatFS) Open(name string) (f fs.File, err error)

Open implements the fs.StatFS interface for *StatFS.

func (*StatFS) Stat

func (fsys *StatFS) Stat(name string) (fi fs.FileInfo, err error)

Stat implements the fs.StatFS interface for *StatFS.

Jump to

Keyboard shortcuts

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