src.elv.sh/pkg/testutil Index | Files

package testutil

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

Package testutil contains common test utilities.

Index

Package Files

must.go recover.go scaled.go temp_env.go testdir.go testutil.go umask.go umask_unix.go

func ApplyDir

func ApplyDir(dir Dir)

ApplyDir creates the given filesystem layout in the current directory.

func Chdir

func Chdir(c Cleanuper, dir string) string

Chdir changes into a directory, and restores the original working directory when a test finishes. It returns the directory for easier chaining.

func InTempDir

func InTempDir(c Cleanuper) string

InTempDir is equivalent to Chdir(c, TempDir(c)).

func InTempHome

func InTempHome(c Cleanuper) string

InTempHome is equivalent to Setenv(c, env.HOME, InTempDir(c))

func Must

func Must(err error)

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

testutil.Must(someFunction(...))

Where someFunction returns a single error value. This is useful with functions like os.Mkdir to succinctly ensure the test fails to proceed if an operation required for the test setup results in an error.

func MustChdir

func MustChdir(dir string)

MustChdir calls os.Chdir and panics if it fails.

func MustCreateEmpty

func MustCreateEmpty(names ...string)

MustCreateEmpty creates empty file, after creating all ancestor directories that don't exist. It panics if an error occurs.

func MustMkdirAll

func MustMkdirAll(names ...string)

MustMkdirAll calls os.MkdirAll for each argument. It panics if an error occurs.

func MustPipe

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

MustPipe calls os.Pipe. It panics if an error occurs.

func MustReadAllAndClose

func MustReadAllAndClose(r io.ReadCloser) []byte

MustReadAllAndClose reads all bytes and closes the ReadCloser. It panics if an error occurs.

func MustWriteFile

func MustWriteFile(filename, data string)

MustWriteFile writes data to a file, after creating all ancestor directories that don't exist. It panics if an error occurs.

func Recover

func Recover(f func()) (r interface{})

func SaveEnv

func SaveEnv(c Cleanuper, name string)

SaveEnv saves the current value of an environment variable so that it will be restored after a test has finished.

func Scaled

func Scaled(d time.Duration) time.Duration

Scaled returns d scaled by $E:ELVISH_TEST_TIME_SCALE. If the environment variable does not exist or contains an invalid value, the scale defaults to 1.

func Setenv

func Setenv(c Cleanuper, name, value string) string

Setenv sets the value of an environment variable for the duration of a test. It returns value.

func TempDir

func TempDir(c Cleanuper) string

TempDir creates a temporary directory for testing that will be removed after the test finishes. It is different from testing.TB.TempDir in that it resolves symlinks in the path of the directory.

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

func TempHome

func TempHome(c Cleanuper) string

TempHome is equivalent to Setenv(c, env.HOME, TempDir(c))

func Umask

func Umask(c Cleanuper, m int)

Umask sets the umask for the duration of the test, and restores it afterwards.

func Unsetenv

func Unsetenv(c Cleanuper, name string)

Setenv unsets an environment variable for the duration of a test.

type Cleanuper

type Cleanuper interface {
    Cleanup(func())
}

Cleanuper wraps the Cleanup method. It is a subset of testing.TB, thus satisfied by *testing.T and *testing.B.

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.

Package testutil imports 8 packages (graph).

Version v0.17.0 (latest) | Published Dec 7, 2021 | Platform: linux/amd64 | Updated 16 hours ago

.
Tools for package owners.