Documentation
¶
Index ¶
- func Filter(t analysistest.Testing, filter func(format string, args ...interface{}) bool) analysistest.Testing
- func ModFile(t *testing.T, path string, fix modfile.VersionFixer) io.Reader
- func ReplaceStderr(onoff bool)
- func RunWithVersions(t *testing.T, dir string, a *analysis.Analyzer, vers []ModuleVersion, ...) map[ModuleVersion][]*analysistest.Result
- func WithModules(t *testing.T, testdata string, gomodfile io.Reader) (dir string)
- func WriteFiles(t TestingT, filemap map[string]string) string
- func WriteFilesFS(t TestingT, fsys fs.FS) string
- type ErrorfFunc
- type ModuleVersion
- type TestingT
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 ¶
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 ¶
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
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
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") }