github.com/rogpeppe/go-internal@v1.12.1-0.20240509064211-c8567cf8e95f/gotooltest/setup_test.go (about)

     1  package gotooltest
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func TestInitGoEnv(t *testing.T) {
     9  	// Set up a temp directory containing a bad go.mod file to
    10  	// ensure the working directory does not influence the probe
    11  	// commands run during initGoEnv
    12  	td := t.TempDir()
    13  
    14  	// If these commands fail we are in bigger trouble
    15  	wd, _ := os.Getwd()
    16  	os.Chdir(td)
    17  
    18  	t.Cleanup(func() {
    19  		os.Chdir(wd)
    20  		os.Remove(td)
    21  	})
    22  
    23  	if err := os.WriteFile("go.mod", []byte("this is rubbish"), 0600); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	if err := initGoEnv(); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  }