using go/parser, go/types, and export_test.go files

173 views
Skip to first unread message

Nate Finch

unread,
Apr 26, 2016, 10:05:35 PM4/26/16
to golang-nuts
I'm trying to write code that uses go/types to parse all of github.com/juju/juju/... and looks for places we create a crypto/tls.Config.

However, my code is tripping up over some test code that uses the export_test.go pattern.  Here's the exact way it's failing:

 go types says "ConfigFileExists not declared by package agent" for this line:  https://github.com/juju/juju/blob/master/agent/agent_test.go#L387

Note that agent_test.go is declared in package agent_test and export_test.go is declared in package agent.

I'm not sure how to resolve this using go/parser and go/types.  here's the code I'm running to parse a directory:


Is there a way around this?  Any way to tell go/parser or go/types to include _test files when parsing?  I didn't immediately see anything, but may well have missed something.

Any help appreciated.

-Nate

Dmitri Shuralyov

unread,
Apr 27, 2016, 1:26:32 AM4/27/16
to golang-nuts
go types says "ConfigFileExists not declared by package agent" for this line:  https://github.com/juju/juju/blob/master/agent/agent_test.go#L387

It's a tricky case, agent.ConfigFileExists is called from package package agent_test of a _test.go file (external test package) but declared in a package agent of a _test.go file (internal test package).

Do you actually want to include tests in your query, or are you including them just because parser.ParseDir is returning them? If you don't want them, use go/build.Import to get a *build.Package and then get .go files from there that are not TestGoFiles nor XTestGoFiles.

I'm not sure off the top of my head how well go/types, go/loader support test packages. Maybe someone else knows more.

Dmitri Shuralyov

unread,
Apr 27, 2016, 1:27:58 AM4/27/16
to golang-nuts
Sorry for typos, I'm used to posting in places where I can always edit/undo. >.<

I meant "go/importer" not "go/loader", and I meant to quote the first paragraph since you wrote it.

Nate Finch

unread,
Apr 27, 2016, 11:58:31 AM4/27/16
to golang-nuts
FWIW, I finally got it working with this code: https://play.golang.org/p/CLnJRrkLeu 

The changes mostly just involved using go/build to import the code and use that for the gofiles.  Changes suggested by the intrepid Andrew Wilkins.

I am skipping the test files now... I don't actually care that much about tests, just production code.
Reply all
Reply to author
Forward
0 new messages