Skip to content

Commit

Permalink
support garble test in main packages
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mvdan authored and lu4p committed Nov 8, 2022
1 parent b6a0284 commit 4167823
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions testdata/script/test.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4167823

Please sign in to comment.