From 47945490eb81473ad592ca87be8de4054f99d6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 20 Sep 2024 13:41:51 +0100 Subject: [PATCH] take advantage of Go 1.22 cmp.Or and no longer needing to shallow copy range loop vars. While here, remove an unused parameter as spotted by gopls. --- goproxytest/proxy.go | 9 +++------ imports/scan.go | 1 + internal/os/execpath/lp_unix.go | 7 +++---- testscript/exe.go | 1 - testscript/testscript.go | 5 ++--- 5 files changed, 9 insertions(+), 14 deletions(-) diff --git a/goproxytest/proxy.go b/goproxytest/proxy.go index d3fc975a..e8fe8a7c 100644 --- a/goproxytest/proxy.go +++ b/goproxytest/proxy.go @@ -24,6 +24,7 @@ package goproxytest import ( "archive/zip" "bytes" + "cmp" "encoding/json" "fmt" "io/fs" @@ -76,12 +77,8 @@ func NewServer(dir, addr string) (*Server, error) { } func newServer(dir, addr string, logf func(string, ...any)) (*Server, error) { - if addr == "" { - addr = "localhost:0" - } - if dir == "" { - dir = "testmod" - } + addr = cmp.Or(addr, "localhost:0") + dir = cmp.Or(dir, "testmod") srv := Server{dir: dir, logf: logf} if err := srv.readModList(); err != nil { return nil, fmt.Errorf("cannot read modules: %v", err) diff --git a/imports/scan.go b/imports/scan.go index e69e271e..dbed3553 100644 --- a/imports/scan.go +++ b/imports/scan.go @@ -85,6 +85,7 @@ Files: var ErrNoGo = fmt.Errorf("no Go source files") +// TODO: replace with maps.Keys from go1.23 func keys(m map[string]bool) []string { var list []string for k := range m { diff --git a/internal/os/execpath/lp_unix.go b/internal/os/execpath/lp_unix.go index 37f18c79..d233a1f5 100644 --- a/internal/os/execpath/lp_unix.go +++ b/internal/os/execpath/lp_unix.go @@ -7,6 +7,7 @@ package execpath import ( + "cmp" "os" "path/filepath" "strings" @@ -45,10 +46,8 @@ func Look(file string, getenv func(string) string) (string, error) { } path := getenv("PATH") for _, dir := range filepath.SplitList(path) { - if dir == "" { - // Unix shell semantics: path element "" means "." - dir = "." - } + // Unix shell semantics: path element "" means "." + dir = cmp.Or(dir, ".") path := filepath.Join(dir, file) if err := findExecutable(path); err == nil { return path, nil diff --git a/testscript/exe.go b/testscript/exe.go index 0ccd7738..9f9fdc14 100644 --- a/testscript/exe.go +++ b/testscript/exe.go @@ -75,7 +75,6 @@ func RunMain(m TestingM, commands map[string]func() int) (exitCode int) { // We're not in a subcommand. for name := range commands { - name := name // Set up this command in the directory we added to $PATH. binfile := filepath.Join(bindir, name) if runtime.GOOS == "windows" { diff --git a/testscript/testscript.go b/testscript/testscript.go index 11460502..e4024837 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -320,7 +320,6 @@ func RunT(t T, p Params) { refCount := int32(len(files)) names := make(map[string]bool) for _, file := range files { - file := file name := filepath.Base(file) if name1, ok := strings.CutSuffix(name, ".txt"); ok { name = name1 @@ -737,13 +736,13 @@ func (ts *TestScript) runLine(line string) (runOK bool) { ts.Fatalf("unknown command %q", args[0]) } } - ts.callBuiltinCmd(args[0], func() { + ts.callBuiltinCmd(func() { cmd(ts, neg, args[1:]) }) return true } -func (ts *TestScript) callBuiltinCmd(cmd string, runCmd func()) { +func (ts *TestScript) callBuiltinCmd(runCmd func()) { ts.runningBuiltin = true defer func() { r := recover()