tools: golang.org/x/tools/go/analysis/analysistest Index | Files

package analysistest

import "golang.org/x/tools/go/analysis/analysistest"

Package analysistest provides utilities for testing analyzers.

Index

Package Files

analysistest.go

Variables

var TestData = func() string {
    testdata, err := filepath.Abs("testdata")
    if err != nil {
        log.Fatal(err)
    }
    return testdata
}

TestData returns the effective filename of the program's "testdata" directory. This function may be overridden by projects using an alternative build system (such as Blaze) that does not run a test in its package directory.

func WriteFiles Uses

func WriteFiles(filemap map[string]string) (dir string, cleanup func(), err error)

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 and a cleanup function to delete it.

type Result Uses

type Result = checker.TestAnalyzerResult

A Result holds the result of applying an analyzer to a package.

func Run Uses

func Run(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Result

Run applies an analysis to the packages denoted by the "go list" patterns.

It loads the packages from the specified GOPATH-style project directory using golang.org/x/tools/go/packages, runs the analysis on them, and checks that each analysis emits the expected diagnostics and facts specified by the contents of '// want ...' comments in the package's source files.

An expectation of a Diagnostic is specified by a string literal containing a regular expression that must match the diagnostic message. For example:

fmt.Printf("%s", 1) // want `cannot provide int 1 to %s`

An expectation of a Fact associated with an object is specified by 'name:"pattern"', where name is the name of the object, which must be declared on the same line as the comment, and pattern is a regular expression that must match the string representation of the fact, fmt.Sprint(fact). For example:

func panicf(format string, args interface{}) { // want panicf:"printfWrapper"

Package facts are specified by the name "package" and appear on line 1 of the first source file of the package.

A single 'want' comment may contain a mixture of diagnostic and fact expectations, including multiple facts about the same object:

// want "diag" "diag2" x:"fact1" x:"fact2" y:"fact3"

Unexpected diagnostics and facts, and unmatched expectations, are reported as errors to the Testing.

Run reports an error to the Testing if loading or analysis failed. Run also returns a Result for each package for which analysis was attempted, even if unsuccessful. It is safe for a test to ignore all the results, but a test may use it to perform additional checks.

type Testing Uses

type Testing interface {
    Errorf(format string, args ...interface{})
}

Testing is an abstraction of a *testing.T.

Package analysistest imports 15 packages (graph) and is imported by 1 packages. Updated 2019-04-25. Refresh now. Tools for package owners.