Documentation
¶
Overview ¶
Package util provides utility functions for writing concise test cases.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TempFile ¶
func TempFile(t T, settings ...TempFileSetting) (path string)
TempFile creates a temporary file that is deleted after the test is completed. If the file cannot be deleted, the test fails with a message containing its path. TempFile creates a new file every time it is called. By default, each file thus created is in a unique directory as created by (*testing.T).TempDir(); this directory is also deleted after the test is completed.
Example ¶
package main
import (
"fmt"
"os"
"testing"
"github.com/shoenig/test/util"
)
var t = new(testing.T)
func main() {
path := util.TempFile(t,
util.String("hello!"),
util.Mode(0o640),
)
b, _ := os.ReadFile(path)
fmt.Println(string(b))
}
Output: hello!
Types ¶
type TempFileSetting ¶
type TempFileSetting func(s *TempFileSettings)
func Dir ¶
func Dir(dir string) TempFileSetting
Dir specifies a directory path to contain the temporary file. If dir is the empty string, the file will be created in the default directory for temporary files, as returned by os.TempDir. A temporary file created in a custom directory will still be deleted after the test runs, though the directory may not.
func Pattern ¶
func Pattern(pattern string) TempFileSetting
Pattern sets the filename to pattern with a random string appended. If pattern contains a '*', the last '*' will be replaced by the random string.
type TempFileSettings ¶
type TempFileSettings struct {
// contains filtered or unexported fields
}