Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/go/analysis/analysistest: Run will pass generated test files to analyzer #41771

Closed
bombsimon opened this issue Oct 3, 2020 · 1 comment
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@bombsimon
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.15 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/simon.sawert/Library/Caches/go-build"
GOENV="/Users/simon.sawert/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/simon.sawert/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/simon.sawert/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/simon.sawert/git/analyzer-mre/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dy/tkxx13zx3n1d_m28k_3lh6w00000gr/T/go-build111181793=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I created testdata including a test file (a file suffixed _test.go) and then I created a test and used analysistest.Run to test this code.

What did you expect to see?

Only my files from the testdata to be passed to the analyzer run function.

What did you see instead?

The generated test file(s) in the build cache gets passed.


The problem for me is that the analyzer I've created adds a diagnostics report on the test code generated in cache and thus I cannot get my test to pass. I guess this might be related to #40574.

Here's an minimal reproducible example:

% tree
.
├── amre.go
├── amre_test.go
└── testdata
    └── src
        └── mypackage
            ├── mypackage.go
            └── mypackage_test.go

3 directories, 6 files
package amre

import (
    "fmt"

    "golang.org/x/tools/go/analysis"
)

var Analyzer = &analysis.Analyzer{
    Name: "amre",
    Run:  run,
}

func run(pass *analysis.Pass) (interface{}, error) {
    for _, file := range pass.Files {
        filename := pass.Fset.Position(file.Pos()).Filename
        fmt.Println("testing", filename)
    }

    return nil, nil
}
package amre

import (
    "testing"

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

func TestA(t *testing.T) {
    testdata := analysistest.TestData()

    analysistest.Run(t, testdata, Analyzer, "mypackage")
}
% go test ./... -v
go test ./... -v
=== RUN   TestA
testing /Users/simon.sawert/Library/Caches/go-build/00/0090ea545eb3074c3efc79841f1af52ecd6009c127392415405c05125895f58a-d
testing /Users/simon.sawert/git/analyzer-mre/testdata/src/mypackage/mypackage.go
testing /Users/simon.sawert/git/analyzer-mre/testdata/src/mypackage/mypackage.go
testing /Users/simon.sawert/git/analyzer-mre/testdata/src/mypackage/mypackage_test.go
--- PASS: TestA (0.96s)
PASS
ok      analyzer-mre    1.258s
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Oct 3, 2020
@gopherbot gopherbot added this to the Unreleased milestone Oct 3, 2020
@bombsimon
Copy link
Author

Oh, this isn't related to the analysistest in any way, it's the analyser itself! Running the same code on a package containing tests will have the same result. Closing this and will try to figure out how I can avoid these getting passed to the analyser completely.

@golang golang locked and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

2 participants