-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: x/tools/go/analysis/analysistest: improved, module-aware API #61336
Comments
Change https://go.dev/cl/509395 mentions this issue: |
The loop over
You should comment on why you don't build the load functionality on top of go/packages/packagestest. Perhaps the go tool should ignore txtar files nest if you make them nest. You could say that a line of the form |
Change https://go.dev/cl/577995 mentions this issue: |
Adds a new package testfiles to help write tests around go files and directories. These are especially helpful when dealing with go.mod files. Adds a new CopyTestFiles function that copies a directory and removes the ".test" file extension, e.g. "go.mod.test" becomes "go.mod". This allows for easily creating analysistests with go.mod files. This change also adds a new TestDir helper to extract to a temporary directory. Consolidates txtar usage around ExtractTxtar, which writes a txtar archive to a directory, and adds a convenience helper TestTxtar that extracts a txtar at a path to a temporary directory. Updates golang/go#61336 Updates golang/go#53063 Updates golang/go#46041 Updates golang/go#37054 Change-Id: I09210f751bbc6ac3f01c34fba22b7e8fa1ddf93f Reviewed-on: https://go-review.googlesource.com/c/tools/+/577995 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
@timothy-king's https://go.dev/cl/577995 may be a simpler approach to this problem. |
[Status: rough draft]
[Split out from preceding proposal #61324]
The analysistest package is too monolithic: if you don't like the Run function, you're on your own. Now that (assuming #61324 is accepted), it is possible to call checker.Analyze programmatically, it should be possible to break the task of testing down into three steps--load, analyze, assert--and allow clients to replace the first and/or third steps with their own logic if they prefer.
Also, the existing API requires GOPATH layout, and cannot use modules. (It is tempting to honor a go.mod file within testdata, but this causes the testdata tree to belong to its own separate module, leading to tests that don't work when run from the module cache, which is bad practice.)
We propose to add a new analysistest API--alongside its existing ("legacy") API--as follows:
I have shown the body of the Test function to indicate that it is largely a helper function, a recipe of four steps: (1) expand the file string to a directory tree; (2) load packages from it; (3) analyze; (4) apply assertions based on the source file tree. Users who decide that this function isn't sufficient for their needs can easily inline or fork it; steps 2-4 are all now public functions.
The LegacyResult declaration warrants some explanation. Previously, Result was an alias for
internal/checker.TestAnalyzerResult
, and the creation of the public alias accidentally published the internal type, which has several public fields. Unfortunately we can't retract it, so this proposal merely changes its name to LegacyResult, to emphasize its status. The Result alias is kept for compatibility, but we would prefer not to refer to it by this name as it may be easily confused with the newchecker.Result
type.https://go.dev/cl/509395 contains a prototype of the new API. A few analyzers' tests have been updated to use the new API.
Questions still to resolve:
The text was updated successfully, but these errors were encountered: