Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
FromContext returns the TempDir from the context, or nil if none is set.
func FromPath ¶
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 ¶
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 ¶
Cleanup removes the entire root directory and all contents. Safe to call multiple times.
func (*TempDir) NewChild ¶
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 ¶
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.