]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/go/go_test.go
[dev.boringcrypto] all: merge master (2f0da6d) into dev.boringcrypto
[gostls13.git] / src / cmd / go / go_test.go
index 38d1fe6d1dcc275f4405977139105e2a0c8d5973..e3fd1cd53fb9c0ff441a5d7d141a93b797f182e8 100644 (file)
@@ -17,7 +17,6 @@ import (
        "internal/testenv"
        "io"
        "io/fs"
-       "io/ioutil"
        "log"
        "os"
        "os/exec"
@@ -32,6 +31,7 @@ import (
        "cmd/go/internal/cache"
        "cmd/go/internal/cfg"
        "cmd/go/internal/robustio"
+       "cmd/go/internal/work"
        "cmd/internal/sys"
 )
 
@@ -100,7 +100,7 @@ func TestMain(m *testing.M) {
 
        // Run with a temporary TMPDIR to check that the tests don't
        // leave anything behind.
-       topTmpdir, err := ioutil.TempDir("", "cmd-go-test-")
+       topTmpdir, err := os.MkdirTemp("", "cmd-go-test-")
        if err != nil {
                log.Fatal(err)
        }
@@ -109,7 +109,7 @@ func TestMain(m *testing.M) {
        }
        os.Setenv(tempEnvName(), topTmpdir)
 
-       dir, err := ioutil.TempDir(topTmpdir, "tmpdir")
+       dir, err := os.MkdirTemp(topTmpdir, "tmpdir")
        if err != nil {
                log.Fatal(err)
        }
@@ -216,6 +216,7 @@ func TestMain(m *testing.M) {
        }
        // Don't let these environment variables confuse the test.
        os.Setenv("GOENV", "off")
+       os.Unsetenv("GOFLAGS")
        os.Unsetenv("GOBIN")
        os.Unsetenv("GOPATH")
        os.Unsetenv("GIT_ALLOW_PROTOCOL")
@@ -616,7 +617,7 @@ func (tg *testgoData) makeTempdir() {
        tg.t.Helper()
        if tg.tempdir == "" {
                var err error
-               tg.tempdir, err = ioutil.TempDir("", "gotest")
+               tg.tempdir, err = os.MkdirTemp("", "gotest")
                tg.must(err)
        }
 }
@@ -633,7 +634,7 @@ func (tg *testgoData) tempFile(path, contents string) {
                        bytes = formatted
                }
        }
-       tg.must(ioutil.WriteFile(filepath.Join(tg.tempdir, path), bytes, 0644))
+       tg.must(os.WriteFile(filepath.Join(tg.tempdir, path), bytes, 0644))
 }
 
 // tempDir adds a temporary directory for a run of testgo.
@@ -774,7 +775,7 @@ func (tg *testgoData) cleanup() {
 func removeAll(dir string) error {
        // module cache has 0444 directories;
        // make them writable in order to remove content.
-       filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
+       filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
                // 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() {
@@ -810,6 +811,7 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
        // so that we can change files.
        for _, copydir := range []string{
                "src/runtime",
+               "src/internal/abi",
                "src/internal/bytealg",
                "src/internal/cpu",
                "src/math/bits",
@@ -820,8 +822,8 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
        } {
                srcdir := filepath.Join(testGOROOT, copydir)
                tg.tempDir(filepath.Join("goroot", copydir))
-               err := filepath.Walk(srcdir,
-                       func(path string, info fs.FileInfo, err error) error {
+               err := filepath.WalkDir(srcdir,
+                       func(path string, info fs.DirEntry, err error) error {
                                if err != nil {
                                        return err
                                }
@@ -833,13 +835,13 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
                                        return err
                                }
                                dest := filepath.Join("goroot", copydir, srcrel)
-                               data, err := ioutil.ReadFile(path)
+                               data, err := os.ReadFile(path)
                                if err != nil {
                                        return err
                                }
                                tg.tempFile(dest, string(data))
-                               if err := os.Chmod(tg.path(dest), info.Mode()|0200); err != nil {
-                                       return err
+                               if strings.Contains(copydir, filepath.Join("pkg", "tool")) {
+                                       os.Chmod(tg.path(dest), 0777)
                                }
                                return nil
                        })
@@ -850,18 +852,18 @@ func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
        tg.setenv("GOROOT", tg.path("goroot"))
 
        addVar := func(name string, idx int) (restore func()) {
-               data, err := ioutil.ReadFile(name)
+               data, err := os.ReadFile(name)
                if err != nil {
                        t.Fatal(err)
                }
                old := data
                data = append(data, fmt.Sprintf("var DummyUnusedVar%d bool\n", idx)...)
-               if err := ioutil.WriteFile(name, append(data, '\n'), 0666); err != nil {
+               if err := os.WriteFile(name, append(data, '\n'), 0666); err != nil {
                        t.Fatal(err)
                }
                tg.sleep()
                return func() {
-                       if err := ioutil.WriteFile(name, old, 0666); err != nil {
+                       if err := os.WriteFile(name, old, 0666); err != nil {
                                t.Fatal(err)
                        }
                }
@@ -1366,6 +1368,30 @@ func TestLdflagsArgumentsWithSpacesIssue3941(t *testing.T) {
        tg.grepStderr("^hello world", `ldflags -X "main.extern=hello world"' failed`)
 }
 
+func TestLdFlagsLongArgumentsIssue42295(t *testing.T) {
+       // Test the extremely long command line arguments that contain '\n' characters
+       // get encoded and passed correctly.
+       skipIfGccgo(t, "gccgo does not support -ldflags -X")
+       tooSlow(t)
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+       tg.tempFile("main.go", `package main
+               var extern string
+               func main() {
+                       print(extern)
+               }`)
+       testStr := "test test test test test \n\\ "
+       var buf bytes.Buffer
+       for buf.Len() < work.ArgLengthForResponseFile+1 {
+               buf.WriteString(testStr)
+       }
+       tg.run("run", "-ldflags", fmt.Sprintf(`-X "main.extern=%s"`, buf.String()), tg.path("main.go"))
+       if tg.stderr.String() != buf.String() {
+               t.Errorf("strings differ")
+       }
+}
+
 func TestGoTestDashCDashOControlsBinaryLocation(t *testing.T) {
        skipIfGccgo(t, "gccgo has no standard packages")
        tooSlow(t)
@@ -1894,6 +1920,18 @@ func TestGoEnv(t *testing.T) {
        tg.grepStdout("gcc", "CC not found")
        tg.run("env", "GOGCCFLAGS")
        tg.grepStdout("-ffaster", "CC arguments not found")
+
+       tg.run("env", "GOVERSION")
+       envVersion := strings.TrimSpace(tg.stdout.String())
+
+       tg.run("version")
+       cmdVersion := strings.TrimSpace(tg.stdout.String())
+
+       // If 'go version' is "go version <version> <goos>/<goarch>", then
+       // 'go env GOVERSION' is just "<version>".
+       if cmdVersion == envVersion || !strings.Contains(cmdVersion, envVersion) {
+               t.Fatalf("'go env GOVERSION' %q should be a shorter substring of 'go version' %q", envVersion, cmdVersion)
+       }
 }
 
 const (
@@ -2627,12 +2665,12 @@ func TestBadCommandLines(t *testing.T) {
        tg.tempFile("src/@x/x.go", "package x\n")
        tg.setenv("GOPATH", tg.path("."))
        tg.runFail("build", "@x")
-       tg.grepStderr("invalid input directory name \"@x\"|cannot use path@version syntax", "did not reject @x directory")
+       tg.grepStderr("invalid input directory name \"@x\"|can only use path@version syntax with 'go get' and 'go install' in module-aware mode", "did not reject @x directory")
 
        tg.tempFile("src/@x/y/y.go", "package y\n")
        tg.setenv("GOPATH", tg.path("."))
        tg.runFail("build", "@x/y")
-       tg.grepStderr("invalid import path \"@x/y\"|cannot use path@version syntax", "did not reject @x/y import path")
+       tg.grepStderr("invalid import path \"@x/y\"|can only use path@version syntax with 'go get' and 'go install' in module-aware mode", "did not reject @x/y import path")
 
        tg.tempFile("src/-x/x.go", "package x\n")
        tg.setenv("GOPATH", tg.path("."))
@@ -2670,7 +2708,7 @@ echo $* >>`+tg.path("pkg-config.out"))
        tg.setenv("GOPATH", tg.path("."))
        tg.setenv("PKG_CONFIG", tg.path("pkg-config.sh"))
        tg.run("build", "x")
-       out, err := ioutil.ReadFile(tg.path("pkg-config.out"))
+       out, err := os.ReadFile(tg.path("pkg-config.out"))
        tg.must(err)
        out = bytes.TrimSpace(out)
        want := "--cflags --static --static -- a a\n--libs --static --static -- a a"