Unreviewed changes
4 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:
```
The name of the file: src/cmd/go/internal/verylongtest/testdata/script/mod_sumdb_golang.txt
Insertions: 1, Deletions: 0.
@@ -14,6 +14,7 @@
[!net:proxy.golang.org] skip
[!net:sum.golang.org] skip
[!git] skip
+env GOMODCACHE=$WORK/modcache # we clean the modcache below
env GOSUMDB=sum.golang.org
env GOPROXY=direct
```
```
The name of the file: src/cmd/go/internal/verylongtest/script_test.go
Insertions: 12, Deletions: 68.
@@ -5,125 +5,66 @@
package verylongtest
import (
- "bytes"
- "cmd/internal/robustio"
"cmd/internal/script"
"cmd/internal/script/scripttest"
- "context"
- "flag"
"fmt"
"internal/testenv"
- "internal/txtar"
"io/fs"
- "log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
- "strings"
"testing"
- "time"
)
-//go:generate go test cmd/go/internal/verylongtest -v -run=TestScript/README --fixreadme
-
-var fixReadme = flag.Bool("fixreadme", false, "if true, update README for script tests")
-
func TestScript(t *testing.T) {
if testing.Short() {
// Don't bother setting up the script engine. None of these are short tests.
t.Skip()
}
- // Tests may create a modcache. Use removeAll to fix the permissions before we run
- // the test, similar to what we do for the standard cmd/go script test suite.
-
testenv.MustHaveGoBuild(t)
testenv.SkipIfShortAndSlow(t)
engine, env := scripttest.NewEngine(t, nil)
+ modcache := filepath.Join(t.TempDir(), "modcache")
+ env = append(env, "GOMODCACHE="+modcache)
+ // Remove write only permissions on GOMODCACHE so we can clear its files.
+ t.Cleanup(func() {
+ filepath.WalkDir(modcache, func(path string, info fs.DirEntry, err error) error {
+ os.Chmod(path, 0777)
+ return nil
+ })
+ })
env = append(env, "GOROOT="+runtime.GOROOT())
- go404proxyAddr := start404Proxy()
+ go404proxyAddr := start404Proxy(t)
env = append(env, "TESTGO_404_PROXY_ADDR="+go404proxyAddr)
engine.Conds["net"] = script.PrefixCondition("can connect to external network host <suffix>", hasNet)
engine.Conds["git"] = script.OnceCondition("the 'git' executable exists and provides the standard CLI", hasWorkingGit)
- runTests(t, t.Context(), engine, env, "testdata/script/*.txt")
+ scripttest.RunTests(t, t.Context(), engine, env, "testdata/script/*.txt")
}
-func start404Proxy() string {
+func start404Proxy(t *testing.T) string {
addr := "localhost:0"
l, err := net.Listen("tcp", addr)
if err != nil {
- log.Fatal(err)
+ t.Fatal(err)
}
- if err != nil {
- log.Fatal(err)
- }
+ t.Cleanup(func() {
+ if err := l.Close(); err != nil {
+ t.Fatal(err)
+ }
+ })
proxyAddr := l.Addr().String()
proxyURL := "http://" + proxyAddr + "/mod"
fmt.Fprintf(os.Stderr, "404 test proxy running at %s\n", proxyURL)
- go func() {
- log.Fatalf("go proxy: http.Serve: %v", http.Serve(l, http.HandlerFunc(func(rw http.ResponseWriter, rew *http.Request) {
- rw.WriteHeader(http.StatusNotFound)
- })))
- }()
+ go http.Serve(l, http.HandlerFunc(func(rw http.ResponseWriter, rew *http.Request) {
+ rw.WriteHeader(http.StatusNotFound)
+ }))
return proxyURL
}
-// runTests kicks off one or more script-based tests using the
-// specified engine, running all test files that match pattern.
-// This function is based on the scripttest.RunTests function, which was
-// adapted from Russ's rsc.io/script/scripttest#Run
-// function, which was in turn forked off cmd/go's runner.
-// It calls defers a removeAll call in the test case to clean up
-// read-only modcache files, similar to the standard go command
-// test suite.
-func runTests(t *testing.T, ctx context.Context, engine *script.Engine, env []string, pattern string) {
- ctx = scripttest.ScriptTestContext(t, ctx)
-
- files, _ := filepath.Glob(pattern)
- if len(files) == 0 {
- t.Fatal("no testdata")
- }
- for _, file := range files {
- file := file
- name := strings.TrimSuffix(filepath.Base(file), ".txt")
- t.Run(name, func(t *testing.T) {
- t.Parallel()
-
- workdir := t.TempDir()
- s, err := script.NewState(ctx, workdir, env)
- if err != nil {
- t.Fatal(err)
- }
-
- defer removeAll(t, workdir)
-
- // Unpack archive.
- a, err := txtar.ParseFile(file)
- if err != nil {
- t.Fatal(err)
- }
- scripttest.InitScriptDirs(t, s)
- if err := s.ExtractFiles(a); err != nil {
- t.Fatal(err)
- }
-
- t.Log(time.Now().UTC().Format(time.RFC3339))
- work, _ := s.LookupEnv("WORK")
- t.Logf("$WORK=%s", work)
-
- // Note: Do not use filepath.Base(file) here:
- // editors that can jump to file:line references in the output
- // will work better seeing the full path relative to the
- // directory containing the command being tested
- // (e.g. where "go test" command is usually run).
- scripttest.Run(t, engine, s, file, bytes.NewReader(a.Comment))
- })
- }
-}
-
func hasNet(*script.State, string) (bool, error) {
return testenv.HasExternalNetwork(), nil
}
@@ -137,24 +78,3 @@
_, err := exec.LookPath("git")
return err == nil, nil
}
-
-func removeAll(t *testing.T, dir string) {
- t.Helper()
-
- // module cache has 0444 directories;
- // make them writable in order to remove content.
- filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
- log.Println("walking ", path)
-
- // chmod not only directories, but also things that we couldn't even stat
- // due to permission errors: they may also be unreadable directories.
- if err != nil || info.IsDir() {
- log.Println("chmodding ", path)
- os.Chmod(path, 0777)
- }
- return nil
- })
- if err := robustio.RemoveAll(dir); err != nil {
- t.Error(err)
- }
-}
```