Documentation ¶
Overview ¶
Package testutils contains convenience utilities for testing.
Index ¶
- Variables
- func AssertCloses(t sktest.TestingT, c io.Closer)
- func ExecTemplate(t sktest.TestingT, tmpl string, data interface{}) string
- func Executable(t sktest.TestingT) string
- func FlagPath(t sktest.TestingT, fileName string) string
- func GetReader(t sktest.TestingT, filename string) io.ReadCloser
- func GetRepoRoot(t sktest.TestingT) string
- func MarshalIndentJSON(t sktest.TestingT, i interface{}) string
- func MarshalJSON(t sktest.TestingT, i interface{}) string
- func MarshalJSONReader(t sktest.TestingT, i interface{}) io.Reader
- func ReadFile(t sktest.TestingT, filename string) string
- func ReadFileBytes(t sktest.TestingT, filename string) []byte
- func ReadJSONFile(t sktest.TestingT, filename string, dest interface{})
- func Remove(t sktest.TestingT, fp string)
- func RemoveAll(t sktest.TestingT, fp string)
- func SetUpFakeHomeDir(t sktest.TestingT, tempDirPattern string)
- func TestDataDir(t sktest.TestingT) string
- func TestDataFilename(t sktest.TestingT, relPathParts ...string) string
- func WriteFile(t sktest.TestingT, filename, contents string)
Constants ¶
This section is empty.
Variables ¶
AnyContext can be used to match any Context objects e.g. m.On("Foo", testutils.AnyContext).Return(...) This is better than trying to used mock.AnythingOfTypeArgument because that only works for concrete types, which could be brittle (e.g. a "normal" context is *context.emptyCtx, but one modified by trace.StartSpan() could be a *context.valueCtx)
Functions ¶
func AssertCloses ¶
AssertCloses takes an ioutil.Closer and asserts that it closes. E.g.: frobber := NewFrobber() defer testutils.AssertCloses(t, frobber)
func ExecTemplate ¶
ExecTemplate parses the given string as a text template, executes it using the given data, and returns the result as a string.
func Executable ¶
Executable returns the filesystem path to the binary running the current test, panicking on failure.
func FlagPath ¶
FlagPath returns the path to a temp file for use as a synchronization flag between the test runner and a mocked-out subprocess launched by tested code; see go/flag-files. It lives next to the test executable and has the specified name, which should contain the name of the test for uniqueness across concurrent tests. It also asserts the absence of said file as a safety measure against leftover files from earlier runs—admittedly unlikely given "go test"'s and Bazel's proclivity for running everything in temp dirs.
func GetReader ¶
func GetReader(t sktest.TestingT, filename string) io.ReadCloser
GetReader reads a file from the caller's testdata directory and panics on error.
func GetRepoRoot ¶
GetRepoRoot returns the path to the root of the checkout.
func MarshalIndentJSON ¶
MarshalIndentJSON encodes the given interface to an indented JSON string.
func MarshalJSON ¶
MarshalJSON encodes the given interface to a JSON string.
func MarshalJSONReader ¶
MarshalJSONReader encodes the given interface to an io.ByteReader of its JSON representation.
func ReadFileBytes ¶
ReadFileBytes reads a file from the caller's testdata directory and returns its contents as a slice of bytes.
func ReadJSONFile ¶
ReadJSONFile reads a JSON file from the caller's testdata directory into the given interface.
func RemoveAll ¶
RemoveAll attempts to remove the given directory and asserts that no error is returned.
func SetUpFakeHomeDir ¶
SetUpFakeHomeDir creates a temporary dir and updates the HOME environment variable with its path. After the caller test completes, the HOME environment variable will be restored to its original value, and the temporary dir will be deleted.
Under Bazel, this is useful because Bazel does not set the HOME environment variable. Without this, some tests that call the "go" binary fail, because some "go" subcommands create a cache under $HOME/.cache/go-build.
Outside of Bazel (i.e. "go test"), this is still useful because it leads to more hermetic tests which do not depend on the specifics of the $HOME directory in the host system.
See https://docs.bazel.build/versions/master/test-encyclopedia.html#initial-conditions.
func TestDataDir ¶
TestDataDir returns the path to the caller's testdata directory, which is assumed to be "<path to caller dir>/testdata".
func TestDataFilename ¶
TestDataFilename returns the absolute path for the given relative path to a file in the local `testdata` directory.
Types ¶
This section is empty.