Documentation ¶
Overview ¶
Package testutil contains util functions to facilitate tests.
Package testutil contains util functions to facilitate tests.
Index ¶
- Variables
- func LoadDir(tb testing.TB, dir string, opts ...LoadDirOpt) map[string]string
- func LoadDirMode(tb testing.TB, dir string, opts ...LoadDirOpt) map[string]ModeAndContents
- func Overwrite(tb testing.TB, path, contents string)
- func OverwriteJoin(tb testing.TB, dir, name, contents string)
- func Prepend(tb testing.TB, dir, baseName, contents string)
- func Remove(tb testing.TB, dir, baseName string)
- func TestMustGlob(tb testing.TB, glob string) (string, bool)
- func TransformStructFields(transform cmp.Option, structExample interface{}, fieldNames ...string) cmp.Option
- func TrimStringPrefixTransformer(prefix string) cmp.Option
- func WithGitRepoAt(prefix string, m map[string]string) map[string]string
- func WriteAll(tb testing.TB, root string, files map[string]string)
- func WriteAllMode(tb testing.TB, root string, files map[string]ModeAndContents)
- type LoadDirOpt
- type ModeAndContents
Constants ¶
This section is empty.
Variables ¶
var ( // This is the SHA of the only commit in the repo above. MinimalGitHeadSHA = "5597fc600ead69ad92c81a22b58c9e551cd86b9a" MinimalGitHeadShortSHA = MinimalGitHeadSHA[:7] )
A minimal but actually valid .git directory that allows running real git commands. This was created by doing:
- git init myrepo && cd myrepo
- git commit --allow-empty -m 'Initial commit'
- dump the contents of .git, ignoring all files other than those below, because they're not strictly necessary.
Functions ¶
func LoadDir ¶ added in v0.8.0
LoadDir reads all the files recursively under "dir", returning their contents as a map[filename]->contents but without file mode. Returns nil if dir doesn't exist. Keys use slash separators, not native.
func LoadDirMode ¶ added in v0.8.0
func LoadDirMode(tb testing.TB, dir string, opts ...LoadDirOpt) map[string]ModeAndContents
LoadDirMode reads all the files recursively under "dir", returning their contents as a map[filename]->contents. Returns nil if dir doesn't exist. Keys use slash separators, not native.
func Overwrite ¶ added in v0.8.0
Overwrite writes the given contents to the given path, failing the test on error. If the enclosing directory doesn't exist, it will be created.
func OverwriteJoin ¶ added in v0.9.3
OverwriteJoin writes the given contents to the path created by filepath.Join(dir, name), failing the test on error. If the enclosing directory doesn't exist, it will be created.
func Prepend ¶ added in v0.9.1
Prepends adds contents the beginning of the file dir/baseName, failing the test on error.
func TransformStructFields ¶ added in v0.9.2
func TransformStructFields(transform cmp.Option, structExample interface{}, fieldNames ...string) cmp.Option
TransformStructFields is an Option to use with the cmp library that modifies certain struct fields before doing the comparison. The provided transform is provided to all struct fields who field name is in the "fieldNames" list and whose struct type matches "structExample".
Example invocation: trim the string "foo" from the beginning of MyStruct.MyFirstField and MyStruct.MySecondField before doing the comparison.
cmp.Diff(x, y, abctestutil.TransformStructFields( abctestutil.TrimStringPrefixTransformer("foo"), MyStruct{}, "MyFirstField", "MySecondField")
func TrimStringPrefixTransformer ¶ added in v0.9.2
TrimStringPrefixTransformer is a Transformer function to use with the cmp library that removes a prefix from strings before comparing them. See the TransformStructFields documentation for an example.
func WithGitRepoAt ¶
WithGitRepoAt adds "files" to the given map containing a minimal git repo. The prefix will be added to the beginning of each filename (e.g. "subdir/"). Returns the input map for ease of call chaining.
Any keys in the input map will not be overwritten. This allows tests to override certain files, say, .git/refs/main.
This is intended to be used with maps that will eventually be passed to WriteAllDefaultMode().
func WriteAllMode ¶ added in v0.8.0
func WriteAllMode(tb testing.TB, root string, files map[string]ModeAndContents)
WriteAllMode saves the given file contents with the given permissions.
Types ¶
type LoadDirOpt ¶ added in v0.8.0
type LoadDirOpt struct {
// contains filtered or unexported fields
}
LoadDirOpt is a "functional option" for the LoadDir* functions.
func SkipGlob ¶ added in v0.8.0
func SkipGlob(glob string) LoadDirOpt
SkipGlob is a LoadDirOpt that skips all directory entries matching the given glob (and their children, in the case of directories).