Skip to content

Commit 76dc4b3

Browse files
authored
testscript: relax constraints around actual source in cmp with update (#107)
There are currently constraints on the actual source for a failed cmp comparison when Params.UpdateScripts is true: the actual must be either stdout or stderr. However it's unclear why this constraint is necessary. Indeed it's a painful constraint when the actual source in a comparison is file written by a previous command. Relax this constraint because, after discussion with @mvdan and @rogpeppe, we believe it is safe to do so. Add a test for this new logic where the actual is a file that is part of the archive.
1 parent be09da5 commit 76dc4b3

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

testscript/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (ts *TestScript) doCmdCmp(args []string, env bool) {
131131
if text1 == text2 {
132132
return
133133
}
134-
if ts.params.UpdateScripts && !env && (args[0] == "stdout" || args[0] == "stderr") {
134+
if ts.params.UpdateScripts && !env {
135135
if scriptFile, ok := ts.scriptFiles[absName2]; ok {
136136
ts.scriptUpdates[scriptFile] = text1
137137
return
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
unquote scripts/testscript.txt
2+
unquote testscript-new.txt
3+
testscript-update scripts
4+
cmp scripts/testscript.txt testscript-new.txt
5+
6+
-- scripts/testscript.txt --
7+
>cmp got want
8+
>
9+
>-- got --
10+
>right
11+
>-- want --
12+
>wrong
13+
-- testscript-new.txt --
14+
>cmp got want
15+
>
16+
>-- got --
17+
>right
18+
>-- want --
19+
>right

testscript/testdata/testscript_update_script_not_in_archive.txt renamed to testscript/testdata/testscript_update_script_expected_not_in_archive.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Verify that comparing stdout against a file not in the archive does nothing
2+
13
unquote scripts/testscript.txt
24
cp scripts/testscript.txt unchanged
35
! testscript-update scripts

testscript/testscript.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ type Params struct {
143143
// error will be ignored.
144144
IgnoreMissedCoverage bool
145145

146-
// UpdateScripts specifies that if a `cmp` command fails and
147-
// its first argument is `stdout` or `stderr` and its second argument
148-
// refers to a file inside the testscript file, the command will succeed
149-
// and the testscript file will be updated to reflect the actual output.
146+
// UpdateScripts specifies that if a `cmp` command fails and its second
147+
// argument refers to a file inside the testscript file, the command will
148+
// succeed and the testscript file will be updated to reflect the actual
149+
// content (which could be stdout, stderr or a real file).
150150
//
151151
// The content will be quoted with txtar.Quote if needed;
152152
// a manual change will be needed if it is not unquoted in the

0 commit comments

Comments
 (0)