testutil

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: MIT Imports: 17 Imported by: 0

README

testutil

godoc.org

Utilities for test of static analysis tools.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter(t analysistest.Testing, filter func(format string, args ...interface{}) bool) analysistest.Testing

Filter calls t.Errorf when the filter returns true.

func ModFile

func ModFile(t *testing.T, path string, fix modfile.VersionFixer) io.Reader

ModFile opens a mod file with the path and fixes versions by the version fixer. If the path is direcotry, ModFile opens go.mod which is under the path.

func ReplaceStderr added in v0.2.0

func ReplaceStderr(onoff bool)

ReplaceStderr sets whether RunWithVersions replace os.Stderr or not. The default value is true which means that RunWithVersions replaces os.Stderr.

func RunWithVersions added in v0.2.0

func RunWithVersions(t *testing.T, dir string, a *analysis.Analyzer, vers []ModuleVersion, pkg string) map[ModuleVersion][]*analysistest.Result

RunWithVersions runs analysistest.Run with modules which version is specified the vers.

Example:

func TestAnalyzer(t *testing.T) {
	vers := AllVersion(t, "github.com/tenntenn/greeting/v2")
	RunWithVersions(t, analysistest.TestData(), sample.Analyzer, vers, "a")
}

The test run in temporary directory which is isolated the dir. analysistest.Run uses packages.Load and it prints errors into os.Stderr. Becase the error messages include the temporary directory path, so RunWithVersions replaces os.Stderr. Replacing os.Stderr is not thread safe. If you want to turn off replacing os.Stderr, you can use ReplaceStderr(false).

func WithModules

func WithModules(t *testing.T, srcdir string, modfile io.Reader) (dir string)

WithModules creates a temp dir which is copied from srcdir and generates vendor directory with go.mod. go.mod can be specified by modfileReader. Example:

func TestAnalyzer(t *testing.T) {
	testdata := testutil.WithModules(t, analysistest.TestData(), nil)
	analysistest.Run(t, testdata, sample.Analyzer, "a")
}

func WriteFiles added in v0.3.1

func WriteFiles(t TestingT, filemap map[string]string) string

WriteFiles wrapper of analysistest.WriteFiles.

WriteFiles is a helper function that creates a temporary directory and populates it with a GOPATH-style project using filemap (which maps file names to contents).

On success it returns the name of the directory. The directory will be deleted by t.Cleanup.

func WriteFilesFS added in v0.4.0

func WriteFilesFS(t TestingT, fsys fs.FS) string

WriteFiles wrapper of analysistest.WriteFiles.

WriteFiles is a helper function that creates a temporary directory and populates it with a GOPATH-style project using fs.FS.

On success it returns the name of the directory. The directory will be deleted by t.Cleanup.

Types

type ErrorfFunc

type ErrorfFunc func(format string, args ...interface{})

ErrorfFunc implements analysistest.Testing.

func (ErrorfFunc) Errorf

func (f ErrorfFunc) Errorf(format string, args ...interface{})

Errorf implements analysistest.Testing.

type ModuleVersion added in v0.2.0

type ModuleVersion = modver.ModuleVersion

ModuleVersion has module path and its version.

func AllVersion added in v0.2.0

func AllVersion(t *testing.T, module string) []ModuleVersion

AllVersion get available all versions of the module.

func FilterVersion added in v0.2.0

func FilterVersion(t *testing.T, module, constraints string) []ModuleVersion

FilterVersion returns versions of the module which satisfy the constraints such as ">= v2.0.0" The constraints rule uses github.com/hashicorp/go-version.

Example:

func TestAnalyzer(t *testing.T) {
	vers := FilterVersion(t, "github.com/tenntenn/greeting/v2", ">= v2.0.0")
	RunWithVersions(t, analysistest.TestData(), sample.Analyzer, vers, "a")
}

func LatestVersion added in v0.3.0

func LatestVersion(t *testing.T, module string, max int) []ModuleVersion

LatestVersion returns most latest versions (<= max) of each minner version.

Example:

func TestAnalyzer(t *testing.T) {
	vers := LatestVersion(t, "github.com/tenntenn/greeting/v2", 3)
	RunWithVersions(t, analysistest.TestData(), sample.Analyzer, vers, "a")
}

type TestingT added in v0.4.0

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

Jump to

Keyboard shortcuts

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