Source file src/cmd/go/scriptcmds_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/script"
     9  	"cmd/go/internal/script/scripttest"
    10  	"cmd/go/internal/work"
    11  	"errors"
    12  	"fmt"
    13  	"os"
    14  	"os/exec"
    15  	"strings"
    16  	"time"
    17  )
    18  
    19  func scriptCommands(interrupt os.Signal, waitDelay time.Duration) map[string]script.Cmd {
    20  	cmds := scripttest.DefaultCmds()
    21  
    22  	// Customize the "exec" interrupt signal and grace period.
    23  	var cancel func(cmd *exec.Cmd) error
    24  	if interrupt != nil {
    25  		cancel = func(cmd *exec.Cmd) error {
    26  			return cmd.Process.Signal(interrupt)
    27  		}
    28  	}
    29  
    30  	cmdExec := script.Exec(cancel, waitDelay)
    31  	cmds["exec"] = cmdExec
    32  
    33  	add := func(name string, cmd script.Cmd) {
    34  		if _, ok := cmds[name]; ok {
    35  			panic(fmt.Sprintf("command %q is already registered", name))
    36  		}
    37  		cmds[name] = cmd
    38  	}
    39  
    40  	add("cc", scriptCC(cmdExec))
    41  	cmdGo := scriptGo(cancel, waitDelay)
    42  	add("go", cmdGo)
    43  	add("stale", scriptStale(cmdGo))
    44  
    45  	return cmds
    46  }
    47  
    48  // scriptCC runs the C compiler along with platform specific options.
    49  func scriptCC(cmdExec script.Cmd) script.Cmd {
    50  	return script.Command(
    51  		script.CmdUsage{
    52  			Summary: "run the platform C compiler",
    53  			Args:    "args...",
    54  		},
    55  		func(s *script.State, args ...string) (script.WaitFunc, error) {
    56  			b := work.NewBuilder(s.Getwd())
    57  			wait, err := cmdExec.Run(s, append(b.GccCmd(".", ""), args...)...)
    58  			if err != nil {
    59  				return wait, err
    60  			}
    61  			waitAndClean := func(s *script.State) (stdout, stderr string, err error) {
    62  				stdout, stderr, err = wait(s)
    63  				if closeErr := b.Close(); err == nil {
    64  					err = closeErr
    65  				}
    66  				return stdout, stderr, err
    67  			}
    68  			return waitAndClean, nil
    69  		})
    70  }
    71  
    72  // scriptGo runs the go command.
    73  func scriptGo(cancel func(*exec.Cmd) error, waitDelay time.Duration) script.Cmd {
    74  	return script.Program(testGo, cancel, waitDelay)
    75  }
    76  
    77  // scriptStale checks that the named build targets are stale.
    78  func scriptStale(cmdGo script.Cmd) script.Cmd {
    79  	return script.Command(
    80  		script.CmdUsage{
    81  			Summary: "check that build targets are stale",
    82  			Args:    "target...",
    83  		},
    84  		func(s *script.State, args ...string) (script.WaitFunc, error) {
    85  			if len(args) == 0 {
    86  				return nil, script.ErrUsage
    87  			}
    88  			tmpl := "{{if .Error}}{{.ImportPath}}: {{.Error.Err}}" +
    89  				"{{else}}{{if not .Stale}}{{.ImportPath}} ({{.Target}}) is not stale{{end}}" +
    90  				"{{end}}"
    91  
    92  			wait, err := cmdGo.Run(s, append([]string{"list", "-e", "-f=" + tmpl}, args...)...)
    93  			if err != nil {
    94  				return nil, err
    95  			}
    96  
    97  			stdout, stderr, err := wait(s)
    98  			if len(stderr) != 0 {
    99  				s.Logf("%s", stderr)
   100  			}
   101  			if err != nil {
   102  				return nil, err
   103  			}
   104  			if out := strings.TrimSpace(stdout); out != "" {
   105  				return nil, errors.New(out)
   106  			}
   107  			return nil, nil
   108  		})
   109  }
   110  

View as plain text