Skip to content
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

don't run main function when testing (fixes #277) #282

Closed
wants to merge 2 commits into from
Closed

Conversation

neelance
Copy link
Member

No description provided.

@dmitshur
Copy link
Member

I can confirm this fixes the reproduce case I've provided.

@dmitshur
Copy link
Member

If it's not too hard, it might be nice to add a regression test for this, since in most cases running main() during a test may not be as immediately obvious but lead to strange side-effects.

Feel free to use or base it on github.com/shurcooL/play/145 if that's helpful.

If you feel confident this won't break in the future or this test doesn't belong here, it's fine not to add it.

@dmitshur
Copy link
Member

I am not familiar with the context of the fix, and I don't completely understand the change, so take that into account.

I don't see anything obviously wrong with it, tests pass and the reported issue is resolved, so it LGTM but I cannot offer full confidence in it.

@neelance
Copy link
Member Author

Test added.

Can I tell you more about the change? The problem was that I assumed that you can not depend on a main package, which is usually the case. Only that testing creates an additional fake main package which depends on the main package being tested. The $init function or a package used to also run the main function and this needs to stay there because of code that needs to be generated to support blocking init functions. So the solution that I chose is to store the package that needs to run the main function in the global $mainPkg variable and add an if statement around the call to main that checks if the current package being initialized is really the correct main package.

@neelance neelance closed this Aug 30, 2015
@neelance neelance deleted the testmain branch August 30, 2015 09:57
@dmitshur
Copy link
Member

That was very helpful indeed, thanks for the explanation.


import "testing"

func TestMain(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neelance, are you aware that this name TestMain has special meaning as of Go 1.4?

The testing package has a new facility to provide more control over running a set of tests. If the test code contains a function

func TestMain(m *testing.M) 

that function will be called instead of running the tests directly. The M struct contains methods to access and run the tests.

Source: https://golang.org/doc/go1.4

I kinda suspect it's an unintentional usage of that feature here, since you're testing a feature named main. If so, I think it's better we rename it to avoid doing something unintentionally just due to a coincidence in naming, as it might cause confusion later (if other tests are ever added here, they will not run). Perhaps func TestNotRunMain(m *testing.M) or so?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neelance, did you see this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know that. Thanks for pointing it out. Fixed via 01e6396.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

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.

2 participants