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