util

package
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MPL-2.0 Imports: 3 Imported by: 0

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 T

type T interface {
	TempDir() string
	Helper()
	Errorf(format string, args ...any)
	Fatalf(format string, args ...any)
	Cleanup(func())
}

type TempFileSetting

type TempFileSetting func(s *TempFileSettings)

func Bytes

func Bytes(data []byte) TempFileSetting

Bytes writes data to the temporary file.

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 Mode

func Mode(mode fs.FileMode) TempFileSetting

Mode sets the temporary file's mode.

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.

func String

func String(data string) TempFileSetting

String writes data to the temporary file.

type TempFileSettings

type TempFileSettings struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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