testutil

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 17 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 DialogTest added in v0.8.0

func DialogTest(ctx context.Context, tb testing.TB, steps []DialogStep, cmd cli.Command, runArgs []string) error

DialogTest is a helper for running tests against a CLI command that involve communicating over stdin and stdout. The expected conversation is defined as a sequence of DialogSteps.

If the observed dialog doesn't match the expected dialog, or if the test times out, then tb.Fatalf() will be called. In either of these cases, a goroutine could be leaked, but we consider that OK, because this is just a test.

cmd.Run() will be called with runArgs. If Run() returns an error, that error will be returned from this function. That is the only error that will ever be returned by this function.

If *both* (1) cmd.Run() returns error and (2) the observed dialog doesn't match the expected dialog, then tb.Fatalf() will be called (so no error will be returned. This allows the dialog to be verified even for cases that return error.

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 MustLoadManifest added in v0.8.0

func MustLoadManifest(ctx context.Context, tb testing.TB, path string) *manifest.Manifest

MustLoadManifest parses the given manifest file.

func Overwrite added in v0.8.0

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

Overwrite writes the given contents to dir/baseName, failing the test on error.

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 ReadWithTimeout

func ReadWithTimeout(tb testing.TB, r io.Reader, wantSubstr string)

ReadWithTimeout does a single read from the given reader. It calls Fatal if that read fails or the returned string doesn't contain wantSubStr. May leak a goroutine on timeout.

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

func WriteWithTimeout

func WriteWithTimeout(tb testing.TB, w io.Writer, msg string)

WriteWithTimeout does a single write to the given writer. It calls Fatal if that read doesn't contain wantSubStr. May leak a goroutine on timeout.

Types

type DialogStep

type DialogStep struct {
	WaitForPrompt string
	ThenRespond   string // should end with newline
}

DialogStep describes the prompt and respond msg.

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

Jump to

Keyboard shortcuts

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