elvish: github.com/elves/elvish/pkg/testutil Index | Files

package testutil

import "github.com/elves/elvish/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 Uses

func ApplyDir(dir Dir)

ApplyDir creates the given filesystem layout in the current directory.

func InTempHome Uses

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 Uses

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 Uses

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 Uses

func MustChdir(dir string)

MustChdir calls os.Chdir and panics if it fails.

func MustCreateEmpty Uses

func MustCreateEmpty(names ...string)

Creates an empty file, and panics if an error occurs.

func MustMkdirAll Uses

func MustMkdirAll(names ...string)

Calls os.MkdirAll and panics if an error is returned.

func MustPipe Uses

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

func MustReadAllAndClose Uses

func MustReadAllAndClose(r io.ReadCloser) []byte

func MustWriteFile Uses

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

Calls ioutil.WriteFile and panics if an error occurs.

func ScaledMs Uses

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 Uses

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 Uses

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 Uses

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 Uses

type File struct {
    Perm    os.FileMode
    Content string
}

File describes a file to create.

Package testutil imports 8 packages (graph) and is imported by 3 packages. Updated 2020-12-01. Refresh now. Tools for package owners.