support `garble test` in main packages

A main package can be imported in one edge case we weren't testing:
when the same main package has any tests, which implicitly import main.

Before the fix, we would panic:

        > garble test -v ./...
        # test/bar/somemaintest.test
        panic: main packages should never need to obfuscate their import paths
pull/606/head
Daniel Martí 2 years ago committed by lu4p
parent b6a0284f84
commit 416782340f

@ -1835,8 +1835,10 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File {
if !lpkg.ToObfuscate {
return true
}
newPath := lpkg.obfuscatedImportPath()
imp.Path.Value = strconv.Quote(newPath)
if lpkg.Name != "main" {
newPath := lpkg.obfuscatedImportPath()
imp.Path.Value = strconv.Quote(newPath)
}
if imp.Name == nil {
imp.Name = &ast.Ident{
NamePos: imp.Path.ValuePos, // ensure it ends up on the same line

@ -28,12 +28,14 @@ stdout 'package bar_test, func name:'
garble test -v ./...
stdout 'ok\s+test/bar\s'
stdout 'PASS.*TestFoo'
stdout 'PASS.*TestMain'
stdout 'PASS.*TestSeparateFoo'
stdout 'SKIP.*TestWithFlag'
stdout 'package bar, func name:'
stdout 'package bar_test, func name:'
! stdout 'OriginalFuncName'
stdout '\?\s+test/bar/somemain\s'
stdout 'ok\s+test/bar/somemaintest\s'
stdout 'ok\s+test/bar/sometest\s'
stdout 'ok\s+test/bar/exporttest\s'
@ -120,6 +122,16 @@ func TestMain(m *testing.M) {
package main
func main() {}
-- somemaintest/main.go --
package main
func main() {}
-- somemaintest/main_test.go --
package main
import "testing"
func TestMain(t *testing.T) {}
-- sometest/foo_test.go --
package sometest

Loading…
Cancel
Save