cmd/testwrapper: support experimental -cachelink

Updates tailscale/go#149

Change-Id: If0483466eb1fc2196838c75f6d53925b1809abff
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2026-02-02 17:11:01 -08:00
committed by Brad Fitzpatrick
parent 14322713a5
commit 7b96c4c23e
+37
View File
@@ -8,6 +8,7 @@ import (
"io" "io"
"os" "os"
"slices" "slices"
"strconv"
"strings" "strings"
"testing" "testing"
) )
@@ -60,6 +61,9 @@ func splitArgs(args []string) (pre, pkgs, post []string, _ error) {
return nil, nil, nil, err return nil, nil, nil, err
} }
fs.Visit(func(f *flag.Flag) { fs.Visit(func(f *flag.Flag) {
if f.Name == "cachelink" && !cacheLink.enabled {
return
}
if f.Value.String() != f.DefValue && f.DefValue != "false" { if f.Value.String() != f.DefValue && f.DefValue != "false" {
pre = append(pre, "-"+f.Name, f.Value.String()) pre = append(pre, "-"+f.Name, f.Value.String())
} else { } else {
@@ -79,6 +83,37 @@ func splitArgs(args []string) (pre, pkgs, post []string, _ error) {
return pre, pkgs, post, nil return pre, pkgs, post, nil
} }
// cacheLink is whether the -cachelink flag is enabled.
//
// The -cachelink flag is Tailscale-specific addition to the "go test" command;
// see https://github.com/tailscale/go/issues/149 and
// https://github.com/golang/go/issues/77349.
//
// In that PR, it's only a boolean, but we implement a custom flag type
// so we can support -cachelink=auto, which enables cachelink if GOCACHEPROG
// is set, which is a behavior we want in our CI environment.
var cacheLink cacheLinkVal
type cacheLinkVal struct {
enabled bool
}
func (c *cacheLinkVal) String() string {
return strconv.FormatBool(c.enabled)
}
func (c *cacheLinkVal) Set(s string) error {
if s == "auto" {
c.enabled = os.Getenv("GOCACHEPROG") != ""
return nil
}
var err error
c.enabled, err = strconv.ParseBool(s)
return err
}
func (*cacheLinkVal) IsBoolFlag() bool { return true }
func newTestFlagSet() *flag.FlagSet { func newTestFlagSet() *flag.FlagSet {
fs := flag.NewFlagSet("testwrapper", flag.ContinueOnError) fs := flag.NewFlagSet("testwrapper", flag.ContinueOnError)
fs.SetOutput(io.Discard) fs.SetOutput(io.Discard)
@@ -90,6 +125,8 @@ func newTestFlagSet() *flag.FlagSet {
fs.String("exec", "", "Command to run tests with") fs.String("exec", "", "Command to run tests with")
fs.Bool("race", false, "build with race detector") fs.Bool("race", false, "build with race detector")
fs.String("vet", "", "vet checks to run, or 'off' or 'all'") fs.String("vet", "", "vet checks to run, or 'off' or 'all'")
fs.Var(&cacheLink, "cachelink", "Go -cachelink value (bool); or 'auto' to enable if GOCACHEPROG is set")
return fs return fs
} }