Debian Codegraph @

testscript/doc.go

raw symbols SCIP
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 /*
6 Package testscript provides support for defining filesystem-based tests by
7 creating scripts in a directory.
8
9 To invoke the tests, call testscript.Run. For example:
10
11 func TestFoo(t *testing.T) {
12 testscript.Run(t, testscript.Params{
13 Dir: "testdata",
14 })
15 }
16
17 A testscript directory holds test scripts with extension txtar or txt run during 'go test'.
18 Each script defines a subtest; the exact set of allowable commands in a
19 script are defined by the parameters passed to the Run function.
20 To run a specific script foo.txtar or foo.txt, run
21
22 go test cmd/go -run=TestName/^foo$
23
24 where TestName is the name of the test that Run is called from.
25
26 To define an executable command (or several) that can be run as part of the script,
27 call Main with the functions that implement the command's functionality.
28 The command functions will be called in a separate process, so are
29 free to mutate global variables without polluting the top level test binary.
30
31 func TestMain(m *testing.M) {
32 testscript.Main(m, map[string] func() {
33 "testscript": testscriptMain,
34 })
35 }
36
37 In general script files should have short names: a few words, not whole sentences.
38 The first word should be the general category of behavior being tested,
39 often the name of a subcommand to be tested or a concept (vendor, pattern).
40
41 Each script is a text archive (go doc golang.org/x/tools/txtar).
42 The script begins with an actual command script to run
43 followed by the content of zero or more supporting files to
44 create in the script's temporary file system before it starts executing.
45
46 As an example:
47
48 # hello world
49 exec cat hello.text
50 stdout 'hello world\n'
51 ! stderr .
52
53 -- hello.text --
54 hello world
55
56 Each script runs in a fresh temporary work directory tree, available to scripts as $WORK.
57 Scripts also have access to these other environment variables:
58
59 PATH=<actual PATH>
60 HOME=/no-home (USERPROFILE on windows, home on plan9)
61 TMPDIR=$WORK/.tmp (TMP on windows)
62 devnull=<value of os.DevNull>
63 /=<value of os.PathSeparator>
64 :=<value of os.PathListSeparator>
65 $=$
66
67 The environment variable $exe (lowercase) is an empty string on most
68 systems, ".exe" on Windows.
69
70 The script's supporting files are unpacked relative to $WORK
71 and then the script begins execution in that
72 directory as well. Thus the example above runs in $WORK
73 with $WORK/hello.txtar containing the listed contents.
74
75 The lines at the top of the script are a sequence of commands to be
76 executed by a small script engine in the testscript package (not the system
77 shell). The script stops and the overall test fails if any particular
78 command fails.
79
80 Each line is parsed into a sequence of space-separated command words,
81 with environment variable expansion and # marking an end-of-line comment.
82 Adding single quotes around text keeps spaces in that text from being
83 treated as word separators and also disables environment variable
84 expansion. Inside a single-quoted block of text, a repeated single
85 quote indicates a literal single quote, as in:
86
87 'Don''t communicate by sharing memory.'
88
89 A line beginning with # is a comment and conventionally explains what is
90 being done or tested at the start of a new phase in the script.
91
92 A special form of environment variable syntax can be used to quote
93 regexp metacharacters inside environment variables. The "@R" suffix
94 is special, and indicates that the variable should be quoted.
95
96 ${VAR@R}
97
98 The command prefix ! indicates that the command on the rest of the line
99 (typically go or a matching predicate) must fail, not succeed. Only certain
100 commands support this prefix. They are indicated below by [!] in the synopsis.
101
102 The command prefix [cond] indicates that the command on the rest of the line
103 should only run when the condition is satisfied. The predefined conditions are:
104
105 - [short] for testing.Short()
106 - [net] for whether the external network can be used
107 - [link] for whether the OS has hard link support
108 - [symlink] for whether the OS has symbolic link support
109 - [exec:prog] for whether prog is available for execution (found by exec.LookPath)
110 - [gc] for whether Go was built with gc
111 - [gccgo] for whether Go was built with gccgo
112 - [go1.x] for whether the Go version is 1.x or later
113 - [unix] for whether the OS is Unix-like (that is, would match the 'unix' build
114 constraint)
115
116 Any known values of GOOS and GOARCH can also be used as conditions. They will be
117 satisfied if the target OS or architecture match the specified value. For example,
118 the condition [darwin] is true if GOOS=darwin, and [amd64] is true if GOARCH=amd64.
119
120 A condition can be negated: [!short] means to run the rest of the line
121 when testing.Short() is false.
122
123 Additional conditions can be added by passing a function to Params.Condition.
124
125 The predefined commands are:
126
127 - cd dir
128 Change to the given directory for future commands.
129
130 - chmod perm path...
131 Change the permissions of the files or directories named by the path arguments
132 to the given octal mode (000 to 777).
133
134 - [!] cmp file1 file2
135 Check that the named files have (or do not have) the same content.
136 By convention, file1 is the actual data and file2 the expected data.
137 File1 can be "stdout" or "stderr" to use the standard output or standard error
138 from the most recent exec or wait command.
139 (If the files have differing content and the command is not negated,
140 the failure prints a diff.)
141
142 - [!] cmpenv file1 file2
143 Like cmp, but environment variables in file2 are substituted before the
144 comparison. For example, $GOOS is replaced by the target GOOS.
145
146 - cp src... dst
147 Copy the listed files to the target file or existing directory.
148 src can include "stdout" or "stderr" to use the standard output or standard error
149 from the most recent exec or go command.
150
151 - env [key=value...]
152 With no arguments, print the environment (useful for debugging).
153 Otherwise add the listed key=value pairs to the environment.
154
155 - [!] exec program [args...] [&]
156 Run the given executable program with the arguments.
157 It must (or must not) succeed.
158 Note that 'exec' does not terminate the script (unlike in Unix shells).
159
160 If the last token is '&', the program executes in the background. The standard
161 output and standard error of the previous command is cleared, but the output
162 of the background process is buffered — and checking of its exit status is
163 delayed — until the next call to 'wait', 'skip', or 'stop' or the end of the
164 test. At the end of the test, any remaining background processes are
165 terminated using os.Interrupt (if supported) or os.Kill.
166
167 If the last token is '&word&` (where "word" is alphanumeric), the
168 command runs in the background but has a name, and can be waited
169 for specifically by passing the word to 'wait', or used to terminate
170 the process by invoking 'kill' with the word passed to it.
171
172 Standard input can be provided using the stdin command; this will be
173 cleared after exec has been called.
174
175 - [!] exists [-readonly] file...
176 Each of the listed files or directories must (or must not) exist.
177 If -readonly is given, the files or directories must be unwritable.
178
179 - [!] grep [-count=N] pattern file
180 The file's content must (or must not) match the regular expression pattern.
181 For positive matches, -count=N specifies an exact number of matches to require.
182
183 - kill [-SIGNAL] [command]
184 Terminate all 'exec' and 'go' commands started in the background (with the '&'
185 token) by sending an termination signal. Recognized signals are KILL and INT.
186 If no signal is specified, KILL is sent.
187
188 If a command argument is specified, it terminates only that command, which
189 must have been started with the final token '&command&` as described for the
190 exec command.
191
192 - mkdir path...
193 Create the listed directories, if they do not already exists.
194
195 - mv path1 path2
196 Rename path1 to path2. OS-specific restrictions may apply when path1 and path2
197 are in different directories.
198
199 - rm file...
200 Remove the listed files or directories.
201
202 - skip [message]
203 Mark the test skipped, including the message if given.
204
205 - [!] stderr [-count=N] pattern
206 Apply the grep command (see above) to the standard error
207 from the most recent exec or wait command.
208
209 - stdin file
210 Set the standard input for the next exec command to the contents of the given file.
211 File can be "stdout" or "stderr" to use the standard output or standard error
212 from the most recent exec or wait command.
213
214 - [!] stdout [-count=N] pattern
215 Apply the grep command (see above) to the standard output
216 from the most recent exec or wait command.
217
218 - ttyin [-stdin] file
219 Attach the next exec command to a controlling pseudo-terminal, and use the
220 contents of the given file as the raw terminal input. If -stdin is specified,
221 also attach the terminal to standard input.
222 Note that this does not attach the terminal to standard output/error.
223
224 - [!] ttyout [-count=N] pattern
225 Apply the grep command (see above) to the raw controlling terminal output
226 from the most recent exec command.
227
228 - stop [message]
229 Stop the test early (marking it as passing), including the message if given.
230
231 - symlink file -> target
232 Create file as a symlink to target. The -> (like in ls -l output) is required.
233
234 - unquote file...
235 Rewrite each file by replacing any leading ">" characters from
236 each line. This enables a file to contain substrings that look like
237 txtar file markers.
238 See also https://godoc.org/github.com/rogpeppe/go-internal/txtar#Unquote
239
240 - wait [command]
241 Wait for all 'exec' and 'go' commands started in the background (with the '&'
242 token) to exit, and display success or failure status for them.
243 After a call to wait, the 'stderr' and 'stdout' commands will apply to the
244 concatenation of the corresponding streams of the background commands,
245 in the order in which those commands were started.
246
247 If an argument is specified, it waits for just that command, which
248 must have been started with the final token '&command&` as described for the
249 exec command.
250
251 When TestScript runs a script and the script fails, by default TestScript shows
252 the execution of the most recent phase of the script (since the last # comment)
253 and only shows the # comments for earlier phases. For example, here is a
254 multi-phase script with a bug in it (TODO: make this example less go-command
255 specific):
256
257 # GOPATH with p1 in d2, p2 in d2
258 env GOPATH=$WORK/d1${:}$WORK/d2
259
260 # build & install p1
261 env
262 go install -i p1
263 ! stale p1
264 ! stale p2
265
266 # modify p2 - p1 should appear stale
267 cp $WORK/p2x.go $WORK/d2/src/p2/p2.go
268 stale p1 p2
269
270 # build & install p1 again
271 go install -i p11
272 ! stale p1
273 ! stale p2
274
275 -- $WORK/d1/src/p1/p1.go --
276 package p1
277 import "p2"
278 func F() { p2.F() }
279 -- $WORK/d2/src/p2/p2.go --
280 package p2
281 func F() {}
282 -- $WORK/p2x.go --
283 package p2
284 func F() {}
285 func G() {}
286
287 The bug is that the final phase installs p11 instead of p1. The test failure looks like:
288
289 $ go test -run=Script
290 --- FAIL: TestScript (3.75s)
291 --- FAIL: TestScript/install_rebuild_gopath (0.16s)
292 script_test.go:223:
293 # GOPATH with p1 in d2, p2 in d2 (0.000s)
294 # build & install p1 (0.087s)
295 # modify p2 - p1 should appear stale (0.029s)
296 # build & install p1 again (0.022s)
297 > go install -i p11
298 [stderr]
299 can't load package: package p11: cannot find package "p11" in any of:
300 /Users/rsc/go/src/p11 (from $GOROOT)
301 $WORK/d1/src/p11 (from $GOPATH)
302 $WORK/d2/src/p11
303 [exit status 1]
304 FAIL: unexpected go command failure
305
306 script_test.go:73: failed at testdata/script/install_rebuild_gopath.txt:15 in $WORK/gopath/src
307
308 FAIL
309 exit status 1
310 FAIL cmd/go 4.875s
311 $
312
313 Note that the commands in earlier phases have been hidden, so that the relevant
314 commands are more easily found, and the elapsed time for a completed phase
315 is shown next to the phase heading. To see the entire execution, use "go test -v",
316 which also adds an initial environment dump to the beginning of the log.
317
318 Note also that in reported output, the actual name of the per-script temporary directory
319 has been consistently replaced with the literal string $WORK.
320
321 If Params.TestWork is true, it causes each test to log the name of its $WORK directory and other
322 environment variable settings and also to leave that directory behind when it exits,
323 for manual debugging of failing tests:
324
325 $ go test -run=Script -testwork
326 --- FAIL: TestScript (3.75s)
327 --- FAIL: TestScript/install_rebuild_gopath (0.16s)
328 script_test.go:223:
329 WORK=/tmp/cmd-go-test-745953508/script-install_rebuild_gopath
330 GOARCH=
331 GOCACHE=/Users/rsc/Library/Caches/go-build
332 GOOS=
333 GOPATH=$WORK/gopath
334 GOROOT=/Users/rsc/go
335 HOME=/no-home
336 TMPDIR=$WORK/tmp
337 exe=
338
339 # GOPATH with p1 in d2, p2 in d2 (0.000s)
340 # build & install p1 (0.085s)
341 # modify p2 - p1 should appear stale (0.030s)
342 # build & install p1 again (0.019s)
343 > go install -i p11
344 [stderr]
345 can't load package: package p11: cannot find package "p11" in any of:
346 /Users/rsc/go/src/p11 (from $GOROOT)
347 $WORK/d1/src/p11 (from $GOPATH)
348 $WORK/d2/src/p11
349 [exit status 1]
350 FAIL: unexpected go command failure
351
352 script_test.go:73: failed at testdata/script/install_rebuild_gopath.txt:15 in $WORK/gopath/src
353
354 FAIL
355 exit status 1
356 FAIL cmd/go 4.875s
357 $
358
359 $ WORK=/tmp/cmd-go-test-745953508/script-install_rebuild_gopath
360 $ cd $WORK/d1/src/p1
361 $ cat p1.go
362 package p1
363 import "p2"
364 func F() { p2.F() }
365 $
366
367 See also: https://pkg.go.dev/github.com/rogpeppe/go-internal/gotooltest
368 */
369 package testscript