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

gotest produces a _testmain.go which doesn't compile when the testing a package called "examples" #2346

Closed
gopherbot opened this issue Oct 7, 2011 · 6 comments

Comments

@gopherbot
Copy link
Contributor

by esko.luontola:

What steps will reproduce the problem?

1. Create file foo_test.go

package examples

import (
    "testing"
)

func TestFoo(t *testing.T) {
}

2. Create Makefile

include $(GOROOT)/src/Make.inc

TARG=examples
GOFILES=\

include $(GOROOT)/src/Make.pkg

3. Run the command make test

What is the expected output?
The test should execute.

What do you see instead?
There is a compile error:
_testmain.go:13: examples redeclared in this block
    previous declaration at _testmain.go:3

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Linux

Which revision are you using?  (hg identify)
06199863489f tip

The generated _testmain.go file is attached. The issue is that the import of the package
being tested conflicts with one of the variables used in that file. Would a general
solution be to always import the package with an alias which is known to be safe?

Attachments:

  1. _testmain.go (638 bytes)
@rsc
Copy link
Contributor

rsc commented Oct 7, 2011

Comment 1:

Need to use _examples_ or something like
that as the variable name.

Owner changed to @adg.

Status changed to Accepted.

@gopherbot
Copy link
Contributor Author

Comment 2 by esko.luontola:

Changing just the variable name examples to _examples_ solves it for "examples" package,
but the same conflict can still happen when testing a package whose name is the same as
one of the other variables (tests, benchmarks, matchPat, matchRe, pat, str, result, err)
or one of the other imports (testing, __os__, __regexp__).
Why not change gotest to produce something like this?
import sut "examples"
var tests = []testing.InternalTest{
        {"examples.TestFoo", sut.TestFoo},
}
I hacked gotest to generate a file like that (see the attachment) and it worked at least
in the above situation. Though it might not work if gotest imports tests from multiple
packages (is it possible?), in which case prefixing the imported packages might be
needed.

Attachments:

  1. gotest-diff.txt (754 bytes)

@rsc
Copy link
Contributor

rsc commented Oct 7, 2011

Comment 3:

Sounds like a reasonable solution; please send us a CL.
http://golang.org/doc/contribute.html
Thanks!
Russ

@gopherbot
Copy link
Contributor Author

Comment 4 by esko.luontola:

CL created: http://golang.org/cl/5254061/

@adg
Copy link
Contributor

adg commented Oct 17, 2011

Comment 5:

Status changed to Started.

@adg
Copy link
Contributor

adg commented Oct 17, 2011

Comment 6:

This issue was closed by revision edd1c91.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned adg Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants