testutil

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package testutil contains util functions to facilitate tests.

Package testutil contains util functions to facilitate tests.

Index

Constants

This section is empty.

Variables

View Source
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

func LoadDir(tb testing.TB, dir string, opts ...LoadDirOpt) map[string]string

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

func Overwrite(tb testing.TB, path, contents string)

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

func OverwriteJoin(tb testing.TB, dir, name, contents string)

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

func Prepend(tb testing.TB, dir, baseName, contents string)

Prepends adds contents the beginning of the file dir/baseName, failing the test on error.

func Remove added in v0.9.1

func Remove(tb testing.TB, dir, baseName string)

func TestMustGlob

func TestMustGlob(tb testing.TB, glob string) (string, bool)

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

func TrimStringPrefixTransformer(prefix string) cmp.Option

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

func WithGitRepoAt(prefix string, m map[string]string) map[string]string

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 WriteAll

func WriteAll(tb testing.TB, root string, files map[string]string)

WriteAll wraps writeAll and sets file permissions to 0600.

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).

type ModeAndContents

type ModeAndContents struct {
	Mode     os.FileMode
	Contents string
}

Directories

Path Synopsis
Package prompt allows testing of "prompting" that simulates asking the user for input using stdin and stdout.
Package prompt allows testing of "prompting" that simulates asking the user for input using stdin and stdout.

Jump to

Keyboard shortcuts

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