toolsgolang.org/x/tools/internal/testfiles Index | Files

package testfiles

import "golang.org/x/tools/internal/testfiles"

Package testfiles provides utilities for writing Go tests with files in testdata.

Index

Functions

func CopyToTmp

func CopyToTmp(t testing.TB, src fs.FS, rename ...string) string

CopyToTmp copies the files and directories in src to a new temporary testing directory dst, and returns dst on success.

After copying the files, it processes each of the 'old,new,' rename directives in order. Each rename directive moves the relative path "old" to the relative path "new" within the directory.

Renaming allows tests to hide files whose names have special meaning, such as "go.mod" files or "testdata" directories from the go command, or ill-formed Go source files from gofmt.

For example if we copy the directory testdata:

testdata/
    go.mod.test
    a/a.go
    b/b.go

with the rename "go.mod.test,go.mod", the resulting files will be:

dst/
    go.mod
    a/a.go
    b/b.go

func ExtractTxtarFileToTmp

func ExtractTxtarFileToTmp(t testing.TB, file string) string

ExtractTxtarFileToTmp read a txtar archive on a given path, extracts it to a temporary directory, and returns the temporary directory.

func LoadPackages

func LoadPackages(t testing.TB, ar *txtar.Archive, patterns ...string) []*packages.Package

LoadPackages loads typed syntax for all packages that match the patterns, interpreted relative to the archive root.

The packages must be error-free.

Source Files

testfiles.go

Version
v0.30.0 (latest)
Published
Feb 10, 2025
Platform
linux/amd64
Imports
9 packages
Last checked
8 hours ago

Tools for package owners.