Source file src/cmd/go/internal/verylongtest/script_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  	"cmd/internal/script"
     9  	"cmd/internal/script/scripttest"
    10  	"fmt"
    11  	"internal/testenv"
    12  	"io/fs"
    13  	"net"
    14  	"net/http"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  	"runtime"
    19  	"testing"
    20  )
    21  
    22  func TestScript(t *testing.T) {
    23  	if testing.Short() {
    24  		// Don't bother setting up the script engine. None of these are short tests.
    25  		t.Skip()
    26  	}
    27  	testenv.MustHaveGoBuild(t)
    28  	testenv.SkipIfShortAndSlow(t)
    29  
    30  	engine, env := scripttest.NewEngine(t, nil)
    31  	modcache := filepath.Join(t.TempDir(), "modcache")
    32  	env = append(env, "GOMODCACHE="+modcache)
    33  	// Remove write only permissions on GOMODCACHE so we can clear its files.
    34  	t.Cleanup(func() {
    35  		filepath.WalkDir(modcache, func(path string, info fs.DirEntry, err error) error {
    36  			os.Chmod(path, 0777)
    37  			return nil
    38  		})
    39  	})
    40  	env = append(env, "GOROOT="+runtime.GOROOT())
    41  	go404proxyAddr := start404Proxy(t)
    42  	env = append(env, "TESTGO_404_PROXY_ADDR="+go404proxyAddr)
    43  	engine.Conds["net"] = script.PrefixCondition("can connect to external network host <suffix>", hasNet)
    44  	engine.Conds["git"] = script.OnceCondition("the 'git' executable exists and provides the standard CLI", hasWorkingGit)
    45  	scripttest.RunTests(t, t.Context(), engine, env, "testdata/script/*.txt")
    46  }
    47  
    48  func start404Proxy(t *testing.T) string {
    49  	addr := "localhost:0"
    50  	l, err := net.Listen("tcp", addr)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	t.Cleanup(func() {
    55  		if err := l.Close(); err != nil {
    56  			t.Fatal(err)
    57  		}
    58  	})
    59  	proxyAddr := l.Addr().String()
    60  	proxyURL := "http://" + proxyAddr + "/mod"
    61  	fmt.Fprintf(os.Stderr, "404 test proxy running at %s\n", proxyURL)
    62  	go http.Serve(l, http.HandlerFunc(func(rw http.ResponseWriter, rew *http.Request) {
    63  		rw.WriteHeader(http.StatusNotFound)
    64  	}))
    65  	return proxyURL
    66  }
    67  
    68  func hasNet(*script.State, string) (bool, error) {
    69  	return testenv.HasExternalNetwork(), nil
    70  }
    71  
    72  func hasWorkingGit() (bool, error) {
    73  	if runtime.GOOS == "plan9" {
    74  		// The Git command is usually not the real Git on Plan 9.
    75  		// See https://golang.org/issues/29640.
    76  		return false, nil
    77  	}
    78  	_, err := exec.LookPath("git")
    79  	return err == nil, nil
    80  }
    81  

View as plain text