From 416782340f725b15afa66942e1af7bf63de9327c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 8 Nov 2022 13:35:00 +0000 Subject: [PATCH] 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 --- main.go | 6 ++++-- testdata/script/test.txtar | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 119f120..4979584 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/testdata/script/test.txtar b/testdata/script/test.txtar index f192bea..ff480d8 100644 --- a/testdata/script/test.txtar +++ b/testdata/script/test.txtar @@ -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