Documentation ¶
Overview ¶
Package testutil provides functionality commonly used by tests.
Index ¶
- func Caller(depth int) string
- func CompareFile(t testing.TB, contents string, golden string, strip func(string) string)
- func CompareFiles(t testing.TB, a, golden string, strip func(string) string)
- func CreateDirectoryTree(t Testing, parent string, depth, fanout, files int)
- func GetFilePath(relativePath string) string
- func GetTmpDir() string
- func GetTmpPath() string
- func GoExecutable(t testing.TB, path string) string
- func GoExecutableEnv(t testing.TB, path string, env []string) string
- func IsBazel() bool
- func ListRecursively(t Testing, parent string) (dirs []string, files []string)
- func NoCleanupOnError(t testing.TB, cleanup func(), args ...interface{})
- func TempDir(t Testing, dir, prefix string) (name string, cleanup func())
- type ByteContent
- type ContentAt
- type FakeContentAt
- func (fca *FakeContentAt) Checksum() string
- 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 CompareFile ¶
CompareFile compares the supplied contents against the contents of the specified file and if they differ calls t.Errorf and displays a diff -u of them. If specified the strip function can be used to cleanup the contents to be compared to remove things such as dates or other spurious information that's not relevant to the comparison.
func CompareFiles ¶
CompareFiles compares 2 files in the same manner as CompareFile.
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 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.
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 GetTmpPath ¶ added in v0.0.2
func GetTmpPath() string
GetTmpPath returns a random file inside of the appropriate scratch directory. The path is neither created nor cleaned up -- clients are expected to use do both.
func GoExecutable ¶
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 "//go/src/grail.com/". For example, "//go/src/grail.com/cmd/bio-metrics/bio-metrics".
func GoExecutableEnv ¶
GoExecutableEnv is like GoExecutable but allows environment variables to be specified.
func IsBazel ¶
func IsBazel() bool
IsBazel checks if the current process is started by "bazel test".
func ListRecursively ¶
ListRecursively recursively lists the files and directories starting at parent.
func NoCleanupOnError ¶
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)
Types ¶
type ByteContent ¶
type ByteContent struct {
Data []byte
}
ByteContent stores data for content storage tests.
func (*ByteContent) Checksum ¶
func (bc *ByteContent) Checksum() string
Checksum implements ContentAt.
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 ¶
type ContentAt interface { io.ReaderAt io.WriterAt // Size returns the total byte count of the contents. Size() int64 // Checksum returns the checksum of the contents. It is typically an MD5 hex // string, following the S3 convention. Checksum() string }
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) Checksum ¶
func (fca *FakeContentAt) Checksum() string
Checksum implements ContentAt.
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 assert provides helper functions for unittests, in a style of hamcrest, gtest and gmock.
|
Package assert provides helper functions for unittests, in a style of hamcrest, gtest and gmock. |
Package encryptiontest provides support for testing encryption and associated key management.
|
Package encryptiontest provides support for testing encryption and associated key management. |
Package expect provides helper functions for unittests, in a style of hamcrest, gtest and gmock.
|
Package expect provides helper functions for unittests, in a style of hamcrest, gtest and gmock. |
Package h provides matchers used by assert and expect packages.
|
Package h provides matchers used by assert and expect packages. |