-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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/cmd/stringer: support generating types defined in test files #66557
Comments
Hello @thanm & @rogpeppe There is a comment in stringer.go
which seems straightforward enough. Not sure if a separate pass is necessary, as long as its possible to tell where each relevant type is located. |
@vikblom sure, I think you could pick this up. Seems straightforward and useful. |
Hi @findleyr , The main loop could be: loop over the relevant packages, find the values of matching types (if any), and generate the code for them in the given package. Issue 1: As I understand it, there may be 3 packages in play:
Issue 2: Stringer will warn if a type is not found. This could be a sizeable diff so I'm open to splitting this across CRs, or hearing suggestions for a simpler approach. I'm not sure how strongly this change needs to stick to existing output/warnings/exits. |
Hi @vikblom, sorry for the slow response. The package Of course, constant values could be defined in I think it makes sense to implement the following heuristic: run stringer on the narrowest package containing the type declaration (in gopls, we say 'narrowest' to mean the package with the fewest files, which is the same as saying: |
Hello again, pardon the summer slowness on my end. That heuristic sounds like a good approach. Then any types "left over" at the end can be errored as not found, matching the behaviour today 👍 I still think there is a need to generate different files. Since stringer can accept many types at once, they might be declared in different packages ( |
Change https://go.dev/cl/604535 mentions this issue: |
I'm sorry, but this was a total hallucination or bad miscommunication on my part -- I suppose I must have been thinking of dot-importing the package under test. It is of course possible to redefine a type in the x_test package. Sorry! Let's sort this out on the CL. |
It might make sense to put this functionality behind a new flag. |
@vikblom I think we want to avoid new flags whenever possible. The way the functionality is implemented in your CL is such that existing use-cases should continue to function as they did before, and new use-cases are now supported. I don't think we need a flag to gate a new, purely additive feature. |
Go version
go version devel go1.23-5e1e3a0025 Thu Mar 21 17:25:54 2024 +0000 linux/amd64
What did you do?
Run this testscript script:
What did you see happen?
What did you expect to see?
I'd expect it to succeed and generate a file named
foo_string_test.go
containing aString
method for the typefoo
.It's sometimes useful to have the convenience of
stringer
to generate a String method even if the type is only defined for a test.The text was updated successfully, but these errors were encountered: