-
Notifications
You must be signed in to change notification settings - Fork 782
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
tools: migrate test generation features to gopls
#1594
Comments
@stamblerre I would like to work on this if no one's working. |
Sounds good! /cc @hyangah to confirm that this would be a good thing to work on |
Please correct me if I'm wrong, to generate tests vscode-go calls gotests lib internally as a child process. However, gotests doesn't return any error if test(s) don't get generated other than "No tests were generated for...". The Probably modifying gotests to return an error for existing tests would be a better idea? |
@n1lesh: Thanks for digging into the underlying cause! An alternative option may actually be to move the test generation logic into |
@stamblerre I can work on the |
gopls
The Gopls will handle the add unit tests request following steps below:
![]()
![]()
NOTE:
|
Cool! A few notes:
Let's just say "Add unit test for X". No need to prefix the code action with "Source:", since it is in the "Source Action" section.
s/grpc/rpc. Also, the command does not return a workspace edit. Rather it calls the Test function naming LGTM. It may be simpler to start with just "Add unit test for function". |
Excited to use this! A couple of suggestions:
|
Cool! Should probably be "Add a test for Bar", but we can discuss that on the CL. This will be great. |
Change https://go.dev/cl/621056 mentions this issue: |
Change https://go.dev/cl/621096 mentions this issue: |
Next CL: Following the existing implementation from For embedding type, we will choose the type name as fields name. E.g. Field
The end goal of the test looks below. (This is generate by the
|
Change https://go.dev/cl/621675 mentions this issue: |
To update this issue with some decisions:
|
Change https://go.dev/cl/622320 mentions this issue: |
This CL is some glue code which build the connection between the LSP "code action request" with second call which compute the actual DocumentChange. AddTest source code action will create a test file if not already exist and insert a random function at the end of the test file. For testing, an internal boolean option "addTestSourceCodeAction" is created and only effective if set explicitly in marker test. For golang/vscode-go#1594 Change-Id: Ie3d9279ea2858805254181608a0d5103afd3a4c6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/621056 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Change https://go.dev/cl/623997 mentions this issue: |
Change https://go.dev/cl/621816 mentions this issue: |
This CL interpret the user's intention by reading the x_test.go's package name. If the x_test.go exist and is using package name x, we allow adding test for unexported function/method. If the x_test.go does not exist or it exist but using package name x_test, we will only allow adding test for exported function/methods. For golang/vscode-go#1594 Change-Id: I20f6c41dc4c53bb816b40982a0ebbbcb1e3a92e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/621675 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Add test for method is not fully implemented yet. For golang/vscode-go#1594 Change-Id: I4e18183baf96242c209e31e02a1b5cd642844c1d Reviewed-on: https://go-review.googlesource.com/c/tools/+/621057 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
This commit improves qualifier by consolidating imports from both the main file (x.go) and its corresponding test file (x_test.go). An imports map is used to track all import paths and their local renames. Imports from x_test.go are prioritized over x.go as gopls is generating test in x_test.go. This ensures that the generated qualifier correctly reflects any necessary renames, improving accuracy and consistency. For golang/vscode-go#1594 Change-Id: I457d5f22f7de4fe86006b57487f243494c8e7f6f Reviewed-on: https://go-review.googlesource.com/c/tools/+/622320 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]>
This commit improves the handling of test cases for functions that return an error as their last return value. - A boolean field called "wantErr" is added to the test case struct to indicate whether an error is expected. - The error return value is now handled before comparing other return values, ensuring correct error handling in test cases. For golang/vscode-go#1594 Change-Id: Ib48f6b85b2fdad96cb7fb563b8bdbf17d692f569 Reviewed-on: https://go-review.googlesource.com/c/tools/+/623997 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Test case names displayed in subtest names instead of test failure messages. For golang/vscode-go#1594 Change-Id: Iab207cd9af0af99a036dc69a8b2230f12bf876fb Reviewed-on: https://go-review.googlesource.com/c/tools/+/621816 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Change https://go.dev/cl/620696 mentions this issue: |
Change https://go.dev/cl/620697 mentions this issue: |
Gopls loops over all the function and find the ones qualifies based on its signature. - When a qualifying constructor is found, the test skeleton uses the constructor to initialize the receiver and call the method. - When no constructor is found, gopls generates a test skeleton with a variable declaration using the receiver type (e.g., "var t T") and includes a TODO comment to remind users to implement receiver variable initialization. For golang/vscode-go#1594 Change-Id: I2a703bbd099f03fd1bf85e516f86484805b4a0ae Reviewed-on: https://go-review.googlesource.com/c/tools/+/620696 Auto-Submit: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Change https://go.dev/cl/626537 mentions this issue: |
Change https://go.dev/cl/627355 mentions this issue: |
- Gopls will honor any renaming of package "testing" if any. - Gopls will collect all the package that have not been imported in foo_test.go and modify the foo_test.go imports. For golang/vscode-go#1594 Change-Id: Id6b87b6417a26f8e925582317e91fb4ebff4a0e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/620697 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Unnamed/blank parameters are now assigned zero values, as they are not referenced within the function body. - The `zeroString` function has been moved from `go/ssa` to `internal/typesinternal` for better organization. - Honor the input parameter name from the function signature. - Input parameters from both consutrctor and target functions are flattened into the test case struct. Potential field name duplication is handled by introducing prefixes. For golang/vscode-go#1594 Change-Id: I8b56d8f3e0f0432d4f9fe269cc7ba86ea46decfc Reviewed-on: https://go-review.googlesource.com/c/tools/+/626537 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Read and determine whether the first input parameter of function or constructor is a context.Context through package name and name comparison. - Call the function or constructor with context.Background() but honor is there is any renaming in foo.go or foo_test.go. - Fix the issue where the constructor param is added to function or method call. For golang/vscode-go#1594 Change-Id: Ic1d145e65bc4b7cb34f637bab8ebdeccd36a33f9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/627355 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Change https://go.dev/cl/629756 mentions this issue: |
Change https://go.dev/cl/630119 mentions this issue: |
- This commit updates addTest and extractToFile to preserve copyright and build constraint comments when creating new files. - The change introduces utility functions to extract these comments from an ast.File. For golang/vscode-go#1594 Change-Id: I2b2f70d6d4de662c8357bca8558c094496c8b2e9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/629756 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Change https://go.dev/cl/629978 mentions this issue: |
This commit refines the logic for determining the package name of new test files in gopls. Previously, when creating a new test file, gopls blindly choose x_test package. This commit expands the logic to consider the target function's signature. For golang/vscode-go#1594 Change-Id: Ia78003bf007479e48861ce643c8c7c366ff960a3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/629978 Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Add feature documentation and demo screenshot. - Remove the internal feature gates guarding add test feature. - Replace spaces with tabs in test function body template. - Run format for generated test function body. For golang/vscode-go#1594 Change-Id: Id2106654c24da61ad6aa31723908e4931aa54430 Reviewed-on: https://go-review.googlesource.com/c/tools/+/630119 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
Change https://go.dev/cl/632857 mentions this issue: |
For golang/vscode-go#1594 Change-Id: I8563e4cde1606a23a402bc2154b1af69cb9ff724 Reviewed-on: https://go-review.googlesource.com/c/tools/+/632857 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Gopls loops over all the function and find the ones qualifies based on its signature. - When a qualifying constructor is found, the test skeleton uses the constructor to initialize the receiver and call the method. - When no constructor is found, gopls generates a test skeleton with a variable declaration using the receiver type (e.g., "var t T") and includes a TODO comment to remind users to implement receiver variable initialization. For golang/vscode-go#1594 Change-Id: I2a703bbd099f03fd1bf85e516f86484805b4a0ae Reviewed-on: https://go-review.googlesource.com/c/tools/+/620696 Auto-Submit: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
- Gopls will honor any renaming of package "testing" if any. - Gopls will collect all the package that have not been imported in foo_test.go and modify the foo_test.go imports. For golang/vscode-go#1594 Change-Id: Id6b87b6417a26f8e925582317e91fb4ebff4a0e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/620697 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Unnamed/blank parameters are now assigned zero values, as they are not referenced within the function body. - The `zeroString` function has been moved from `go/ssa` to `internal/typesinternal` for better organization. - Honor the input parameter name from the function signature. - Input parameters from both consutrctor and target functions are flattened into the test case struct. Potential field name duplication is handled by introducing prefixes. For golang/vscode-go#1594 Change-Id: I8b56d8f3e0f0432d4f9fe269cc7ba86ea46decfc Reviewed-on: https://go-review.googlesource.com/c/tools/+/626537 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Read and determine whether the first input parameter of function or constructor is a context.Context through package name and name comparison. - Call the function or constructor with context.Background() but honor is there is any renaming in foo.go or foo_test.go. - Fix the issue where the constructor param is added to function or method call. For golang/vscode-go#1594 Change-Id: Ic1d145e65bc4b7cb34f637bab8ebdeccd36a33f9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/627355 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Change https://go.dev/cl/633703 mentions this issue: |
Is your feature request related to a problem? Please describe.
I'm always frustrated when I click on "Generate test for function" and see "No tests were generated for ...".
Describe the solution you'd like
I would like to see a reason of why there was no tests were generated. For example - "test for ... already exist".
Additional context
It would be perfect to see a location of generated tests if there is already existing one. Or after successful generation.
The text was updated successfully, but these errors were encountered: