Source file src/cmd/internal/script/scripttest/run.go

     1  // Copyright 2022 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 scripttest adapts the script engine for use in tests.
     6  package scripttest
     7  
     8  import (
     9  	"bytes"
    10  	"cmd/internal/script"
    11  	"context"
    12  	"fmt"
    13  	"internal/testenv"
    14  	"internal/txtar"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  	"runtime"
    19  	"strings"
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  // ToolReplacement records the name of a tool to replace
    25  // within a given GOROOT for script testing purposes.
    26  type ToolReplacement struct {
    27  	ToolName        string // e.g. compile, link, addr2line, etc
    28  	ReplacementPath string // path to replacement tool exe
    29  	EnvVar          string // env var setting (e.g. "FOO=BAR")
    30  }
    31  
    32  // NewEngine constructs a new [script.Engine] and environment to be used with
    33  // [RunTests].
    34  func NewEngine(t *testing.T, repls []ToolReplacement) (*script.Engine, []string) {
    35  	// Nearly all script tests involve doing builds, so don't
    36  	// bother here if we don't have "go build".
    37  	testenv.MustHaveGoBuild(t)
    38  
    39  	// Skip this path on plan9, which doesn't support symbolic
    40  	// links (we would have to copy too much).
    41  	if runtime.GOOS == "plan9" {
    42  		t.Skipf("no symlinks on plan9")
    43  	}
    44  
    45  	// Locate our Go tool.
    46  	gotool, err := testenv.GoTool()
    47  	if err != nil {
    48  		t.Fatalf("locating go tool: %v", err)
    49  	}
    50  
    51  	goEnv := func(name string) string {
    52  		out, err := exec.Command(gotool, "env", name).CombinedOutput()
    53  		if err != nil {
    54  			t.Fatalf("go env %s: %v\n%s", name, err, out)
    55  		}
    56  		return strings.TrimSpace(string(out))
    57  	}
    58  
    59  	// Construct an initial set of commands + conditions to make available
    60  	// to the script tests.
    61  	cmds := DefaultCmds()
    62  	conds := DefaultConds()
    63  
    64  	addcmd := func(name string, cmd script.Cmd) {
    65  		if _, ok := cmds[name]; ok {
    66  			panic(fmt.Sprintf("command %q is already registered", name))
    67  		}
    68  		cmds[name] = cmd
    69  	}
    70  
    71  	prependToPath := func(env []string, dir string) {
    72  		found := false
    73  		for k := range env {
    74  			ev := env[k]
    75  			if !strings.HasPrefix(ev, "PATH=") {
    76  				continue
    77  			}
    78  			oldpath := ev[5:]
    79  			env[k] = "PATH=" + dir + string(filepath.ListSeparator) + oldpath
    80  			found = true
    81  			break
    82  		}
    83  		if !found {
    84  			t.Fatalf("could not update PATH")
    85  		}
    86  	}
    87  
    88  	setenv := func(env []string, varname, val string) []string {
    89  		pref := varname + "="
    90  		found := false
    91  		for k := range env {
    92  			if !strings.HasPrefix(env[k], pref) {
    93  				continue
    94  			}
    95  			env[k] = pref + val
    96  			found = true
    97  			break
    98  		}
    99  		if !found {
   100  			env = append(env, varname+"="+val)
   101  		}
   102  		return env
   103  	}
   104  
   105  	interrupt := func(cmd *exec.Cmd) error {
   106  		return cmd.Process.Signal(os.Interrupt)
   107  	}
   108  	gracePeriod := 60 * time.Second // arbitrary
   109  
   110  	// Set up an alternate go root for running script tests, since it
   111  	// is possible that we might want to replace one of the installed
   112  	// tools with a unit test executable.
   113  	goroot := goEnv("GOROOT")
   114  	tmpdir := t.TempDir()
   115  	tgr := SetupTestGoRoot(t, tmpdir, goroot)
   116  
   117  	// Replace tools if appropriate
   118  	for _, repl := range repls {
   119  		ReplaceGoToolInTestGoRoot(t, tgr, repl.ToolName, repl.ReplacementPath)
   120  	}
   121  
   122  	// Add in commands for "go" and "cc".
   123  	testgo := filepath.Join(tgr, "bin", "go")
   124  	gocmd := script.Program(testgo, interrupt, gracePeriod)
   125  	addcmd("go", gocmd)
   126  	cmdExec := cmds["exec"]
   127  	addcmd("cc", scriptCC(cmdExec, goEnv("CC")))
   128  
   129  	// Add various helpful conditions related to builds and toolchain use.
   130  	goHostOS, goHostArch := goEnv("GOHOSTOS"), goEnv("GOHOSTARCH")
   131  	AddToolChainScriptConditions(t, conds, goHostOS, goHostArch)
   132  
   133  	// Environment setup.
   134  	env := os.Environ()
   135  	prependToPath(env, filepath.Join(tgr, "bin"))
   136  	env = setenv(env, "GOROOT", tgr)
   137  	// GOOS and GOARCH are expected to be set by the toolchain script conditions.
   138  	env = setenv(env, "GOOS", runtime.GOOS)
   139  	env = setenv(env, "GOARCH", runtime.GOARCH)
   140  	for _, repl := range repls {
   141  		// consistency check
   142  		chunks := strings.Split(repl.EnvVar, "=")
   143  		if len(chunks) != 2 {
   144  			t.Fatalf("malformed env var setting: %s", repl.EnvVar)
   145  		}
   146  		env = append(env, repl.EnvVar)
   147  	}
   148  
   149  	// Manufacture engine...
   150  	engine := &script.Engine{
   151  		Conds: conds,
   152  		Cmds:  cmds,
   153  		Quiet: !testing.Verbose(),
   154  	}
   155  
   156  	return engine, env
   157  }
   158  
   159  // RunToolScriptTest kicks off a set of script tests runs for
   160  // a tool of some sort (compiler, linker, etc). The expectation
   161  // is that we'll be called from the top level cmd/X dir for tool X,
   162  // and that instead of executing the install tool X we'll use the
   163  // test binary instead.
   164  func RunToolScriptTest(t *testing.T, repls []ToolReplacement, scriptsdir string, fixReadme bool) {
   165  	// Locate our Go tool.
   166  	gotool, err := testenv.GoTool()
   167  	if err != nil {
   168  		t.Fatalf("locating go tool: %v", err)
   169  	}
   170  
   171  	engine, env := NewEngine(t, repls)
   172  
   173  	t.Run("README", func(t *testing.T) {
   174  		checkScriptReadme(t, engine, env, scriptsdir, gotool, fixReadme)
   175  	})
   176  
   177  	// ... and kick off tests.
   178  	ctx := context.Background()
   179  	pattern := filepath.Join(scriptsdir, "*.txt")
   180  	RunTests(t, ctx, engine, env, pattern)
   181  }
   182  
   183  // ScriptTestContext returns a context with a grace period for cleaning up
   184  // subprocesses of a script test.
   185  //
   186  // When we run commands that execute subprocesses, we want to reserve two grace
   187  // periods to clean up. We will send the first termination signal when the
   188  // context expires, then wait one grace period for the process to produce
   189  // whatever useful output it can (such as a stack trace). After the first grace
   190  // period expires, we'll escalate to os.Kill, leaving the second grace period
   191  // for the test function to record its output before the test process itself
   192  // terminates.
   193  //
   194  // The grace period is 100ms or 5% of the time remaining until
   195  // [testing.T.Deadline], whichever is greater.
   196  func ScriptTestContext(t *testing.T, ctx context.Context) context.Context {
   197  	deadline, ok := t.Deadline()
   198  	if !ok {
   199  		return ctx
   200  	}
   201  
   202  	gracePeriod := 100 * time.Millisecond
   203  	timeout := time.Until(deadline)
   204  
   205  	// If time allows, increase the termination grace period to 5% of the
   206  	// remaining time.
   207  	gracePeriod = max(gracePeriod, timeout/20)
   208  
   209  	// Reserve two grace periods to clean up
   210  	timeout -= 2 * gracePeriod
   211  
   212  	ctx, cancel := context.WithTimeout(ctx, timeout)
   213  	t.Cleanup(cancel)
   214  	return ctx
   215  }
   216  
   217  // RunTests kicks off one or more script-based tests using the
   218  // specified engine, running all test files that match pattern.
   219  // This function adapted from Russ's rsc.io/script/scripttest#Run
   220  // function, which was in turn forked off cmd/go's runner.
   221  func RunTests(t *testing.T, ctx context.Context, engine *script.Engine, env []string, pattern string) {
   222  	ctx = ScriptTestContext(t, ctx)
   223  
   224  	files, _ := filepath.Glob(pattern)
   225  	if len(files) == 0 {
   226  		t.Fatal("no testdata")
   227  	}
   228  	for _, file := range files {
   229  		file := file
   230  		name := strings.TrimSuffix(filepath.Base(file), ".txt")
   231  		t.Run(name, func(t *testing.T) {
   232  			t.Parallel()
   233  
   234  			workdir := t.TempDir()
   235  			s, err := script.NewState(ctx, workdir, env)
   236  			if err != nil {
   237  				t.Fatal(err)
   238  			}
   239  
   240  			// Unpack archive.
   241  			a, err := txtar.ParseFile(file)
   242  			if err != nil {
   243  				t.Fatal(err)
   244  			}
   245  			InitScriptDirs(t, s)
   246  			if err := s.ExtractFiles(a); err != nil {
   247  				t.Fatal(err)
   248  			}
   249  
   250  			t.Log(time.Now().UTC().Format(time.RFC3339))
   251  			work, _ := s.LookupEnv("WORK")
   252  			t.Logf("$WORK=%s", work)
   253  
   254  			// Note: Do not use filepath.Base(file) here:
   255  			// editors that can jump to file:line references in the output
   256  			// will work better seeing the full path relative to the
   257  			// directory containing the command being tested
   258  			// (e.g. where "go test" command is usually run).
   259  			Run(t, engine, s, file, bytes.NewReader(a.Comment))
   260  		})
   261  	}
   262  }
   263  
   264  // InitScriptDirs sets up directories for executing a script test.
   265  //
   266  //   - WORK (env var) is set to the current working directory.
   267  //   - TMPDIR (env var; TMP on Windows) is set to $WORK/tmp.
   268  //   - $TMPDIR is created.
   269  func InitScriptDirs(t testing.TB, s *script.State) {
   270  	must := func(err error) {
   271  		if err != nil {
   272  			t.Helper()
   273  			t.Fatal(err)
   274  		}
   275  	}
   276  
   277  	work := s.Getwd()
   278  	must(s.Setenv("WORK", work))
   279  	must(os.MkdirAll(filepath.Join(work, "tmp"), 0777))
   280  	must(s.Setenv(tempEnvName(), filepath.Join(work, "tmp")))
   281  }
   282  
   283  func tempEnvName() string {
   284  	switch runtime.GOOS {
   285  	case "windows":
   286  		return "TMP"
   287  	case "plan9":
   288  		return "TMPDIR" // actually plan 9 doesn't have one at all but this is fine
   289  	default:
   290  		return "TMPDIR"
   291  	}
   292  }
   293  
   294  // scriptCC runs the platform C compiler.
   295  func scriptCC(cmdExec script.Cmd, ccexe string) script.Cmd {
   296  	return script.Command(
   297  		script.CmdUsage{
   298  			Summary: "run the platform C compiler",
   299  			Args:    "args...",
   300  		},
   301  		func(s *script.State, args ...string) (script.WaitFunc, error) {
   302  			return cmdExec.Run(s, append([]string{ccexe}, args...)...)
   303  		})
   304  }
   305  

View as plain text