package testutil
import "src.elv.sh/pkg/testutil"
Package testutil contains common test utilities.
Index ¶
- func ApplyDir(dir Dir)
- func InTempHome() (string, func())
- func InTestDir() (string, func())
- func Must(err error)
- func MustChdir(dir string)
- func MustCreateEmpty(names ...string)
- func MustMkdirAll(names ...string)
- func MustPipe() (*os.File, *os.File)
- func MustReadAllAndClose(r io.ReadCloser) []byte
- func MustWriteFile(filename string, data []byte, perm os.FileMode)
- func ScaledMs(ms int) time.Duration
- func TestDir() (string, func())
- func WithTempEnv(name, value string) func()
- type Dir
- type File
- type Symlink
Package Files ¶
must.go scaled_ms.go temp_env.go testdir.go testutil.go
func ApplyDir ¶
ApplyDir creates the given filesystem layout in the current directory.
func InTempHome ¶
InTempHome is like InTestDir, but it also sets HOME to the temporary directory and restores the original HOME in cleanup.
func InTestDir ¶
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 ¶
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 ¶
MustChdir calls os.Chdir and panics if it fails.
func MustCreateEmpty ¶
MustCreateEmpty creates an empty file, and panics if an error occurs.
func MustMkdirAll ¶
MustMkdirAll calls os.MkdirAll and panics if an error is returned.
func MustPipe ¶
func MustReadAllAndClose ¶
func MustReadAllAndClose(r io.ReadCloser) []byte
func MustWriteFile ¶
MustWriteFile calls ioutil.WriteFile and panics if an error occurs.
func ScaledMs ¶
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 ¶
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 ¶
WithTempEnv sets an environment variable to a temporary value, and returns a function for restoring the old value.
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 File ¶
File describes a file to create.
type Symlink ¶
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.