filesystem

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package filesystem provides an abstraction over the native file system operations.

This allows for easy testing and mocking of file system interactions.

Below, the package filesystem (@file_system_mock.go) furnishes a mock implementation of the FileSystem interface intended for testing. It enables the monitoring of file operations and the emulation of file system interactions without real disk I/O, showcasing the "magic" of Go programming language (🎩🪄).

Additionally, it can be utilized for tracking file activities during tests or without tests (as Expert).

Copyright (c) 2023 H0llyW00dzZ

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileLike added in v1.2.6

type FileLike interface {
	Close() error
	Read(p []byte) (n int, err error)
	Write(p []byte) (n int, err error)
	Seek(offset int64, whence int) (int64, error)
}

FileLike is an interface that includes the subset of *os.File methods that you need to mock.

type FileSystem

type FileSystem interface {
	Create(name string) (*os.File, error)
	WriteFile(name string, data []byte, perm fs.FileMode) error
	ReadFile(name string) ([]byte, error) // Added ReadFile method
	Stat(name string) (os.FileInfo, error)
	FileExists(name string) (bool, error) // Added FileExists method to the interface
}

FileSystem is an interface that abstracts file system operations such as creating files, writing to files, and retrieving file information. This allows for implementations that can interact with the file system or provide mock functionality for testing purposes. FileSystem interface now includes ReadFile method.

type MockExporter

type MockExporter struct {
	ErrToReturn error // ErrToReturn is the error that ConvertSessionsToCSV will return when called.
}

MockExporter is a mock implementation of the exporter.Exporter interface for testing purposes. It allows for the simulation of session conversion to CSV format and can be set to return errors for testing error handling.

Note: this types is proof of concept after touring golang.

func (*MockExporter) ConvertSessionsToCSV

func (m *MockExporter) ConvertSessionsToCSV(ctx context.Context, sessions []exporter.Session, formatOption int, csvFileName string) error

ConvertSessionsToCSV simulates the conversion of sessions to CSV format. It returns an error specified by ErrToReturn, allowing for error handling tests.

Note: this function is proof of concept after touring golang.

type MockFile added in v1.2.6

type MockFile struct {
	// contains filtered or unexported fields
}

MockFile simulates an os.File for testing purposes, may you not using this if you are not Expert.

func (*MockFile) Close added in v1.2.6

func (mf *MockFile) Close() error

Close simulates closing the file, it's a no-op for the mock.

func (*MockFile) Read added in v1.2.6

func (mf *MockFile) Read(p []byte) (n int, err error)

Read simulates reading bytes from the file.

func (*MockFile) Seek added in v1.2.6

func (mf *MockFile) Seek(offset int64, whence int) (int64, error)

Seek simulates seeking in the file, it's a no-op for the mock.

func (*MockFile) Write added in v1.2.6

func (mf *MockFile) Write(p []byte) (n int, err error)

Write simulates writing bytes to the file.

type MockFileSystem

type MockFileSystem struct {
	Files                 map[string][]byte // Files maps file names to file contents.
	WriteFileCalled       bool              // Track if WriteFile has been called.
	WriteFilePath         string            // Track the path provided to WriteFile.
	WriteFileData         []byte            // Track the data provided to WriteFile.
	WriteFilePerm         fs.FileMode       // Track the file permissions provided to WriteFile.
	FileExistsCalled      bool              // Track if FileExists has been called.
	FileExistsErr         error             // Track the error to return from FileExists.
	FileExistsShouldError bool              // Track if FileExists should return an error.
	ReadFileCalled        bool              // this field to track if ReadFile has been caled.
	ReadFileData          []byte            // Optionally track the data provided to ReadFile.
	ReadFileErr           error             // Optionally track the error provider to ReadFile.
}

MockFileSystem is a mock implementation of the FileSystem interface that can be used in tests. It uses a map to store file names and associated data, allowing for the simulation of file creation, reading, and writing without actual file system interaction.

func NewMockFileSystem added in v1.2.0

func NewMockFileSystem() *MockFileSystem

NewMockFileSystem creates a new instance of MockFileSystem with initialized internal structures.

func (*MockFileSystem) Close added in v1.2.6

func (mf *MockFileSystem) Close() error

Implement the Close method if needed for testing

func (*MockFileSystem) Create

func (m *MockFileSystem) Create(name string) (*os.File, error)

Create simulates the creation of a file by adding a new entry in the Files map.

func (*MockFileSystem) FileExists added in v1.2.1

func (m *MockFileSystem) FileExists(name string) (bool, error)

FileExists checks if the given file name exists in the mock file system.

func (*MockFileSystem) ReadFile

func (m *MockFileSystem) ReadFile(name string) ([]byte, error)

ReadFile simulates reading the content of a file from the Files map. If the file exists, it returns the content as a byte slice; otherwise, it returns an error.

func (*MockFileSystem) Stat

func (m *MockFileSystem) Stat(name string) (fs.FileInfo, error)

Stat returns the FileInfo for the given file name if it exists in the mock file system. If the file does not exist, it returns an error to simulate the os.Stat behavior.

func (*MockFileSystem) WriteFile

func (m *MockFileSystem) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile simulates writing data to a file in the Files map. It creates a new buffer with the provided data, simulating a successful write operation.

type RealFileSystem

type RealFileSystem struct{}

RealFileSystem implements the FileSystem interface by wrapping the os package functions, thus providing an actual file system interaction mechanism.

func (RealFileSystem) Create

func (rfs RealFileSystem) Create(name string) (*os.File, error)

Create creates a new file with the given name. It wraps the os.Create function and returns a pointer to the created file along with any error encountered.

func (RealFileSystem) FileExists added in v1.2.1

func (rfs RealFileSystem) FileExists(name string) (bool, error)

FileExists checks if a file exists in the file system at the given path. It returns a boolean indicating existence, and an error for any underlying filesystem issues encountered.

func (RealFileSystem) ReadFile added in v1.2.0

func (rfs RealFileSystem) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns the contents. It wraps the os.ReadFile function.

func (RealFileSystem) Stat

func (rfs RealFileSystem) Stat(name string) (os.FileInfo, error)

Stat returns the FileInfo structure describing the file named by the given name. It wraps the os.Stat function and returns the FileInfo and any error encountered, for instance, if the file does not exist.

func (RealFileSystem) WriteFile

func (rfs RealFileSystem) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile writes data to a file named by filename. If the file does not exist, WriteFile creates it with permissions perm; otherwise WriteFile truncates it before writing.

Jump to

Keyboard shortcuts

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