testutil

package
v0.0.0-...-36397d2 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package testutil provides functionality commonly used by tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Caller

func Caller(depth int) string

Caller returns a string of the form <file>:<line> for the caller at the specified depth.

func CreateDirectoryTree

func CreateDirectoryTree(t Testing, parent string, depth, fanout, files int)

CreateDirectoryTree creates a directory tree for use in tests. Parent specifies the root directory, depth the number of directory levels and fanout the number of directories at each level; files is the number of files to create at each level. Directories are named d0..n. Files are named f0..n and the contents of each file are its own name.

func ExpectedError

func ExpectedError(t *testing.T, depth int, err error, msg string)

ExpectedError tests for an expected error and associated error message.

func FailOnError

func FailOnError(depth int, t interface {
	Fatal(...interface{})
}, err error)

FailOnError will call Fatal if the supplied error parameter is non-nil, with the location of its caller at the specified depth and the non-nil error.

func GetFilePath

func GetFilePath(relativePath string) string

GetFilePath detects if we're running under "bazel test". If so, it builds a path to the test data file based on Bazel environment variables. Otherwise, it tries to build a path relative to $GRAIL. If that fails, it returns the input path unchanged.

relativePath will need to be prefixed with a Bazel workspace designation if the paths go across workspaces. For example, "@grail//my/file/path" will address a file relative to the $GRAIL root instead of relative to the $GRAIL/go/src/grail.com path of the grailgo workspace. Only certain workspaces are recognized when running tests for the Go tool. Add more workspaces as necessary in the map below.

func GetTmpDir

func GetTmpDir() string

GetTmpDir will retrieve/generate a test-specific directory appropriate for writing scratch data. When running under Bazel, Bazel should clean up the directory. However, when running under vanilla Go tooling, it will not be cleaned up. Thus, it's probably best for a test to clean up any test directories itself.

func GoExecutable

func GoExecutable(t interface {
	Fatalf(string, ...interface{})
},
	sh *gosh.Shell,
	path string) string

GoExecutable returns the Go executable for "path", or builds the executable and returns its path. The latter happens when the caller is not running under Bazel. "path" must start with "@grailgo//". For example, "@grailgo//cmd/bio-metrics/bio-metrics".

func ListRecursively

func ListRecursively(t Testing, parent string) (dirs []string, files []string)

ListRecursively recursively lists the files and directories starting at parent.

func NoCleanupOnError

func NoCleanupOnError(t interface {
	Fail()
	Log(...interface{})
	Failed() bool
}, cleanup func(), args ...interface{})

NoCleanupOnError avoids calling the supplied cleanup function when a test has failed or paniced. The Log function is called with args when the test has failed and is typically used to log the location of the state that would have been removed by the cleanup function. Common usage would be:

tempdir, cleanup := testutil.TempDir(t, "", "scandb-state-") defer testutil.NoCleanupOnError(t, cleanup, "tempdir:", tempdir)

func SetTMPDIR

func SetTMPDIR(d string) string

SetTMPDIR will cause os.TempDir to return the supplied value. It assumes that the enivornment variable TMPDIR is preferred by os.TempDir() and hence manipulates that environment variable. A common usage is from within tests to cause os.TempDir to return a bogus value and to trigger subsequent errors.

func TempDir

func TempDir(t Testing, dir, prefix string) (name string, cleanup func())

TempDir is like ioutil.TempDir but intended for use from within tests. In particular, it will t.Fatal if it fails and returns a function that can be defer'ed by the caller to remove the newly created directory.

Types

type ByteContent

type ByteContent struct {
	Data []byte
}

ByteContent stores data for content storage tests.

func (*ByteContent) ReadAt

func (bc *ByteContent) ReadAt(p []byte, off int64) (int, error)

ReadAt reads from the specified offset

func (*ByteContent) Set

func (bc *ByteContent) Set(data []byte)

Set sets the contents

func (*ByteContent) Size

func (bc *ByteContent) Size() int64

Size returns the size of the contents

func (*ByteContent) WriteAt

func (bc *ByteContent) WriteAt(p []byte, off int64) (int, error)

WriteAt writes at the specified offset

type ContentAt

type ContentAt interface {
	io.ReaderAt
	io.WriterAt

	Size() int64
}

ContentAt allows users of test clients to implement their own content storage. This is useful when mocking very large files.

type FakeContentAt

type FakeContentAt struct {
	T interface {
		Fatal(...interface{})
	}
	SizeInBytes int64
	Current     int64
	FailureRate float64
}

FakeContentAt implements io.[Reader|ReaderAt|Seeker|Writer] using a virtual file with a predictable pattern. The Read* interfaces will data for the slice based on the virtual file containing a repeating pattern containing the lowercase alphabet. The WriteAt function will verify that the pattern is maintained. This enables unittests with large files without paying the performance penalty of disk writes.

func (*FakeContentAt) Read

func (fca *FakeContentAt) Read(p []byte) (int, error)

Read implements the io.Reader.

func (*FakeContentAt) ReadAt

func (fca *FakeContentAt) ReadAt(p []byte, off int64) (int, error)

ReadAt implements io.ReaderAt.

func (*FakeContentAt) Seek

func (fca *FakeContentAt) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker.

func (*FakeContentAt) Size

func (fca *FakeContentAt) Size() int64

Size returns the size of the fake content.

func (*FakeContentAt) Write

func (fca *FakeContentAt) Write(p []byte) (int, error)

Write implements io.Writer.

func (*FakeContentAt) WriteAt

func (fca *FakeContentAt) WriteAt(p []byte, off int64) (int, error)

WriteAt implements io.WriterAt.

type MockTB

type MockTB struct {
	Failed bool
	Result string
}

MockTB is a mock implementation of gosh.TB. FailNow and Fatalf will set Failed to true. Logf and Fatalf write their log message to Result. MockTB is intended for building negatived tests.

func (*MockTB) FailNow

func (m *MockTB) FailNow()

FailNow implements TB.

func (*MockTB) Fatalf

func (m *MockTB) Fatalf(format string, args ...interface{})

Fatalf implements TB.

func (*MockTB) Logf

func (m *MockTB) Logf(format string, args ...interface{})

Logf implements TB.

type Testing

type Testing interface {
	FailNow()
	Logf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
}

Testing is a subset of testing.TB that allows an instance of testing.T to be passed without having to import testing into this non-test package.

Directories

Path Synopsis
Package encryptiontest provides support for testing encryption and associated key management.
Package encryptiontest provides support for testing encryption and associated key management.

Jump to

Keyboard shortcuts

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