github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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 40 The environment variable $exe (lowercase) is an empty string on most systems, ".exe" on Windows. 41 42 The scripts supporting files are unpacked relative to $GOPATH/src (aka $WORK/gopath/src) 43 and then the script begins execution in that directory as well. Thus the example above runs 44 in $WORK/gopath/src with GOPATH=$WORK/gopath and $WORK/gopath/src/hello.go 45 containing the listed contents. 46 47 The lines at the top of the script are a sequence of commands to be executed 48 by a tiny script engine in ../../script_test.go (not the system shell). 49 The script stops and the overall test fails if any particular command fails. 50 51 Each line is parsed into a sequence of space-separated command words, 52 with environment variable expansion and # marking an end-of-line comment. 53 Adding single quotes around text keeps spaces in that text from being treated 54 as word separators and also disables environment variable expansion. 55 Inside a single-quoted block of text, a repeated single quote indicates 56 a literal single quote, as in: 57 58 'Don''t communicate by sharing memory.' 59 60 A line beginning with # is a comment and conventionally explains what is 61 being done or tested at the start of a new phase in the script. 62 63 The command prefix ! indicates that the command on the rest of the line 64 (typically go or a matching predicate) must fail, not succeed. Only certain 65 commands support this prefix. They are indicated below by [!] in the synopsis. 66 67 The command prefix [cond] indicates that the command on the rest of the line 68 should only run when the condition is satisfied. The available conditions are: 69 70 - GOOS and GOARCH values, like [386], [windows], and so on. 71 - Compiler names, like [gccgo], [gc]. 72 - Test environment details: 73 - [short] for testing.Short() 74 - [cgo], [msan], [race] for whether cgo, msan, and the race detector can be used 75 - [net] for whether the external network can be used 76 - [link] for testenv.HasLink() 77 - [symlink] for testenv.HasSymlink() 78 - [exec:prog] for whether prog is available for execution (found by exec.LookPath) 79 80 A condition can be negated: [!short] means to run the rest of the line 81 when testing.Short() is false. 82 83 The commands are: 84 85 - cd dir 86 Change to the given directory for future commands. 87 88 - cmp file1 file2 89 Check that the named files have the same content. 90 By convention, file1 is the actual data and file2 the expected data. 91 File1 can be "stdout" or "stderr" to use the standard output or standard error 92 from the most recent exec or go command. 93 (If the files have differing content, the failure prints a diff.) 94 95 - cp src... dst 96 Copy the listed files to the target file or existing directory. 97 98 - env [key=value...] 99 With no arguments, print the environment (useful for debugging). 100 Otherwise add the listed key=value pairs to the environment. 101 102 - [!] exec program [args...] 103 Run the given executable program with the arguments. 104 It must (or must not) succeed. 105 Note that 'exec' does not terminate the script (unlike in Unix shells). 106 107 - [!] exists [-readonly] file... 108 Each of the listed files or directories must (or must not) exist. 109 If -readonly is given, the files or directories must be unwritable. 110 111 - [!] go args... 112 Run the (test copy of the) go command with the given arguments. 113 It must (or must not) succeed. 114 115 - [!] grep [-count=N] pattern file 116 The file's content must (or must not) match the regular expression pattern. 117 For positive matches, -count=N specifies an exact number of matches to require. 118 119 - mkdir path... 120 Create the listed directories, if they do not already exists. 121 122 - rm file... 123 Remove the listed files or directories. 124 125 - skip [message] 126 Mark the test skipped, including the message if given. 127 128 - [!] stale path... 129 The packages named by the path arguments must (or must not) 130 be reported as "stale" by the go command. 131 132 - [!] stderr [-count=N] pattern 133 Apply the grep command (see above) to the standard error 134 from the most recent exec or go command. 135 136 - [!] stdout [-count=N] pattern 137 Apply the grep command (see above) to the standard output 138 from the most recent exec or go command. 139 140 - stop [message] 141 Stop the test early (marking it as passing), including the message if given. 142 143 - symlink file -> target 144 Create file as a symlink to target. The -> (like in ls -l output) is required. 145 146 When TestScript runs a script and the script fails, by default TestScript shows 147 the execution of the most recent phase of the script (since the last # comment) 148 and only shows the # comments for earlier phases. For example, here is a 149 multi-phase script with a bug in it: 150 151 # GOPATH with p1 in d2, p2 in d2 152 env GOPATH=$WORK/d1${:}$WORK/d2 153 154 # build & install p1 155 env 156 go install -i p1 157 ! stale p1 158 ! stale p2 159 160 # modify p2 - p1 should appear stale 161 cp $WORK/p2x.go $WORK/d2/src/p2/p2.go 162 stale p1 p2 163 164 # build & install p1 again 165 go install -i p11 166 ! stale p1 167 ! stale p2 168 169 -- $WORK/d1/src/p1/p1.go -- 170 package p1 171 import "p2" 172 func F() { p2.F() } 173 -- $WORK/d2/src/p2/p2.go -- 174 package p2 175 func F() {} 176 -- $WORK/p2x.go -- 177 package p2 178 func F() {} 179 func G() {} 180 181 The bug is that the final phase installs p11 instead of p1. The test failure looks like: 182 183 $ go test -run=Script 184 --- FAIL: TestScript (3.75s) 185 --- FAIL: TestScript/install_rebuild_gopath (0.16s) 186 script_test.go:223: 187 # GOPATH with p1 in d2, p2 in d2 (0.000s) 188 # build & install p1 (0.087s) 189 # modify p2 - p1 should appear stale (0.029s) 190 # build & install p1 again (0.022s) 191 > go install -i p11 192 [stderr] 193 can't load package: package p11: cannot find package "p11" in any of: 194 /Users/rsc/go/src/p11 (from $GOROOT) 195 $WORK/d1/src/p11 (from $GOPATH) 196 $WORK/d2/src/p11 197 [exit status 1] 198 FAIL: unexpected go command failure 199 200 script_test.go:73: failed at testdata/script/install_rebuild_gopath.txt:15 in $WORK/gopath/src 201 202 FAIL 203 exit status 1 204 FAIL cmd/go 4.875s 205 $ 206 207 Note that the commands in earlier phases have been hidden, so that the relevant 208 commands are more easily found, and the elapsed time for a completed phase 209 is shown next to the phase heading. To see the entire execution, use "go test -v", 210 which also adds an initial environment dump to the beginning of the log. 211 212 Note also that in reported output, the actual name of the per-script temporary directory 213 has been consistently replaced with the literal string $WORK. 214 215 The cmd/go test flag -testwork (which must appear on the "go test" command line after 216 standard test flags) causes each test to log the name of its $WORK directory and other 217 environment variable settings and also to leave that directory behind when it exits, 218 for manual debugging of failing tests: 219 220 $ go test -run=Script -work 221 --- FAIL: TestScript (3.75s) 222 --- FAIL: TestScript/install_rebuild_gopath (0.16s) 223 script_test.go:223: 224 WORK=/tmp/cmd-go-test-745953508/script-install_rebuild_gopath 225 GOARCH= 226 GOCACHE=/Users/rsc/Library/Caches/go-build 227 GOOS= 228 GOPATH=$WORK/gopath 229 GOROOT=/Users/rsc/go 230 HOME=/no-home 231 TMPDIR=$WORK/tmp 232 exe= 233 234 # GOPATH with p1 in d2, p2 in d2 (0.000s) 235 # build & install p1 (0.085s) 236 # modify p2 - p1 should appear stale (0.030s) 237 # build & install p1 again (0.019s) 238 > go install -i p11 239 [stderr] 240 can't load package: package p11: cannot find package "p11" in any of: 241 /Users/rsc/go/src/p11 (from $GOROOT) 242 $WORK/d1/src/p11 (from $GOPATH) 243 $WORK/d2/src/p11 244 [exit status 1] 245 FAIL: unexpected go command failure 246 247 script_test.go:73: failed at testdata/script/install_rebuild_gopath.txt:15 in $WORK/gopath/src 248 249 FAIL 250 exit status 1 251 FAIL cmd/go 4.875s 252 $ 253 254 $ WORK=/tmp/cmd-go-test-745953508/script-install_rebuild_gopath 255 $ cd $WORK/d1/src/p1 256 $ cat p1.go 257 package p1 258 import "p2" 259 func F() { p2.F() } 260 $ 261