bazel-contrib / bazel-contrib/rules_go
How to test an analysis.Analyzer?
- Dominant language
- Go
- Stars
- 1.5k
- Forks
- 760
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 12
Description
### What version of rules_go are you using?
0.47.0
### What version of gazelle are you using?
0.36.0
### What version of Bazel are you using?
7.1.1
### Does this issue reproduce with the latest releases of all the above?
Yes
### What operating system and processor architecture are you using?
macOS arm64
### Any other potentially useful information about your toolchain?
N/A
### What did you do?
I want to test a locally created analyzer using Go sourcecode in a testdata directory.
**Test file**
```go
func TestAll(t *testing.T) {
pkgsDriver, err := runfiles.Rlocation("io_bazel_rules_go/go/tools/go_bin_runner/bin/go")
require.NoError(t, err)
err = os.Setenv("GOPACKAGESDRIVER", "off")
require.NoError(t, err)
err = os.Setenv("PATH", os.Getenv("PATH")+":"+filepath.Dir(pkgsDriver))
require.NoError(t, err)
testDataDir, err := runfiles.Rlocation("simc/build/go/lint/channel_suffix/testdata")
require.NoError(t, err)
analysistest.Run(t, testDataDir, Analyzer)
}
```
### What did you expect to see?
A passing test.
### What did you see instead?
```
go tool not available: 'go env GOROOT' does not match runtime.GOROOT:
```
I've tried a few alternatives.
1. Running the analysis test directly
```go
func TestAll(t *testing.T) {
analysistest.Run(t, analysistest.TestData(), Analyzer)
}
// ERROR: go tool not available: exec: "go": executable file not found in $PATH
```
2. Use the gopackagesdriver
```go
func TestAll(t *testing.T) {
pkgsDriver, err := runfiles.Rlocation("io_bazel_rules_go/go/tools/gopackagesdriver/gopackagesdriver_/gopackagesdriver")
require.NoError(t, err)
err = os.Setenv("GOPACKAGESDRIVER", pkgsDriver)
require.NoError(t, err)
testDataDir, err := runfiles.Rlocation("simc/build/go/lint/channel_suffix/testdata")
require.NoError(t, err)
analysistest.Run(t, testDataDir, Analyzer)
}
// ERROR: analysistest.go:344: loading []: unexpected end of JSON input
```
Contributor guide
Research direction
Start with the provided TestAll code and the testdata directory, then compare the analysistest.Run calls and the GOPACKAGESDRIVER setup described in the issue. Trace the reported Go tool and unexpected JSON-loading errors to determine which test setup is supported. Done means a locally created Analyzer test runs successfully and passes against the testdata.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- build-system, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100