Documentation
¶
Overview ¶
Package testutil contains common test utilities.
Index ¶
- func ApplyDir(dir Dir)
- func ApplyDirIn(dir Dir, root string)
- func Chdir(c Cleanuper, dir string) string
- func ChmodOrSkip(s Skipper, name string, mode fs.FileMode)
- func Dedent(text string) string
- func InTempDir(c Cleanuper) string
- func InTempHome(c Cleanuper) string
- func Recover(f func()) (r any)
- func SaveEnv(c Cleanuper, name string)
- func Scaled(d time.Duration) time.Duration
- func Set[T any](c Cleanuper, p *T, v T)
- func Setenv(c Cleanuper, name, value string) string
- func TempDir(c Cleanuper) string
- func TempHome(c Cleanuper) string
- func TestTimeScale() float64
- func Umask(c Cleanuper, m int)
- func Unsetenv(c Cleanuper, name string)
- type Cleanuper
- type Dir
- type FS
- type File
- type Skipper
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
ApplyDirIn creates the given filesystem layout in a given directory.
func Chdir ¶
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
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
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 InTempHome ¶
InTempHome is equivalent to Setenv(c, env.HOME, InTempDir(c))
func SaveEnv ¶
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
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 ¶
Setenv sets the value of an environment variable for the duration of a test. It returns value.
func TempDir ¶
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 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.
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 ¶
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 Skipper ¶ added in v0.20.0
Skipper wraps the Skipf method. It is a subset of testing.TB, thus satisfied by *testing.T and *testing.B.