filetest

package
v0.0.0-...-3fa40c8 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2023 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package filetest provides utility functions that can be used during unit testing. Any resource created by the package during a test is cleaned up automatically when the test is done.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearFile

func ClearFile(t *testing.T, file *os.File)

ClearFile truncates the content of a file.

func CreateDir

func CreateDir(t *testing.T) string

CreateDir creates a temporary directory. The directory is deleted after the test is done running.

func CreateFile

func CreateFile(t *testing.T, content string) *os.File

CreateFile creates a temporary file with the content specified used during the test. The file is closed and deleted after the test is done running.

func OverrideStdin

func OverrideStdin(t *testing.T, f *os.File)

OverrideStdin overrides os.Stdin with the specified file and restores it after the test.

func ReadFile

func ReadFile(t *testing.T, name string) []string

ReadFile reads a text file and returns each line in a slice. If the file name is empty, returns an empty slice.

Types

type StubReader

type StubReader struct {
	Buffer []byte
	Index  int
	Err    error
}

StubReader implements the io.ReadCloser interface. It is used during tests to return the content of its internal buffer, or to return an error as needed.

func NewStubReader

func NewStubReader(buffer []byte, err error) *StubReader

NewStubReader returns a new StubReader object that reads from the buffer specified. If err is not nil, it will return an error on the first read. If the end of the buffer is reached, it will return an io.EOF error.

func (*StubReader) Close

func (r *StubReader) Close() error

Close implements the io.Closer interface.

func (*StubReader) Read

func (r *StubReader) Read(p []byte) (int, error)

Read implements the io.Reader interface.

type StubWriter

type StubWriter struct {
	Buffer []byte
	Count  int
	Err    error
}

StubWriter implements the io.Writer interface. It is used during tests to examine the content of the data written, and to return fake errors as needed.

func NewStubWriter

func NewStubWriter(err error) *StubWriter

NewStubWriter returns a new StubWriter.

func (*StubWriter) Close

func (w *StubWriter) Close() error

Close implements the io.Closer interface.

func (*StubWriter) Write

func (w *StubWriter) Write(p []byte) (n int, err error)

Write implements the io.Writer interface.

Jump to

Keyboard shortcuts

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