Skip to content

Commit 77fe68f

Browse files
committed
testscript: fix RequireExplicitExec error messages
They used args[0], which is the first argument to the command, rather than the command name itself: > gofumpt foo.go FAIL: [...] use 'exec foo.go' rather than 'foo.go' (because RequireExplicitExec is enabled) I believe I introduced this regression when refactoring the pull request due to Roger's review, as I moved the code out of TestMain, where args[0] was still the name of the command to be run. Note that I can't easily add a regression test here, because the testscript command being used in the tests is not a top-level command run as a separate process, so it does not produce stdout nor stderr. The change seems easy enough to review manually, and our tests don't strictly check all error messages anyway. While here, remove the unnecessary use of -verbose.
1 parent bb36173 commit 77fe68f

File tree

3 files changed

+144
-121
lines changed

3 files changed

+144
-121
lines changed

testscript/cmd.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ var scriptCmds = map[string]func(*TestScript, bool, []string){
3535
"exists": (*TestScript).cmdExists,
3636
"grep": (*TestScript).cmdGrep,
3737
"mkdir": (*TestScript).cmdMkdir,
38+
"mv": (*TestScript).cmdMv,
3839
"rm": (*TestScript).cmdRm,
39-
"unquote": (*TestScript).cmdUnquote,
4040
"skip": (*TestScript).cmdSkip,
41-
"stdin": (*TestScript).cmdStdin,
4241
"stderr": (*TestScript).cmdStderr,
42+
"stdin": (*TestScript).cmdStdin,
4343
"stdout": (*TestScript).cmdStdout,
4444
"stop": (*TestScript).cmdStop,
4545
"symlink": (*TestScript).cmdSymlink,
4646
"unix2dos": (*TestScript).cmdUNIX2DOS,
47+
"unquote": (*TestScript).cmdUnquote,
4748
"wait": (*TestScript).cmdWait,
4849
}
4950

@@ -95,29 +96,22 @@ func (ts *TestScript) cmdChmod(neg bool, args []string) {
9596

9697
// cmp compares two files.
9798
func (ts *TestScript) cmdCmp(neg bool, args []string) {
98-
if neg {
99-
// It would be strange to say "this file can have any content except this precise byte sequence".
100-
ts.Fatalf("unsupported: ! cmp")
101-
}
10299
if len(args) != 2 {
103100
ts.Fatalf("usage: cmp file1 file2")
104101
}
105102

106-
ts.doCmdCmp(args, false)
103+
ts.doCmdCmp(neg, args, false)
107104
}
108105

109106
// cmpenv compares two files with environment variable substitution.
110107
func (ts *TestScript) cmdCmpenv(neg bool, args []string) {
111-
if neg {
112-
ts.Fatalf("unsupported: ! cmpenv")
113-
}
114108
if len(args) != 2 {
115109
ts.Fatalf("usage: cmpenv file1 file2")
116110
}
117-
ts.doCmdCmp(args, true)
111+
ts.doCmdCmp(neg, args, true)
118112
}
119113

120-
func (ts *TestScript) doCmdCmp(args []string, env bool) {
114+
func (ts *TestScript) doCmdCmp(neg bool, args []string, env bool) {
121115
name1, name2 := args[0], args[1]
122116
text1 := ts.ReadFile(name1)
123117

@@ -128,8 +122,15 @@ func (ts *TestScript) doCmdCmp(args []string, env bool) {
128122
if env {
129123
text2 = ts.expand(text2)
130124
}
131-
if text1 == text2 {
132-
return
125+
eq := text1 == text2
126+
if neg {
127+
if eq {
128+
ts.Fatalf("%s and %s do not differ", name1, name2)
129+
}
130+
return // they differ, as expected
131+
}
132+
if eq {
133+
return // they are equal, as expected
133134
}
134135
if ts.params.UpdateScripts && !env {
135136
if scriptFile, ok := ts.scriptFiles[absName2]; ok {
@@ -324,6 +325,16 @@ func (ts *TestScript) cmdMkdir(neg bool, args []string) {
324325
}
325326
}
326327

328+
func (ts *TestScript) cmdMv(neg bool, args []string) {
329+
if neg {
330+
ts.Fatalf("unsupported: ! mv")
331+
}
332+
if len(args) != 2 {
333+
ts.Fatalf("usage: mv old new")
334+
}
335+
ts.Check(os.Rename(ts.MkAbs(args[0]), ts.MkAbs(args[1])))
336+
}
337+
327338
// unquote unquotes files.
328339
func (ts *TestScript) cmdUnquote(neg bool, args []string) {
329340
if neg {

testscript/doc.go

Lines changed: 111 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ commands support this prefix. They are indicated below by [!] in the synopsis.
100100
The command prefix [cond] indicates that the command on the rest of the line
101101
should only run when the condition is satisfied. The predefined conditions are:
102102
103-
- [short] for testing.Short()
104-
- [net] for whether the external network can be used
105-
- [link] for whether the OS has hard link support
106-
- [symlink] for whether the OS has symbolic link support
107-
- [exec:prog] for whether prog is available for execution (found by exec.LookPath)
108-
- [gc] for whether Go was built with gc
109-
- [gccgo] for whether Go was built with gccgo
110-
- [go1.x] for whether the Go version is 1.x or later
111-
- [unix] for whether the OS is Unix-like (that is, would match the 'unix' build
112-
constraint)
103+
- [short] for testing.Short()
104+
- [net] for whether the external network can be used
105+
- [link] for whether the OS has hard link support
106+
- [symlink] for whether the OS has symbolic link support
107+
- [exec:prog] for whether prog is available for execution (found by exec.LookPath)
108+
- [gc] for whether Go was built with gc
109+
- [gccgo] for whether Go was built with gccgo
110+
- [go1.x] for whether the Go version is 1.x or later
111+
- [unix] for whether the OS is Unix-like (that is, would match the 'unix' build
112+
constraint)
113113
114114
Any known values of GOOS and GOARCH can also be used as conditions. They will be
115115
satisfied if the target OS or architecture match the specified value. For example,
@@ -122,102 +122,107 @@ Additional conditions can be added by passing a function to Params.Condition.
122122
123123
The predefined commands are:
124124
125-
- cd dir
126-
Change to the given directory for future commands.
127-
128-
- chmod perm path...
129-
Change the permissions of the files or directories named by the path arguments
130-
to the given octal mode (000 to 777).
131-
132-
- cmp file1 file2
133-
Check that the named files have the same content.
134-
By convention, file1 is the actual data and file2 the expected data.
135-
File1 can be "stdout" or "stderr" to use the standard output or standard error
136-
from the most recent exec or wait command.
137-
(If the files have differing content, the failure prints a diff.)
138-
139-
- cmpenv file1 file2
140-
Like cmp, but environment variables in file2 are substituted before the
141-
comparison. For example, $GOOS is replaced by the target GOOS.
142-
143-
- cp src... dst
144-
Copy the listed files to the target file or existing directory.
145-
src can include "stdout" or "stderr" to use the standard output or standard error
146-
from the most recent exec or go command.
147-
148-
- env [key=value...]
149-
With no arguments, print the environment (useful for debugging).
150-
Otherwise add the listed key=value pairs to the environment.
151-
152-
- [!] exec program [args...] [&]
153-
Run the given executable program with the arguments.
154-
It must (or must not) succeed.
155-
Note that 'exec' does not terminate the script (unlike in Unix shells).
156-
157-
If the last token is '&', the program executes in the background. The standard
158-
output and standard error of the previous command is cleared, but the output
159-
of the background process is buffered — and checking of its exit status is
160-
delayed — until the next call to 'wait', 'skip', or 'stop' or the end of the
161-
test. At the end of the test, any remaining background processes are
162-
terminated using os.Interrupt (if supported) or os.Kill.
163-
164-
If the last token is '&word&` (where "word" is alphanumeric), the
165-
command runs in the background but has a name, and can be waited
166-
for specifically by passing the word to 'wait'.
167-
168-
Standard input can be provided using the stdin command; this will be
169-
cleared after exec has been called.
170-
171-
- [!] exists [-readonly] file...
172-
Each of the listed files or directories must (or must not) exist.
173-
If -readonly is given, the files or directories must be unwritable.
174-
175-
- [!] grep [-count=N] pattern file
176-
The file's content must (or must not) match the regular expression pattern.
177-
For positive matches, -count=N specifies an exact number of matches to require.
178-
179-
- mkdir path...
180-
Create the listed directories, if they do not already exists.
181-
182-
- unquote file...
183-
Rewrite each file by replacing any leading ">" characters from
184-
each line. This enables a file to contain substrings that look like
185-
txtar file markers.
186-
See also https://godoc.org/github.com/rogpeppe/go-internal/txtar#Unquote
187-
188-
- rm file...
189-
Remove the listed files or directories.
190-
191-
- skip [message]
192-
Mark the test skipped, including the message if given.
193-
194-
- stdin file
195-
Set the standard input for the next exec command to the contents of the given file.
196-
File can be "stdout" or "stderr" to use the standard output or standard error
197-
from the most recent exec or wait command.
198-
199-
- [!] stderr [-count=N] pattern
200-
Apply the grep command (see above) to the standard error
201-
from the most recent exec or wait command.
202-
203-
- [!] stdout [-count=N] pattern
204-
Apply the grep command (see above) to the standard output
205-
from the most recent exec or wait command.
206-
207-
- stop [message]
208-
Stop the test early (marking it as passing), including the message if given.
209-
210-
- symlink file -> target
211-
Create file as a symlink to target. The -> (like in ls -l output) is required.
212-
213-
- wait [command]
214-
Wait for all 'exec' and 'go' commands started in the background (with the '&'
215-
token) to exit, and display success or failure status for them.
216-
After a call to wait, the 'stderr' and 'stdout' commands will apply to the
217-
concatenation of the corresponding streams of the background commands,
218-
in the order in which those commands were started.
219-
220-
If an argument is specified, it waits for just that command.
125+
- cd dir
126+
Change to the given directory for future commands.
127+
128+
- chmod perm path...
129+
Change the permissions of the files or directories named by the path arguments
130+
to the given octal mode (000 to 777).
131+
132+
- [!] cmp file1 file2
133+
Check that the named files have (or do not have) the same content.
134+
By convention, file1 is the actual data and file2 the expected data.
135+
File1 can be "stdout" or "stderr" to use the standard output or standard error
136+
from the most recent exec or wait command.
137+
(If the files have differing content and the command is not negated,
138+
the failure prints a diff.)
139+
140+
- [!] cmpenv file1 file2
141+
Like cmp, but environment variables in file2 are substituted before the
142+
comparison. For example, $GOOS is replaced by the target GOOS.
143+
144+
- cp src... dst
145+
Copy the listed files to the target file or existing directory.
146+
src can include "stdout" or "stderr" to use the standard output or standard error
147+
from the most recent exec or go command.
148+
149+
- env [key=value...]
150+
With no arguments, print the environment (useful for debugging).
151+
Otherwise add the listed key=value pairs to the environment.
152+
153+
- [!] exec program [args...] [&]
154+
Run the given executable program with the arguments.
155+
It must (or must not) succeed.
156+
Note that 'exec' does not terminate the script (unlike in Unix shells).
157+
158+
If the last token is '&', the program executes in the background. The standard
159+
output and standard error of the previous command is cleared, but the output
160+
of the background process is buffered — and checking of its exit status is
161+
delayed — until the next call to 'wait', 'skip', or 'stop' or the end of the
162+
test. At the end of the test, any remaining background processes are
163+
terminated using os.Interrupt (if supported) or os.Kill.
164+
165+
If the last token is '&word&` (where "word" is alphanumeric), the
166+
command runs in the background but has a name, and can be waited
167+
for specifically by passing the word to 'wait'.
168+
169+
Standard input can be provided using the stdin command; this will be
170+
cleared after exec has been called.
171+
172+
- [!] exists [-readonly] file...
173+
Each of the listed files or directories must (or must not) exist.
174+
If -readonly is given, the files or directories must be unwritable.
175+
176+
- [!] grep [-count=N] pattern file
177+
The file's content must (or must not) match the regular expression pattern.
178+
For positive matches, -count=N specifies an exact number of matches to require.
179+
180+
- mkdir path...
181+
Create the listed directories, if they do not already exists.
182+
183+
- mv path1 path2
184+
Rename path1 to path2. OS-specific restrictions may apply when path1 and path2
185+
are in different directories.
186+
187+
- rm file...
188+
Remove the listed files or directories.
189+
190+
- skip [message]
191+
Mark the test skipped, including the message if given.
192+
193+
- [!] stderr [-count=N] pattern
194+
Apply the grep command (see above) to the standard error
195+
from the most recent exec or wait command.
196+
197+
- stdin file
198+
Set the standard input for the next exec command to the contents of the given file.
199+
File can be "stdout" or "stderr" to use the standard output or standard error
200+
from the most recent exec or wait command.
201+
202+
- [!] stdout [-count=N] pattern
203+
Apply the grep command (see above) to the standard output
204+
from the most recent exec or wait command.
205+
206+
- stop [message]
207+
Stop the test early (marking it as passing), including the message if given.
208+
209+
- symlink file -> target
210+
Create file as a symlink to target. The -> (like in ls -l output) is required.
211+
212+
- unquote file...
213+
Rewrite each file by replacing any leading ">" characters from
214+
each line. This enables a file to contain substrings that look like
215+
txtar file markers.
216+
See also https://godoc.org/github.com/rogpeppe/go-internal/txtar#Unquote
217+
218+
- wait [command]
219+
Wait for all 'exec' and 'go' commands started in the background (with the '&'
220+
token) to exit, and display success or failure status for them.
221+
After a call to wait, the 'stderr' and 'stdout' commands will apply to the
222+
concatenation of the corresponding streams of the background commands,
223+
in the order in which those commands were started.
224+
225+
If an argument is specified, it waits for just that command.
221226
222227
When TestScript runs a script and the script fails, by default TestScript shows
223228
the execution of the most recent phase of the script (since the last # comment)

testscript/testdata/cpstdout.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
exec cat hello.text
55
cp stdout got
66
cmp got hello.text
7+
! cmp got different.text
8+
9+
exists got
10+
mv got new
11+
! exists got
12+
cmp new hello.text
713

814
-- hello.text --
915
hello world
10-
16+
-- different.text --
17+
goodbye

0 commit comments

Comments
 (0)