Skip to content

Commit

Permalink
cmd/internal/testdir: use sync.OnceValue
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 6, 2024
1 parent 46bccde commit 2b832b4
Showing 1 changed file with 29 additions and 46 deletions.
75 changes: 29 additions & 46 deletions src/cmd/internal/testdir/testdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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").
Expand Down

0 comments on commit 2b832b4

Please sign in to comment.