github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/go/testdata/script/README (about) 1 This directory holds test scripts *.txt run during 'go test cmd/go'. 2 To run a specific script foo.txt 3 4 go test cmd/go -run=Script/^foo$ 5 6 In general script files should have short names: a few words, not whole sentences. 7 The first word should be the general category of behavior being tested, 8 often the name of a go subcommand (list, build, test, ...) or concept (vendor, pattern). 9 10 Each script is a text archive (go doc cmd/go/internal/txtar). 11 The script begins with an actual command script to run 12 followed by the content of zero or more supporting files to 13 create in the script's temporary file system before it starts executing. 14 15 As an example, run_hello.txt says: 16 17 # hello world 18 go run hello.go 19 stderr 'hello world' 20 ! stdout . 21 22 -- hello.go -- 23 package main 24 func main() { println("hello world") } 25 26 Each script runs in a fresh temporary work directory tree, available to scripts as $WORK. 27 Scripts also have access to these other environment variables: 28 29 GOARCH=<target GOARCH> 30 GOCACHE=<actual GOCACHE being used outside the test> 31 GOOS=<target GOOS> 32 GOPATH=$WORK/gopath 33 GOPROXY=<local module proxy serving from cmd/go/testdata/mod> 34 GOROOT=<actual GOROOT> 35 HOME=/no-home 36 PATH=<actual PATH> 37 TMPDIR=$WORK/tmp 38 devnull=<value of os.DevNull> 39 goversion=<current Go version; for example, 1.12> 40 41 The environment variable $exe (lowercase) is an empty string on most systems, ".exe" on Windows. 42 43 The scripts supporting files are unpacked relative to $GOPATH/src (aka $WORK/gopath/src) 44 and then the script begins execution in that directory as well. Thus the example above runs 45 in $WORK/gopath/src with GOPATH=$WORK/gopath and $WORK/gopath/src/hello.go 46 containing the listed contents. 47 48 The lines at the top of the script are a sequence of commands to be executed 49 by a tiny script engine in ../../script_test.go (not the system shell). 50 The script stops and the overall test fails if any particular command fails. 51 52 Each line is parsed into a sequence of space-separated command words, 53 with environment variable expansion and # marking an end-of-line comment. 54 Adding single quotes around text keeps spaces in that text from being treated 55 as word separators and also disables environment variable expansion. 56 Inside a single-quoted block of text, a repeated single quote indicates 57 a literal single quote, as in: 58 59 'Don''t communicate by sharing memory.' 60 61 A line beginning with # is a comment and conventionally explains what is 62 being done or tested at the start of a new phase in the script. 63 64 The command prefix ! indicates that the command on the rest of the line 65 (typically go or a matching predicate) must fail, not succeed. Only certain 66 commands support this prefix. They are indicated below by [!] in the synopsis. 67 68 The command prefix [cond] indicates that the command on the rest of the line 69 should only run when the condition is satisfied. The available conditions are: 70 71 - GOOS and GOARCH values, like [386], [windows], and so on. 72 - Compiler names, like [gccgo], [gc]. 73 - Test environment details: 74 - [short] for testing.Short() 75 - [cgo], [msan], [race] for whether cgo, msan, and the race detector can be used 76 - [net] for whether the external network can be used 77 - [link] for testenv.HasLink() 78 - [root] for os.Geteuid() == 0 79 - [symlink] for testenv.HasSymlink() 80 - [exec:prog] for whether prog is available for execution (found by exec.LookPath) 81 82 A condition can be negated: [!short] means to run the rest of the line 83 when testing.Short() is false. 84 85 The commands are: 86 87 - [!] cc args... [&] 88 Run the C compiler, the platform specific flags (i.e. `go env GOGCCFLAGS`) will be 89 added automatically before args. 90 91 - cd dir 92 Change to the given directory for future commands. 93 94 - chmod perm path... 95 Change the permissions of the files or directories named by the path arguments 96 to be equal to perm. Only numerical permissions are supported. 97 98 - cmp file1 file2 99 Check that the named files have the same content. 100 By convention, file1 is the actual data and file2 the expected data. 101 File1 can be "stdout" or "stderr" to use the standard output or standard error 102 from the most recent exec or go command. 103 (If the files have differing content, the failure prints a diff.) 104 105 - cmpenv file1 file2 106 Like cmp, but environment variables are substituted in the file contents 107 before the comparison. For example, $GOOS is replaced by the target GOOS. 108 109 - cp src... dst 110 Copy the listed files to the target file or existing directory. 111 112 - env [key=value...] 113 With no arguments, print the environment (useful for debugging). 114 Otherwise add the listed key=value pairs to the environment. 115 116 - [!] exec program [args...] [&] 117 Run the given executable program with the arguments. 118 It must (or must not) succeed. 119 Note that 'exec' does not terminate the script (unlike in Unix shells). 120 121 If the last token is '&', the program executes in the background. The standard 122 output and standard error of the previous command is cleared, but the output 123 of the background process is buffered — and checking of its exit status is 124 delayed — until the next call to 'wait', 'skip', or 'stop' or the end of the 125 test. At the end of the test, any remaining background processes are 126 terminated using os.Interrupt (if supported) or os.Kill. 127 128 - [!] exists [-readonly] file... 129 Each of the listed files or directories must (or must not) exist. 130 If -readonly is given, the files or directories must be unwritable. 131 132 - [!] go args... [&] 133 Run the (test copy of the) go command with the given arguments. 134 It must (or must not) succeed. 135 136 - [!] grep [-count=N] pattern file 137 The file's content must (or must not) match the regular expression pattern. 138 For positive matches, -count=N specifies an exact number of matches to require. 139 140 - mkdir path... 141 Create the listed directories, if they do not already exists. 142 143 - rm file... 144 Remove the listed files or directories. 145 146 - skip [message] 147 Mark the test skipped, including the message if given. 148 149 - [!] stale path... 150 The packages named by the path arguments must (or must not) 151 be reported as "stale" by the go command. 152 153 - [!] stderr [-count=N] pattern 154 Apply the grep command (see above) to the standard error 155 from the most recent exec, go, or wait command. 156 157 - [!] stdout [-count=N] pattern 158 Apply the grep command (see above) to the standard output 159 from the most recent exec, go, or wait command. 160 161 - stop [message] 162 Stop the test early (marking it as passing), including the message if given. 163 164 - symlink file -> target 165 Create file as a symlink to target. The -> (like in ls -l output) is required. 166 167 - wait 168 Wait for all 'exec' and 'go' commands started in the background (with the '&' 169 token) to exit, and display success or failure status for them. 170 After a call to wait, the 'stderr' and 'stdout' commands will apply to the 171 concatenation of the corresponding streams of the background commands, 172 in the order in which those commands were started. 173 174 When TestScript runs a script and the script fails, by default TestScript shows 175 the execution of the most recent phase of the script (since the last # comment) 176 and only shows the # comments for earlier phases. For example, here is a 177 multi-phase script with a bug in it: 178 179 # GOPATH with p1 in d2, p2 in d2 180 env GOPATH=$WORK/d1${:}$WORK/d2 181 182 # build & install p1 183 env 184 go install -i p1 185 ! stale p1 186 ! stale p2 187 188 # modify p2 - p1 should appear stale 189 cp $WORK/p2x.go $WORK/d2/src/p2/p2.go 190 stale p1 p2 191 192 # build & install p1 again 193 go install -i p11 194 ! stale p1 195 ! stale p2 196 197 -- $WORK/d1/src/p1/p1.go -- 198 package p1 199 import "p2" 200 func F() { p2.F() } 201 -- $WORK/d2/src/p2/p2.go -- 202 package p2 203 func F() {} 204 -- $WORK/p2x.go -- 205 package p2 206 func F() {} 207 func G() {} 208 209 The bug is that the final phase installs p11 instead of p1. The test failure looks like: 210 211 $ go test -run=Script 212 --- FAIL: TestScript (3.75s) 213 --- FAIL: TestScript/install_rebuild_gopath (0.16s) 214 script_test.go:223: 215 # GOPATH with p1 in d2, p2 in d2 (0.000s) 216 # build & install p1 (0.087s) 217 # modify p2 - p1 should appear stale (0.029s) 218 # build & install p1 again (0.022s) 219 > go install -i p11 220 [stderr] 221 can't load package: package p11: cannot find package "p11" in any of: 222 /Users/rsc/go/src/p11 (from $GOROOT) 223 $WORK/d1/src/p11 (from $GOPATH) 224 $WORK/d2/src/p11 225 [exit status 1] 226 FAIL: unexpected go command failure 227 228 script_test.go:73: failed at testdata/script/install_rebuild_gopath.txt:15 in $WORK/gopath/src 229 230 FAIL 231 exit status 1 232 FAIL cmd/go 4.875s 233 $ 234 235 Note that the commands in earlier phases have been hidden, so that the relevant 236 commands are more easily found, and the elapsed time for a completed phase 237 is shown next to the phase heading. To see the entire execution, use "go test -v", 238 which also adds an initial environment dump to the beginning of the log. 239 240 Note also that in reported output, the actual name of the per-script temporary directory 241 has been consistently replaced with the literal string $WORK. 242 243 The cmd/go test flag -testwork (which must appear on the "go test" command line after 244 standard test flags) causes each test to log the name of its $WORK directory and other 245 environment variable settings and also to leave that directory behind when it exits, 246 for manual debugging of failing tests: 247 248 $ go test -run=Script -work 249 --- FAIL: TestScript (3.75s) 250 --- FAIL: TestScript/install_rebuild_gopath (0.16s) 251 script_test.go:223: 252 WORK=/tmp/cmd-go-test-745953508/script-install_rebuild_gopath 253 GOARCH= 254 GOCACHE=/Users/rsc/Library/Caches/go-build 255 GOOS= 256 GOPATH=$WORK/gopath 257 GOROOT=/Users/rsc/go 258 HOME=/no-home 259 TMPDIR=$WORK/tmp 260 exe= 261 262 # GOPATH with p1 in d2, p2 in d2 (0.000s) 263 # build & install p1 (0.085s) 264 # modify p2 - p1 should appear stale (0.030s) 265 # build & install p1 again (0.019s) 266 > go install -i p11 267 [stderr] 268 can't load package: package p11: cannot find package "p11" in any of: 269 /Users/rsc/go/src/p11 (from $GOROOT) 270 $WORK/d1/src/p11 (from $GOPATH) 271 $WORK/d2/src/p11 272 [exit status 1] 273 FAIL: unexpected go command failure 274 275 script_test.go:73: failed at testdata/script/install_rebuild_gopath.txt:15 in $WORK/gopath/src 276 277 FAIL 278 exit status 1 279 FAIL cmd/go 4.875s 280 $ 281 282 $ WORK=/tmp/cmd-go-test-745953508/script-install_rebuild_gopath 283 $ cd $WORK/d1/src/p1 284 $ cat p1.go 285 package p1 286 import "p2" 287 func F() { p2.F() } 288 $ 289