lfilemock

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package lfilemock provides some mock file objects. This is not meant to be exhaustive, it is used to cover the cases needed to test luce. Over time it will expand.

Index

Constants

View Source
const (
	ErrNewType   = lerr.Str("lfilemock.New contents must be string or []byte")
	ErrParseType = lerr.Str("lfilemock.Parse values must be string, []byte or map[string]any")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteFile

type ByteFile struct {
	Name string
	Data []byte
}

ByteFile is used to create files in mock directory trees. ByteFiles are created when calling ParseDir.

func (*ByteFile) DirEntry

func (f *ByteFile) DirEntry() os.DirEntry

DirEntry fulfills Node. It creates a DirEntry instance for the ByteFile.

func (*ByteFile) File

func (f *ByteFile) File() *File

File fulfills Node. It uses a ByteFile to create an instance of File that fulfills lfile.File.

func (*ByteFile) Next

func (f *ByteFile) Next(key string, create bool, _ navigator.VoidContext) (Node, bool)

Next fulfills Node and navigator.Nexter. It is used internally for navigating the mock directory.

type DirEntry

type DirEntry struct {
	EntryName string
	Dir       bool
	fs.FileMode
	Err error
	fs.FileInfo
}

DirEntry fulfills os.DirEntry.

func (*DirEntry) Info

func (de *DirEntry) Info() (fs.FileInfo, error)

Info returns FileInfo and Err

func (*DirEntry) IsDir

func (de *DirEntry) IsDir() bool

IsDir returns Dir

func (*DirEntry) Name

func (de *DirEntry) Name() string

Name returns EntryName

func (*DirEntry) Type

func (de *DirEntry) Type() fs.FileMode

Type returns FileMode

type Directory

type Directory struct {
	Name     string
	Children map[string]Node
	Err      error
}

Directory is used to create mock directory trees. Directories are created when calling ParseDir.

func Parse

func Parse(root map[string]any) *Directory

ParseDir allows for mock directory trees to be setup easily. To create a File the value can be either a string or a []byte. To create a sub directory the value should be another map[string]any which will be recursivly parsed.

func (*Directory) AddDir

func (d *Directory) AddDir(name string) *Directory

AddDir adds a sub directory. This can be used when modifying the mock directory tree.

func (*Directory) AddFile

func (d *Directory) AddFile(name string, contents []byte) *ByteFile

AddFile adds a ByteFile. This can be used when modifying the mock directory tree.

func (*Directory) DirEntry

func (d *Directory) DirEntry() os.DirEntry

DirEntry fulfills Node. It creates a DirEntry instance for the Directory.

func (*Directory) File

func (d *Directory) File() *File

File fulfills Node. It creates an instance of *File that fulfills lfile.File.

func (*Directory) Get

func (d *Directory) Get(path string) (n Node, found bool)

func (*Directory) Next

func (d *Directory) Next(key string, create bool, _ navigator.VoidContext) (Node, bool)

Next fulfills Node and navigator.Nexter. It is used internally for navigating the mock directory.

func (*Directory) Repository

func (d *Directory) Repository() lfile.Repository

Parse allows for mock directory trees to be setup easily. To create a File the value can be either a string or a []byte. To create a sub directory the value should be another map[string]any which will be recursivly parsed. The returned Repository fulfills lfile.Repository.

type File

type File struct {
	FileName string
	*bytes.Buffer
	Dir        bool
	DirEntries []os.DirEntry
	os.FileInfo
	FileSize int64
	os.FileMode
	Err error
}

File mock allows for *os.File to be simulated including various errors.

func New

func New(name string, contents any) *File

New creates a file using either a string or []byte.

func (*File) Close

func (f *File) Close() error

Close returns f.Err.

func (*File) Name

func (f *File) Name() string

Name returns Filename

func (*File) Read

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

Read returns f.Err is that is set, otherwise it wraps f.Buffer.Read.

func (*File) ReadDir

func (f *File) ReadDir(n int) ([]os.DirEntry, error)

ReadDir returns DirEntries and Err.

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

Stat creates an instance of lfilemock.FileInfo derived from the instance of File.

type FileInfo

type FileInfo struct {
	FileName string
	FileSize int64
	os.FileMode
	Mod time.Time
	Dir bool
}

FileInfo fulfills os.FileInfo

func (*FileInfo) IsDir

func (fi *FileInfo) IsDir() bool

IsDir returns Dir

func (*FileInfo) ModTime

func (fi *FileInfo) ModTime() time.Time

ModTime returns Mod

func (*FileInfo) Mode

func (fi *FileInfo) Mode() os.FileMode

Mode returns FileMode

func (*FileInfo) Name

func (fi *FileInfo) Name() string

Name returns Filename

func (*FileInfo) Size

func (fi *FileInfo) Size() int64

Size returns Filesize

func (*FileInfo) Sys

func (fi *FileInfo) Sys() any

Sys exists to fulfill os.FileInfo but always returns nil.

type Node

type Node interface {
	File() *File
	DirEntry() os.DirEntry
	Next(key string, create bool, vc navigator.VoidContext) (Node, bool)
}

Node is fulfilled by ByteFile and Directory creating a mock directory tree.

type Repository

type Repository struct {
	Node
}

Repository fulfills lfile.Repository and is intended to mock a File system.

func (*Repository) Create

func (r *Repository) Create(name string) (lfile.File, error)

Create fulfills lfile.Repository. It creates a file.

func (*Repository) Open

func (r *Repository) Open(name string) (lfile.File, error)

Open fulfills lfile.Repository. It opens a file or directory if it exists.

func (*Repository) Remove

func (r *Repository) Remove(name string) error

Remove fulfills lfile.Repository. It removes a file if it exists.

Jump to

Keyboard shortcuts

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