Documentation ¶
Overview ¶
Package testutil provides functionality commonly used by tests.
Index ¶
- func Caller(depth int) string
- func CreateDirectoryTree(t Testing, parent string, depth, fanout, files int)
- func ExpectedError(t *testing.T, depth int, err error, msg string)
- func FailOnError(depth int, t interface{ ... }, err error)
- func GetFilePath(relativePath string) string
- func GetTmpDir() string
- func GoExecutable(t interface{ ... }, sh *gosh.Shell, path string) string
- func ListRecursively(t Testing, parent string) (dirs []string, files []string)
- func NoCleanupOnError(t interface{ ... }, cleanup func(), args ...interface{})
- func SetTMPDIR(d string) string
- func TempDir(t Testing, dir, prefix string) (name string, cleanup func())
- type ByteContent
- type ContentAt
- type FakeContentAt
- func (fca *FakeContentAt) Read(p []byte) (int, error)
- func (fca *FakeContentAt) ReadAt(p []byte, off int64) (int, error)
- func (fca *FakeContentAt) Seek(offset int64, whence int) (int64, error)
- func (fca *FakeContentAt) Size() int64
- func (fca *FakeContentAt) Write(p []byte) (int, error)
- func (fca *FakeContentAt) WriteAt(p []byte, off int64) (int, error)
- type MockTB
- type Testing
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Caller ¶
Caller returns a string of the form <file>:<line> for the caller at the specified depth.
func CreateDirectoryTree ¶
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 ¶
ExpectedError tests for an expected error and associated error message.
func FailOnError ¶
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 ¶
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 ¶
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 ¶
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.
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) Size ¶
func (bc *ByteContent) Size() int64
Size returns the size of the contents
type ContentAt ¶
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.
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. |