tmpdir

package
v1.42.3 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithValue

func WithValue(ctx context.Context, td *TempDir) context.Context

WithValue returns a new context with the given TempDir attached. Use this to inject an existing TempDir into a context (e.g., sharing a TempDir across multiple test contexts).

Types

type TempDir

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

TempDir manages a tree of temporary directories. All files and child directories live under a single root path that can be removed in one shot via Cleanup(). After initialization, the struct has no mutable state — NewChild and NewFile delegate uniqueness to os.MkdirTemp and os.CreateTemp respectively, so no locking is needed on the hot path.

func FromContext

func FromContext(ctx context.Context) *TempDir

FromContext returns the TempDir from the context, or nil if none is set.

func FromPath

func FromPath(dir string) *TempDir

FromPath creates a TempDir backed by an existing directory. The caller owns the lifecycle of the directory; Cleanup() is a no-op. This is useful for wrapping a directory from t.TempDir() where the test framework handles cleanup automatically.

func Root

func Root(ctx context.Context, prefix string) (context.Context, *TempDir)

Root creates a new root temp directory with the given prefix and returns a context with the TempDir attached. Callers should defer Cleanup() on the returned TempDir to ensure all temp files are removed.

func (*TempDir) Cleanup

func (t *TempDir) Cleanup() error

Cleanup removes the entire root directory and all contents. Safe to call multiple times.

func (*TempDir) NewChild

func (t *TempDir) NewChild(name string) (string, func(), error)

NewChild creates a named subdirectory under this TempDir. The returned cleanup function removes the subdirectory and all contents; callers should defer it to reclaim space early. The root Cleanup acts as a safety net if the per-child cleanup is missed. The cleanup function is safe to call multiple times and is safe to call after the root has already been cleaned up.

func (*TempDir) NewFile

func (t *TempDir) NewFile(pattern string) (*os.File, func(), error)

NewFile creates a new temp file under this TempDir with the given name pattern (as in os.CreateTemp). The caller is responsible for closing the file. The returned cleanup function removes the file; callers should defer it to reclaim space early. The root Cleanup acts as a safety net if the per-file cleanup is missed. The cleanup function is safe to call multiple times.

Jump to

Keyboard shortcuts

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