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  	"internal/testenv"
    10  	"os"
    11  	"os/exec"
    12  	"runtime"
    13  	"testing"
    14  )
    15  
    16  // Regression test for golang.org/issue/34499: version command should not crash
    17  // when executed in a deleted directory on Linux.
    18  func TestExecInDeletedDir(t *testing.T) {
    19  	switch runtime.GOOS {
    20  	case "windows", "plan9",
    21  		"aix",                // Fails with "device busy".
    22  		"solaris", "illumos": // Fails with "invalid argument".
    23  		t.Skipf("%v does not support removing the current working directory", runtime.GOOS)
    24  	}
    25  	gotool := testenv.GoToolPath(t)
    26  
    27  	tmpdir := t.TempDir()
    28  	t.Chdir(tmpdir)
    29  
    30  	if err := os.Remove(tmpdir); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	// `go version` should not fail
    35  	var stdout, stderr bytes.Buffer
    36  	cmd := exec.Command(gotool, "version")
    37  	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.
    38  	cmd.Stdout = &stdout
    39  	cmd.Stderr = &stderr
    40  	if err := cmd.Run(); err != nil {
    41  		t.Fatalf("running go version: %v\n[stdout]: %s\n[stderr]: %s", err, stdout.Bytes(), stderr.Bytes())
    42  	}
    43  }
    44  

View as plain text