Source file src/cmd/go/internal/verylongtest/go_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package verylongtest
     6  
     7  import (
     8  	"bytes"
     9  	"flag"
    10  	"fmt"
    11  	"internal/testenv"
    12  	"os"
    13  	"os/exec"
    14  	"runtime"
    15  	"testing"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	flag.Parse()
    20  	if testing.Short() {
    21  		return
    22  	}
    23  
    24  	tmpdir, err := os.MkdirTemp("", "verylongtest")
    25  	if err != nil {
    26  		fmt.Fprintf(os.Stderr, "failed to create temp gocache: %v\n", err)
    27  		os.Exit(1)
    28  	}
    29  	os.Setenv("GOCACHE", tmpdir)
    30  
    31  	code := m.Run()
    32  
    33  	os.RemoveAll(tmpdir)
    34  	os.Exit(code)
    35  }
    36  
    37  // Regression test for golang.org/issue/34499: version command should not crash
    38  // when executed in a deleted directory on Linux.
    39  func TestExecInDeletedDir(t *testing.T) {
    40  	switch runtime.GOOS {
    41  	case "windows", "plan9",
    42  		"aix",                // Fails with "device busy".
    43  		"solaris", "illumos": // Fails with "invalid argument".
    44  		t.Skipf("%v does not support removing the current working directory", runtime.GOOS)
    45  	}
    46  	gotool := testenv.GoToolPath(t)
    47  
    48  	tmpdir := t.TempDir()
    49  	t.Chdir(tmpdir)
    50  
    51  	if err := os.Remove(tmpdir); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	// `go version` should not fail
    56  	var stdout, stderr bytes.Buffer
    57  	cmd := exec.Command(gotool, "version")
    58  	cmd.Env = append(os.Environ(), "GO111MODULE=off") // This behavior doesn't apply with GO111MODULE != off because we need to know the module to check the version.
    59  	cmd.Stdout = &stdout
    60  	cmd.Stderr = &stderr
    61  	if err := cmd.Run(); err != nil {
    62  		t.Fatalf("running go version: %v\n[stdout]: %s\n[stderr]: %s", err, stdout.Bytes(), stderr.Bytes())
    63  	}
    64  }
    65  

View as plain text