@@ -30,6 +30,18 @@ func TestMain(m *testing.M) {
3030 os .Exit (testMain (m ))
3131}
3232
33+ // tmpDir is used to cleanup logged commands -- s/tmpDir/$TMPDIR/
34+ var tmpDir string
35+
36+ // prettyPrintf prints lines with tmpDir sanitized.
37+ func prettyPrintf (format string , args ... interface {}) {
38+ s := fmt .Sprintf (format , args ... )
39+ if tmpDir != "" {
40+ s = strings .ReplaceAll (s , tmpDir , "$TMPDIR" )
41+ }
42+ fmt .Print (s )
43+ }
44+
3345func testMain (m * testing.M ) int {
3446 // Copy testdata into GOPATH/src/testplugin, along with a go.mod file
3547 // declaring the same path.
@@ -39,6 +51,7 @@ func testMain(m *testing.M) int {
3951 log .Panic (err )
4052 }
4153 defer os .RemoveAll (GOPATH )
54+ tmpDir = GOPATH
4255
4356 modRoot := filepath .Join (GOPATH , "src" , "testplugin" )
4457 altRoot := filepath .Join (GOPATH , "alt" , "src" , "testplugin" )
@@ -49,21 +62,29 @@ func testMain(m *testing.M) int {
4962 if err := overlayDir (dstRoot , srcRoot ); err != nil {
5063 log .Panic (err )
5164 }
65+ prettyPrintf ("mkdir -p %s\n " , dstRoot )
66+ prettyPrintf ("rsync -a %s/ %s\n " , srcRoot , dstRoot )
67+
5268 if err := os .WriteFile (filepath .Join (dstRoot , "go.mod" ), []byte ("module testplugin\n " ), 0666 ); err != nil {
5369 log .Panic (err )
5470 }
71+ prettyPrintf ("echo 'module testplugin' > %s/go.mod\n " , dstRoot )
5572 }
5673
5774 os .Setenv ("GOPATH" , filepath .Join (GOPATH , "alt" ))
5875 if err := os .Chdir (altRoot ); err != nil {
5976 log .Panic (err )
77+ } else {
78+ prettyPrintf ("cd %s\n " , altRoot )
6079 }
6180 os .Setenv ("PWD" , altRoot )
6281 goCmd (nil , "build" , "-buildmode=plugin" , "-o" , filepath .Join (modRoot , "plugin-mismatch.so" ), "./plugin-mismatch" )
6382
6483 os .Setenv ("GOPATH" , GOPATH )
6584 if err := os .Chdir (modRoot ); err != nil {
6685 log .Panic (err )
86+ } else {
87+ prettyPrintf ("cd %s\n " , modRoot )
6788 }
6889 os .Setenv ("PWD" , modRoot )
6990
@@ -78,6 +99,7 @@ func testMain(m *testing.M) int {
7899 if err := os .WriteFile ("plugin2-dup.so" , so , 0444 ); err != nil {
79100 log .Panic (err )
80101 }
102+ prettyPrintf ("cp plugin2.so plugin2-dup.so\n " )
81103
82104 goCmd (nil , "build" , "-buildmode=plugin" , "-o=sub/plugin1.so" , "./sub/plugin1" )
83105 goCmd (nil , "build" , "-buildmode=plugin" , "-o=unnamed1.so" , "./unnamed1/main.go" )
@@ -94,8 +116,53 @@ func goCmd(t *testing.T, op string, args ...string) {
94116 run (t , "go" , append ([]string {op , "-gcflags" , gcflags }, args ... )... )
95117}
96118
119+ // escape converts a string to something suitable for a shell command line.
120+ func escape (s string ) string {
121+ s = strings .Replace (s , "\\ " , "\\ \\ " , - 1 )
122+ s = strings .Replace (s , "'" , "\\ '" , - 1 )
123+ // Conservative guess at characters that will force quoting
124+ if s == "" || strings .ContainsAny (s , "\\ ;#*&$~?!|[]()<>{}`" ) {
125+ s = "'" + s + "'"
126+ }
127+ return s
128+ }
129+
130+ // asCommandLine renders cmd as something that could be copy-and-pasted into a command line
131+ func asCommandLine (cwd string , cmd * exec.Cmd ) string {
132+ s := "("
133+ if cmd .Dir != "" && cmd .Dir != cwd {
134+ s += "cd" + escape (cmd .Dir ) + ";"
135+ }
136+ for _ , e := range cmd .Env {
137+ if ! strings .HasPrefix (e , "PATH=" ) &&
138+ ! strings .HasPrefix (e , "HOME=" ) &&
139+ ! strings .HasPrefix (e , "USER=" ) &&
140+ ! strings .HasPrefix (e , "SHELL=" ) {
141+ s += " "
142+ s += escape (e )
143+ }
144+ }
145+ // These EVs are relevant to this test.
146+ for _ , e := range os .Environ () {
147+ if strings .HasPrefix (e , "PWD=" ) ||
148+ strings .HasPrefix (e , "GOPATH=" ) ||
149+ strings .HasPrefix (e , "LD_LIBRARY_PATH=" ) {
150+ s += " "
151+ s += escape (e )
152+ }
153+ }
154+ for _ , a := range cmd .Args {
155+ s += " "
156+ s += escape (a )
157+ }
158+ s += " )"
159+ return s
160+ }
161+
97162func run (t * testing.T , bin string , args ... string ) string {
98163 cmd := exec .Command (bin , args ... )
164+ cmdLine := asCommandLine ("." , cmd )
165+ prettyPrintf ("%s\n " , cmdLine )
99166 cmd .Stderr = new (strings.Builder )
100167 out , err := cmd .Output ()
101168 if err != nil {
0 commit comments