testutil

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2021 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package testutil contains common test utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyDir added in v0.15.0

func ApplyDir(dir Dir)

ApplyDir creates the given filesystem layout in the current directory.

func InTempHome added in v0.15.0

func InTempHome() (string, func())

InTempHome is like InTestDir, but it also sets HOME to the temporary directory and restores the original HOME in cleanup.

func InTestDir added in v0.15.0

func InTestDir() (string, func())

InTestDir is like TestDir, but also changes into the test directory, and the cleanup function also changes back to the original working directory.

It panics if it could not get the working directory or change directory. It is only suitable for use in tests.

func Must added in v0.15.0

func Must(err error)

Must panics if the error value is not nil. It is typically used like this:

testutil.Must(a_function())

Where `a_function` returns a single error value. This is useful with functions like os.Mkdir to succinctly ensure the test fails to proceed if a "can't happen" failure does, in fact, happen.

func MustChdir added in v0.15.0

func MustChdir(dir string)

MustChdir calls os.Chdir and panics if it fails.

func MustCreateEmpty added in v0.15.0

func MustCreateEmpty(names ...string)

MustCreateEmpty creates an empty file, and panics if an error occurs.

func MustMkdirAll added in v0.15.0

func MustMkdirAll(names ...string)

MustMkdirAll calls os.MkdirAll and panics if an error is returned.

func MustPipe added in v0.15.0

func MustPipe() (*os.File, *os.File)

func MustReadAllAndClose added in v0.15.0

func MustReadAllAndClose(r io.ReadCloser) []byte

func MustWriteFile added in v0.15.0

func MustWriteFile(filename string, data []byte, perm os.FileMode)

MustWriteFile calls ioutil.WriteFile and panics if an error occurs.

func ScaledMs

func ScaledMs(ms int) time.Duration

ScaledMs returns ms milliseconds, scaled by the ELVISH_TEST_TIME_SCALE environment variable. If the variable does not exist, the scale defaults to 1.

func TestDir added in v0.15.0

func TestDir() (string, func())

TestDir creates a temporary directory for testing. It returns the path of the temporary directory and a cleanup function to remove the temporary directory. The path has symlinks resolved with filepath.EvalSymlinks.

It panics if the test directory cannot be created or symlinks cannot be resolved. It is only suitable for use in tests.

func WithTempEnv added in v0.15.0

func WithTempEnv(name, value string) func()

WithTempEnv sets an environment variable to a temporary value, and returns a function for restoring the old value.

Types

type Dir added in v0.15.0

type Dir map[string]interface{}

Dir describes the layout of a directory. The keys of the map represent filenames. Each value is either a string (for the content of a regular file with permission 0644), a File, or a Dir.

type File added in v0.15.0

type File struct {
	Perm    os.FileMode
	Content string
}

File describes a file to create.

type Symlink struct{ Target string }

Symlink defines the target path of a symlink to be created.

Jump to

Keyboard shortcuts

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