Source file src/cmd/go/scriptconds_test.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main_test
     6  
     7  import (
     8  	"cmd/go/internal/cfg"
     9  	"cmd/internal/script"
    10  	"cmd/internal/script/scripttest"
    11  	"errors"
    12  	"fmt"
    13  	"internal/testenv"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  	"runtime"
    18  	"runtime/debug"
    19  	"sync"
    20  	"testing"
    21  )
    22  
    23  func scriptConditions(t *testing.T) map[string]script.Cond {
    24  	conds := scripttest.DefaultConds()
    25  
    26  	scripttest.AddToolChainScriptConditions(t, conds, goHostOS, goHostArch)
    27  
    28  	add := func(name string, cond script.Cond) {
    29  		if _, ok := conds[name]; ok {
    30  			panic(fmt.Sprintf("condition %q is already registered", name))
    31  		}
    32  		conds[name] = cond
    33  	}
    34  
    35  	lazyBool := func(summary string, f func() bool) script.Cond {
    36  		return script.OnceCondition(summary, func() (bool, error) { return f(), nil })
    37  	}
    38  
    39  	add("abscc", script.Condition("default $CC path is absolute and exists", defaultCCIsAbsolute))
    40  	add("bzr", lazyBool("the 'bzr' executable exists and provides the standard CLI", hasWorkingBzr))
    41  	add("case-sensitive", script.OnceCondition("$WORK filesystem is case-sensitive", isCaseSensitive))
    42  	add("cc", script.PrefixCondition("go env CC = <suffix> (ignoring the go/env file)", ccIs))
    43  	add("git", lazyBool("the 'git' executable exists and provides the standard CLI", hasWorkingGit))
    44  	add("net", script.PrefixCondition("can connect to external network host <suffix>", hasNet))
    45  	add("trimpath", script.OnceCondition("test binary was built with -trimpath", isTrimpath))
    46  
    47  	return conds
    48  }
    49  
    50  func defaultCCIsAbsolute(s *script.State) (bool, error) {
    51  	GOOS, _ := s.LookupEnv("GOOS")
    52  	GOARCH, _ := s.LookupEnv("GOARCH")
    53  	defaultCC := cfg.DefaultCC(GOOS, GOARCH)
    54  	if filepath.IsAbs(defaultCC) {
    55  		if _, err := exec.LookPath(defaultCC); err == nil {
    56  			return true, nil
    57  		}
    58  	}
    59  	return false, nil
    60  }
    61  
    62  func ccIs(s *script.State, want string) (bool, error) {
    63  	CC, _ := s.LookupEnv("CC")
    64  	if CC != "" {
    65  		return CC == want, nil
    66  	}
    67  	GOOS, _ := s.LookupEnv("GOOS")
    68  	GOARCH, _ := s.LookupEnv("GOARCH")
    69  	return cfg.DefaultCC(GOOS, GOARCH) == want, nil
    70  }
    71  
    72  var scriptNetEnabled sync.Map // testing.TB → already enabled
    73  
    74  func hasNet(s *script.State, host string) (bool, error) {
    75  	if !testenv.HasExternalNetwork() {
    76  		return false, nil
    77  	}
    78  
    79  	// TODO(bcmills): Add a flag or environment variable to allow skipping tests
    80  	// for specific hosts and/or skipping all net tests except for specific hosts.
    81  
    82  	t, ok := tbFromContext(s.Context())
    83  	if !ok {
    84  		return false, errors.New("script Context unexpectedly missing testing.TB key")
    85  	}
    86  
    87  	if netTestSem != nil {
    88  		// When the number of external network connections is limited, we limit the
    89  		// number of net tests that can run concurrently so that the overall number
    90  		// of network connections won't exceed the limit.
    91  		_, dup := scriptNetEnabled.LoadOrStore(t, true)
    92  		if !dup {
    93  			// Acquire a net token for this test until the test completes.
    94  			netTestSem <- struct{}{}
    95  			t.Cleanup(func() {
    96  				<-netTestSem
    97  				scriptNetEnabled.Delete(t)
    98  			})
    99  		}
   100  	}
   101  
   102  	// Since we have confirmed that the network is available,
   103  	// allow cmd/go to use it.
   104  	s.Setenv("TESTGONETWORK", "")
   105  	return true, nil
   106  }
   107  
   108  func isCaseSensitive() (bool, error) {
   109  	tmpdir, err := os.MkdirTemp(testTmpDir, "case-sensitive")
   110  	if err != nil {
   111  		return false, fmt.Errorf("failed to create directory to determine case-sensitivity: %w", err)
   112  	}
   113  	defer os.RemoveAll(tmpdir)
   114  
   115  	fcap := filepath.Join(tmpdir, "FILE")
   116  	if err := os.WriteFile(fcap, []byte{}, 0644); err != nil {
   117  		return false, fmt.Errorf("error writing file to determine case-sensitivity: %w", err)
   118  	}
   119  
   120  	flow := filepath.Join(tmpdir, "file")
   121  	_, err = os.ReadFile(flow)
   122  	switch {
   123  	case err == nil:
   124  		return false, nil
   125  	case os.IsNotExist(err):
   126  		return true, nil
   127  	default:
   128  		return false, fmt.Errorf("unexpected error reading file when determining case-sensitivity: %w", err)
   129  	}
   130  }
   131  
   132  func isTrimpath() (bool, error) {
   133  	info, _ := debug.ReadBuildInfo()
   134  	if info == nil {
   135  		return false, errors.New("missing build info")
   136  	}
   137  
   138  	for _, s := range info.Settings {
   139  		if s.Key == "-trimpath" && s.Value == "true" {
   140  			return true, nil
   141  		}
   142  	}
   143  	return false, nil
   144  }
   145  
   146  func hasWorkingGit() bool {
   147  	if runtime.GOOS == "plan9" {
   148  		// The Git command is usually not the real Git on Plan 9.
   149  		// See https://golang.org/issues/29640.
   150  		return false
   151  	}
   152  	_, err := exec.LookPath("git")
   153  	return err == nil
   154  }
   155  
   156  func hasWorkingBzr() bool {
   157  	bzr, err := exec.LookPath("bzr")
   158  	if err != nil {
   159  		return false
   160  	}
   161  	// Check that 'bzr help' exits with code 0.
   162  	// See go.dev/issue/71504 for an example where 'bzr' exists in PATH but doesn't work.
   163  	err = exec.Command(bzr, "help").Run()
   164  	return err == nil
   165  }
   166  

View as plain text