testutil

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: BSD-2-Clause Imports: 13 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

func ApplyDir(dir Dir)

ApplyDir creates the given filesystem layout in the current directory.

func ApplyDirIn added in v0.19.0

func ApplyDirIn(dir Dir, root string)

ApplyDirIn creates the given filesystem layout in a given 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 ChmodOrSkip added in v0.20.0

func ChmodOrSkip(s Skipper, name string, mode fs.FileMode)

ChmodOrSkip runs os.Chmod, but skips the test if file's mode is not exactly mode or if there is any error.

func Dedent added in v0.19.0

func Dedent(text string) string

Dedent removes an optional leading newline, and removes the indentation present in the first line from all subsequent non-empty lines.

Dedent panics if any non-empty line does not start with the same indentation as the first line.

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 Recover

func Recover(f func()) (r any)

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

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 Set added in v0.19.0

func Set[T any](c Cleanuper, p *T, v T)

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 TestTimeScale added in v0.20.0

func TestTimeScale() float64

TestTimeScale parses $E:ELVISH_TEST_TIME_SCALE, defaulting to 1 it it's not set or can't be parsed as a float64.

func Umask added in v0.17.0

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.

Types

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]any

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.

func (Dir) Open added in v0.20.0

func (dir Dir) Open(name string) (fs.File, error)

type FS added in v0.20.0

type FS map[string]File

type File

type File struct {
	Perm    os.FileMode
	Content string
}

File describes a file to create.

type Skipper added in v0.20.0

type Skipper interface {
	Skipf(format string, args ...any)
}

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

Jump to

Keyboard shortcuts

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