You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not being able to use cgo from within gotest prevents integration of regression testing
on packages that make use of C.
What steps will reproduce the problem?
1. import "C" from a new test file
2. run go test <package> on its package
What is the expected output?
> ok/FAIL <package> 0.020s
> ...
What do you see instead?
> can't load package: package <package>: use of cgo in test
/path/to/file/using/cgo/dlbug_fail_test.go not supported
Which compiler are you using (5g, 6g, 8g, gccgo)?
8g
Which operating system are you using?
OS X 10.8.1 (12B19)
Which version are you using? (run 'go version')
go version go1.0.2
Please provide any additional information below.
This is not strictly true.
You can test a package that imports "C" just fine.
What you cannot do is import "C" in the test files.
Why do you want to do that?
Russ
Fair enough, but the idea of a test case is that it's as small and local as possible to
reduce the number of variables. Importing an external package in its entirety sort of
defeats that notion.
I'm a bit confused. It sounds like you think I said the answer is to
package up the C functionality you need during your test in some other
package and then import that "external" package.
What I'm saying is that import "C" is an implementation detail of the
package you are trying to test. The test itself shouldn't need to
import "C" at all.
Russ
> I'm a bit confused. It sounds like you think I said the answer is to
> package up the C functionality you need during your test in some other
> package and then import that "external" package.
No, I follow your reasoning completely. There's nothing wrong with saving some labor by
requiring some indirection, and it actually may better isolate the issue. It's just
counterintuitive.
Wanting to use cgo in a test seems reasonable to me.
As an example I implemented the makedev, major and minor functions in Go, and the natural way to test the implementation is to use the C implementation as a reference.
If you actually read the code that I provided, you will see that you don't
need to create another package.
What I meant is that you need to put the test in a different package.
This is what it is done in test/cgo_test.go.
The alternative is to put cgo code inside the package being tested, but
this means that
the cgo code will be build (slowly, compared to gc) and will be included in
the generated binary package.
Ideally I would like to have the cgo code built and used only by the test
code.
Thanks
—
Reply to this email directly or view it on GitHub #4030 (comment).
Activity
rsc commentedon Sep 1, 2012
Comment 1:
gopherbot commentedon Sep 1, 2012
Comment 2 by d.wright.dev:
rsc commentedon Sep 1, 2012
Comment 3:
gopherbot commentedon Sep 1, 2012
Comment 4 by d.wright.dev:
rsc commentedon Sep 12, 2012
Comment 5:
Status changed to WorkingAsIntended.
perillo commentedon Feb 21, 2015
Wanting to use cgo in a test seems reasonable to me.
As an example I implemented the makedev, major and minor functions in Go, and the natural way to test the implementation is to use the C implementation as a reference.
[-]cgo from within gotest[/-][+]testing: cgo from within gotest[/+]minux commentedon Feb 23, 2015
perillo commentedon Feb 24, 2015
On Tue, Feb 24, 2015 at 12:31 AM, Minux Ma notifications@github.com wrote:
Thanks.
However this is simply too much "complex" for my use case, since I will
need to create a separate test package just to test 5 lines of code.
Manlio Perillo
minux commentedon Feb 24, 2015
perillo commentedon Feb 25, 2015
On Tue, Feb 24, 2015 at 9:09 PM, Minux Ma notifications@github.com wrote:
The alternative is to put cgo code inside the package being tested, but
this means that
the cgo code will be build (slowly, compared to gc) and will be included in
the generated binary package.
Ideally I would like to have the cgo code built and used only by the test
code.
Thanks
—
minux commentedon Feb 25, 2015