testutils

package
v0.0.0-...-bc12874 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: BSD-3-Clause Imports: 16 Imported by: 15

Documentation

Overview

Package testutils contains convenience utilities for testing.

Index

Constants

This section is empty.

Variables

View Source
var AnyContext = mock.MatchedBy(func(c context.Context) bool {

	return true
})

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

func AssertCloses(t sktest.TestingT, c io.Closer)

AssertCloses takes an ioutil.Closer and asserts that it closes. E.g.: frobber := NewFrobber() defer testutils.AssertCloses(t, frobber)

func ExecTemplate

func ExecTemplate(t sktest.TestingT, tmpl string, data interface{}) string

ExecTemplate parses the given string as a text template, executes it using the given data, and returns the result as a string.

func Executable

func Executable(t sktest.TestingT) string

Executable returns the filesystem path to the binary running the current test, panicking on failure.

func FlagPath

func FlagPath(t sktest.TestingT, fileName string) string

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

func GetRepoRoot(t sktest.TestingT) string

GetRepoRoot returns the path to the root of the checkout.

func MarshalIndentJSON

func MarshalIndentJSON(t sktest.TestingT, i interface{}) string

MarshalIndentJSON encodes the given interface to an indented JSON string.

func MarshalJSON

func MarshalJSON(t sktest.TestingT, i interface{}) string

MarshalJSON encodes the given interface to a JSON string.

func MarshalJSONReader

func MarshalJSONReader(t sktest.TestingT, i interface{}) io.Reader

MarshalJSONReader encodes the given interface to an io.ByteReader of its JSON representation.

func ReadFile

func ReadFile(t sktest.TestingT, filename string) string

ReadFile reads a file from the caller's testdata directory.

func ReadFileBytes

func ReadFileBytes(t sktest.TestingT, filename string) []byte

ReadFileBytes reads a file from the caller's testdata directory and returns its contents as a slice of bytes.

func ReadJSONFile

func ReadJSONFile(t sktest.TestingT, filename string, dest interface{})

ReadJSONFile reads a JSON file from the caller's testdata directory into the given interface.

func Remove

func Remove(t sktest.TestingT, fp string)

Remove attempts to remove the given file and asserts that no error is returned.

func RemoveAll

func RemoveAll(t sktest.TestingT, fp string)

RemoveAll attempts to remove the given directory and asserts that no error is returned.

func SetUpFakeHomeDir

func SetUpFakeHomeDir(t sktest.TestingT, tempDirPattern string)

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

func TestDataDir(t sktest.TestingT) string

TestDataDir returns the path to the caller's testdata directory, which is assumed to be "<path to caller dir>/testdata".

func TestDataFilename

func TestDataFilename(t sktest.TestingT, relPathParts ...string) string

TestDataFilename returns the absolute path for the given relative path to a file in the local `testdata` directory.

func WriteFile

func WriteFile(t sktest.TestingT, filename, contents string)

WriteFile writes the given contents to the given file path, reporting any error.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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