aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal
diff options
context:
space:
mode:
authorKir Kolyshkin <kolyshkin@gmail.com>2024-09-04 18:15:04 -0700
committerGopher Robot <gobot@golang.org>2024-09-06 13:26:38 +0000
commit2b832b4296220aab1189e1b7e939e4e68e8924b9 (patch)
tree67b2857c9fe755a8d976354959f4e65c504bd9d9 /src/cmd/internal
parent46bccdebfaea94157b743beeb207aa5afd70e7a8 (diff)
downloadgo-2b832b4296220aab1189e1b7e939e4e68e8924b9.tar.xz
cmd/internal/testdir: use sync.OnceValue
Change-Id: I90fd0318c7f85032ef8b6621331fe2a8a2da41f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/611040 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/cmd/internal')
-rw-r--r--src/cmd/internal/testdir/testdir_test.go75
1 files changed, 29 insertions, 46 deletions
diff --git a/src/cmd/internal/testdir/testdir_test.go b/src/cmd/internal/testdir/testdir_test.go
index 31cca41a13..c3cadd5fc6 100644
--- a/src/cmd/internal/testdir/testdir_test.go
+++ b/src/cmd/internal/testdir/testdir_test.go
@@ -209,40 +209,28 @@ func compileInDir(runcmd runCmd, dir string, flags []string, importcfg string, p
return runcmd(cmd...)
}
-var stdlibImportcfgStringOnce sync.Once // TODO(#56102): Use sync.OnceValue once available. Also below.
-var stdlibImportcfgString string
-
-func stdlibImportcfg() string {
- stdlibImportcfgStringOnce.Do(func() {
- cmd := exec.Command(goTool, "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std")
- cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
- output, err := cmd.Output()
- if err != nil {
- log.Fatal(err)
- }
- stdlibImportcfgString = string(output)
- })
- return stdlibImportcfgString
-}
-
-var stdlibImportcfgFilenameOnce sync.Once
-var stdlibImportcfgFilename string
+var stdlibImportcfg = sync.OnceValue(func() string {
+ cmd := exec.Command(goTool, "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std")
+ cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
+ output, err := cmd.Output()
+ if err != nil {
+ log.Fatal(err)
+ }
+ return string(output)
+})
-func stdlibImportcfgFile() string {
- stdlibImportcfgFilenameOnce.Do(func() {
- tmpdir, err := os.MkdirTemp("", "importcfg")
- if err != nil {
- log.Fatal(err)
- }
- filename := filepath.Join(tmpdir, "importcfg")
- err = os.WriteFile(filename, []byte(stdlibImportcfg()), 0644)
- if err != nil {
- log.Fatal(err)
- }
- stdlibImportcfgFilename = filename
- })
- return stdlibImportcfgFilename
-}
+var stdlibImportcfgFile = sync.OnceValue(func() string {
+ tmpdir, err := os.MkdirTemp("", "importcfg")
+ if err != nil {
+ log.Fatal(err)
+ }
+ filename := filepath.Join(tmpdir, "importcfg")
+ err = os.WriteFile(filename, []byte(stdlibImportcfg()), 0644)
+ if err != nil {
+ log.Fatal(err)
+ }
+ return filename
+})
func linkFile(runcmd runCmd, goname string, importcfg string, ldflags []string) (err error) {
if importcfg == "" {
@@ -946,7 +934,6 @@ func (t test) run() error {
case ".s":
asms = append(asms, filepath.Join(longdir, file.Name()))
}
-
}
if len(asms) > 0 {
emptyHdrFile := filepath.Join(tempDir, "go_asm.h")
@@ -1132,19 +1119,15 @@ func (t test) run() error {
}
}
-var execCmdOnce sync.Once
-var execCmd []string
-
-func findExecCmd() []string {
- execCmdOnce.Do(func() {
- if goos == runtime.GOOS && goarch == runtime.GOARCH {
- // Do nothing.
- } else if path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)); err == nil {
- execCmd = []string{path}
- }
- })
+var findExecCmd = sync.OnceValue(func() (execCmd []string) {
+ if goos == runtime.GOOS && goarch == runtime.GOARCH {
+ return nil
+ }
+ if path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)); err == nil {
+ execCmd = []string{path}
+ }
return execCmd
-}
+})
// checkExpectedOutput compares the output from compiling and/or running with the contents
// of the corresponding reference output file, if any (replace ".go" with ".out").