-
Notifications
You must be signed in to change notification settings - Fork 569
Add support for TestMain function in test mode #380
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
Conversation
LGTM. Thanks for the contribution! |
For reference, this is the commit that added this feature to Go: golang/go@182d131. So far, the changes in this PR look good, but I see a few things missing from that commit. |
@@ -319,11 +319,13 @@ func main() { | |||
} | |||
|
|||
for _, decl := range testPkg.Archive.Declarations { | |||
if strings.HasPrefix(decl.FullName, testPkg.ImportPath+".Test") { | |||
switch { | |||
case decl.FullName == testPkg.ImportPath+".TestMain": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only checks that the func name matches "TestMain", but doesn't check that the func signature takes m *testing.M
parameters and has no return variables, does it?
That potentially means that some Go package that has a func TestMain(t *testing.T)
will get interpreted differently by go
and gopherjs
, if I understand this correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed, if the signature is func TestMain(t *testing.T)
then the go
tool treats it like a normal test:
31 $ go test -v
=== RUN TestMain
--- PASS: TestMain (0.00s)
=== RUN Test1
--- PASS: Test1 (0.00s)
PASS
ok github.com/shurcooL/play/31 0.022s
I think if you add those 2 pieces of missing functionality I mentioned in inline comments above, this should be good to merge. A test would be helpful too, if possible. Thank you! |
Thanks, @shurcooL, for your critique. My original work included the extra error message, but somehow I missed that in my final cut. I've added it back in. The other issue will be more involved, as the GopherJS implementation differs significantly from the Go implementation at this point. Let me see what I can come up with. |
Thanks. I've looked a little into it, and I saw that the original Go code works with the Go AST, while here in GopherJS you're ranging over Unfortunately, it's not documented, so it's hard to tell, but I hope/suspect you can find the func signature in the Finally, if you don't mind, please do the additional changes in separate commits, and squash at the end when we're ready to merge. That makes the PR easier to review as it changes. |
4641452
to
d77d525
Compare
My latest commit probably needs some work. It is in some ways a blind copy-and-paste of the relevant bits of logic from Go into GopherJS, adapting them where necessary. This means that we now run Also, Go builds 'test' as a separate executable, which lends itself to a clean separation from the other This also gets us much closer to supporting Examples, which depends on an But this seemed like a good place to solicit feedback. |
What's the status of this PR? |
I have not been following closely all the work happening with GopherJS lately, in particular the long-term plans to be more compatible with upstream tools, so I don't know if this PR is, or will be, obsoleted by some of that work. If the approach I took in this PR is appropriate, I would be happy to try to resolve the conflicts and get it back into a mergable state. |
I think I went a little trigger-happy with that label. It applies to issues only, and this is a PR. I'll remove it. It's true this issue will become resolved once we move to using a modified version of cmd/go, but the timeline for that is unclear and it may not happen very soon. There's still value in resolving this. But it's good to be aware of which issues are relevant to the build tool and not the compiler. |
This PR would be really useful. |
My personal hacking time has been limited lately, but I'll try to get to this again soon, and bring the PR up to date. If someone else wants to get to it before I do, please feel free. |
Specifically, we now borrow Go's testFuncs.load(), isTestMain, and isTest functions, instead of relying on our own, less robust, implementation. In terms of functionality, this means that we: - We require the first character after the 'Test' prefix to be capital. e.g. don't treat TesticularCancer() as a test. - We don't treat TestMain(t *testing.T) as TestMain(m *testing.M) - We should be able to support Examples as well (issue gopherjs#239) with minimal effort
This PR is now up to date, and as far as I know, ready to merge. Both of @shurcooL's issues have been addressed: Proper function signature checking on TestMain(), and the TestMain-related error message 'multiple definitions of TestMain' takes priority over the standard 'TestMain redeclared in this block' message. I welcome any further feedback or suggestions. |
@@ -18,11 +20,14 @@ import ( | |||
"path" | |||
"path/filepath" | |||
"runtime" | |||
// "sort" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this.
(I guess it's commented out because of the example code being commented out, right? In any case, it's unclear when examples will work, so this may stay here for a while, and it's not helpful. Better to remove it now and add it when it's needed.)
Thank you for working on this and your patience @flimzy. I've left 2 minor comments, but if this works, I see no reason not merge this. It has LGTM from @neelance, and even though there have been changes since then, as long as it works, we can merge. Sorry for letting this drag on for longer than it should've; I've been focusing on other work and did not have enough time to spend on this. |
Tested locally, Thanks a lot again! :) |
Thanks for merging! |
This adds support for the
TestMain
function in test mode, described here, and requested in issue #239. Naturally, I took inspiration from the relevant official go source.I have tested it to the extent I know to. It's possible I may have missed some functionality. Any other suggestions or feedback is welcome.