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: Functionality for verifying SuggestedFixes and TextEdits #38044

Closed
bflad opened this issue Mar 24, 2020 · 4 comments · Fixed by bflad/tfproviderlint#165
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@bflad
Copy link

bflad commented Mar 24, 2020

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

$ go version
go version go1.14 darwin/amd64

Does this issue reproduce with the latest release?

Yes, but this is also a feature request. 😄

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bflad/Library/Caches/go-build"
GOENV="/Users/bflad/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/bflad/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/bflad/src/github.com/terraform-providers/terraform-provider-aws/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/w8/05f3x02n27x72g0mc2jy6_180000gp/T/go-build629459130=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Attempting to test an *analysis.Analyzer that reports a Diagnostic with SuggestedFixes and TextEdits, e.g.

			pass.Report(analysis.Diagnostic{
				Pos:     selectorExpr.Pos(),
				End:     selectorExpr.End(),
				Message: fmt.Sprintf("%s: deprecated %s should be replaced with %s", analyzerName, selectorExprBuf.String(), newSelectorExprBuf.String()),
				SuggestedFixes: []analysis.SuggestedFix{
					{
						Message: "Replace",
						TextEdits: []analysis.TextEdit{
							{
								Pos:     selectorExpr.Pos(),
								End:     selectorExpr.End(),
								NewText: newSelectorExprBuf.Bytes(),
							},
						},
					},
				},
			})

What did you expect to see?

The ability or the framework to verify potential code changes via TextEdits.

What did you see instead?

Seems like there is not a native or built-in way to test these. I wrote together a proof of concept implementation that does the following:

  • Takes a source directory (e.g. testdata/src/a) and copies to a temporary directory
  • Wraps the Analyzer in a singlechecker.Main and re-executes the test with the -fix flag
  • Compares expected directory (e.g. testdata/src/a_fixed) file contents with "fixed" temporary directory file contents

Full code: https://github.com/bflad/tfproviderlint/blob/master/helper/analysisfixtest/analysisfixtest.go

Testing implementation matches analysistest.Run:

func TestAnalyzerFixes(t *testing.T) {
	testdata := analysistest.TestData()
	analysisfixtest.Run(t, testdata, V008.Analyzer, "a")
}

Since the -fix flag is only handled by the multichecker/singlechecker drivers, this would be improved with #31897 (x/tools/go/analysis: provide driver for running analysis programmatically), rather than wrapping the Analyzer in a self-executing a child process.

It would be wonderful if functionality like this could be baked into go/analysis though! Thank you for your consideration.

@gopherbot gopherbot added this to the Unreleased milestone Mar 24, 2020
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Mar 24, 2020
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 24, 2020
@dominikh
Copy link
Member

Does golang/tools@b1df990 adequately address this?

/cc @matloob

@matloob
Copy link
Contributor

matloob commented Apr 13, 2020

I think it does... @bflad?

@bflad
Copy link
Author

bflad commented Apr 14, 2020

This looks fantastic at first glance -- double checking in my codebase and will close this out if all looks good there too. Thanks!

bflad added a commit to bflad/tfproviderlint that referenced this issue Apr 14, 2020
bflad added a commit to bflad/tfproviderlint that referenced this issue Apr 14, 2020
…xes (#165)

* Switch from helper/analysisfixtest to analysistest.RunWithSuggestedFixes

Reference: golang/go#38044

* deps: Remove github.com/sergi/go-diff

No longer needed with removal of helper/analysisfixtest.
@bflad
Copy link
Author

bflad commented Apr 14, 2020

Works great 🎉 Thanks so much!

@golang golang locked and limited conversation to collaborators Apr 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants