package testutil

import "src.elv.sh/pkg/testutil"

Package testutil contains common test utilities.

Index

Package Files

must.go scaled_ms.go temp_env.go testdir.go testutil.go

func ApplyDir

func ApplyDir(dir Dir)

ApplyDir creates the given filesystem layout in the current directory.

func InTempHome

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

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

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

func MustChdir(dir string)

MustChdir calls os.Chdir and panics if it fails.

func MustCreateEmpty

func MustCreateEmpty(names ...string)

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

func MustMkdirAll

func MustMkdirAll(names ...string)

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

func MustPipe

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

func MustReadAllAndClose

func MustReadAllAndClose(r io.ReadCloser) []byte

func MustWriteFile

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

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

func WithTempEnv(name, value string) func()

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

type Dir

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

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.

Package testutil imports 8 packages (graph) and is imported by 3 packages. Updated 1 month ago.

.
Tools for package owners.