-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: support modules #37054
Comments
Just to understand this a bit better, do you mean that the test code in the test tree can depend on an external module and download it? |
I mean the test code behaves as if it is part of the module that contains the analyzer. I'm still getting used to modules, but I envisioned it working one of two ways:
|
Hm, I think Option 1 would create some problems because running the test would modify the module is contained in, and that could be very surprising. Option 2 would be more desirable depending on how it works. |
Hi there! 👋 I'm not sure if this is an intended use case, but we love that we can write It'd be neat if this could use something like Go 1.14's |
I'd welcome a contribution to fix this if the code is simple enough. My preference is that there's no extra configuration other than the module file of the module the tests are contained in. |
Instead of using a real `framework.Context` I had to use a stub struct, see golang/go#37054
Instead of using a real `framework.Context` I had to use a stub struct, see golang/go#37054
I am writing an analyzer that verifies that the correct packages are imported. I have files in
So in my case "analysistest: support modules" means it knowing how to look in the module cache for package sources. |
hey @tomarrell, can you explain how you resolved this by vendoring? tried grokking tomarrell/wrapcheck@5a99e6a but there's a lot there and don't see much mention of go mod. i've run into this issue building analyzers & am not super familiar with vendoring. thanks :) |
@zhammer I'm not @tommarrel but here's how I resolved this: https://github.com/planetscale/go-logkeycheck/tree/main/internal/logkeycheck/testdata I did not vendor the entire set of dependencies into the repo (mostly due to personal preference). Instead, I created a make task ( EDIT: updated and fixed links |
Save my life. thanks a lot. 👍 |
forgot to answer but thank you @joemiller! it’d be great if there were a slack/discord/community for folks writing golang codemods |
Hey, any updates on this? I am writing analyzer that verifies if certain imports are used in a project. Including vendor folder in order to test the analyzer seems messy and I would like to avoid it, especially if I want to push the project to remote repository with CI. |
This comment was marked as spam.
This comment was marked as spam.
I do not believe there have been any updates on the Go team's side since #37054 (comment). A community contribution would still be welcomed. |
Hi - I am proposing a new analyzer test run helper in #53063 - in the issue, my focus was to provide a replacement of For populating test data ( |
For anyone reading this - I hacked it around by using rogpeppe's testscript https://github.com/rogpeppe/go-internal/tree/master/testscript and manually adding the expect on all the lines. It's not as great as using analysistest, but I really want to use real go.mod without stubbing everything and figuring out how GOPATH works exactly again |
Related: #46041 |
Closing in favor of #46041. |
Modules were explicitly disabled in analysistest. This lead to failures in tests where testdata required modules. This change introduces new struct Options that contains configuration for a new func `RunWithOptions`. `RunWithOptions` is called internally by `Run` and `RunWithSuggestedFixes` with modules disabled for backward compatibility. `RunWithOptions` can be run explicitly too, with a configuration allowing usage of modules. Fixes golang/go#46041 Fixes golang/go#37054
Modules were explicitly disabled in analysistest. This lead to failures in tests where testdata required modules. This change introduces new struct Options that contains configuration for a new func `RunWithOptions`. `RunWithOptions` is called internally by `Run` and `RunWithSuggestedFixes` with modules disabled for backward compatibility. `RunWithOptions` can be run explicitly too, with a configuration allowing usage of modules. It also work with Go workspace. Fixes golang/go#46041 Fixes golang/go#37054
Modules were explicitly disabled in analysistest. This lead to failures in tests where testdata required modules. This change introduces new struct Options that contains configuration for a new func `RunWithOptions`. `RunWithOptions` is called internally by `Run` and `RunWithSuggestedFixes` with modules disabled for backward compatibility. `RunWithOptions` can be run explicitly too, with a configuration allowing usage of modules. It also work with Go workspace. Fixes golang/go#46041 Fixes golang/go#37054 Change-Id: I91a9365b46ea44afae79e78333720ea8c46489d7
Change https://go.dev/cl/506275 mentions this issue: |
Change https://go.dev/cl/506355 mentions this issue: |
Change https://go.dev/cl/506356 mentions this issue: |
Making the testdata directory a separate Go module and vendoring dependencies under
Anyway, it seems that a proper solution is incoming, looking forward to it. |
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>
Reference: golang/go#37054 When this Go module was under heavy development, the analysis testing framework in `golang.org/x/tools` only supported `GOPATH` based resolution. Subsequently, dependencies were vendored and a `vendor` symlink was placed in each of the test case directories. With recent versions of `golang.org/x/tools`, placing a `go.mod` in the `testdata` directory will cause the analysis testing framework to enable Go module support. Only minor import and pathing changes are needed to convert over. Consumers will no longer be burdened with Go module dependencies only needed for testing this Go module.
Reference: golang/go#37054 When this Go module was under heavy development, the analysis testing framework in `golang.org/x/tools` only supported `GOPATH` based resolution. Subsequently, dependencies were vendored and a `vendor` symlink was placed in each of the test case directories. With recent versions of `golang.org/x/tools`, placing a `go.mod` in the `testdata` directory will cause the analysis testing framework to enable Go module support. Only minor import and pathing changes are needed to convert over. Consumers will no longer be burdened with Go module dependencies only needed for testing this Go module.
Currently the analysistest helper only supports a GOPATH like tree. This is fine for stand alone analyses, but does not help much for analyses designed to run against a particular module. For example, you have an analysis that checks all code in your module correctly/safely interacts with a specific package in your module. Ideally you would be able to write an analysis test with a stub file that is able to load other packages/dependencies of the current module.
/cc @matloob @heschik
The text was updated successfully, but these errors were encountered: