Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 4 commits into from
Jun 5, 2016

Conversation

flimzy
Copy link
Member

@flimzy flimzy commented Jan 19, 2016

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.

@neelance
Copy link
Member

LGTM. Thanks for the contribution!
@shurcooL PTAL

@dmitshur
Copy link
Member

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":
Copy link
Member

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.

Copy link
Member

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

@dmitshur
Copy link
Member

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!

@flimzy
Copy link
Member Author

flimzy commented Jan 21, 2016

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.

@dmitshur
Copy link
Member

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 testPkg.Archive.Declarations, which has type Decl.

Unfortunately, it's not documented, so it's hard to tell, but I hope/suspect you can find the func signature in the Vars field.

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.

@flimzy flimzy force-pushed the testmain branch 2 times, most recently from 4641452 to d77d525 Compare January 23, 2016 13:00
@flimzy
Copy link
Member Author

flimzy commented Jan 23, 2016

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 parser.ParseFile() on test files a second time, but it appears this is the way Go does it, too--and perhaps there's a reason for this (beyond simply being easier to write) that would be revealed with further investigation.

Also, Go builds 'test' as a separate executable, which lends itself to a clean separation from the other go commands. GopherJS is more monolythic, meaning that adding this functionality into tool.go might be considered more clutter. A simple step toward clarity might be just to create a test.go, but within the same main package. I think @neelance has hinted at some long-term plans to potentially refactor parts of tool.go, so I don't know how this might fit into that larger picture.

This also gets us much closer to supporting Examples, which depends on an *ast.File argument to doc.Examples to extract the expected output for each example function. This commit doesn't yet make Examples fully functional, though (so I have some of that commented out), but I don't think it will be terribly hard to get it working (which I'll work on next).

But this seemed like a good place to solicit feedback.

@neelance
Copy link
Member

What's the status of this PR?

@dmitshur dmitshur added the gopherjs-tool Related to the gopherjs tool or its build system (but not the compiler itself). label Apr 1, 2016
@flimzy
Copy link
Member Author

flimzy commented Apr 2, 2016

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.

@dmitshur
Copy link
Member

dmitshur commented Apr 2, 2016

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.

@dmitshur dmitshur removed the gopherjs-tool Related to the gopherjs tool or its build system (but not the compiler itself). label Apr 2, 2016
@bep
Copy link

bep commented May 10, 2016

This PR would be really useful.

@flimzy
Copy link
Member Author

flimzy commented May 11, 2016

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.

flimzy added 4 commits May 15, 2016 12:15
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
@flimzy
Copy link
Member Author

flimzy commented May 15, 2016

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"
Copy link
Member

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.)

@dmitshur
Copy link
Member

dmitshur commented Jun 5, 2016

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.

@dmitshur
Copy link
Member

dmitshur commented Jun 5, 2016

Tested locally, gopherjs test still works and TestMain is working. Merging. I'll apply the 2 comments I left above myself. Edit: Applied in a3087f6.

Thanks a lot again! :)

@dmitshur dmitshur merged commit dbe4e14 into gopherjs:master Jun 5, 2016
dmitshur added a commit that referenced this pull request Jun 5, 2016
@flimzy
Copy link
Member Author

flimzy commented Jun 6, 2016

Thanks for merging!

@flimzy flimzy deleted the testmain branch June 6, 2016 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants