Skip to content

Commit 827babf

Browse files
author
Bryan C. Mills
committed
cmd/go: add mv and support "! cmp" in script tests
For #50183 Change-Id: Ie384333fb7a69d0d2cfaba0cfc4eb7afba2fd745 Reviewed-on: https://go-review.googlesource.com/c/go/+/380916 Trust: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
1 parent 6eb58cd commit 827babf

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

src/cmd/go/script_test.go

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ var scriptCmds = map[string]func(*testScript, simpleStatus, []string){
491491
"go": (*testScript).cmdGo,
492492
"grep": (*testScript).cmdGrep,
493493
"mkdir": (*testScript).cmdMkdir,
494+
"mv": (*testScript).cmdMv,
494495
"rm": (*testScript).cmdRm,
495496
"skip": (*testScript).cmdSkip,
496497
"stale": (*testScript).cmdStale,
@@ -585,10 +586,6 @@ func (ts *testScript) cmdChmod(want simpleStatus, args []string) {
585586

586587
// cmp compares two files.
587588
func (ts *testScript) cmdCmp(want simpleStatus, args []string) {
588-
if want != success {
589-
// It would be strange to say "this file can have any content except this precise byte sequence".
590-
ts.fatalf("unsupported: %v cmp", want)
591-
}
592589
quiet := false
593590
if len(args) > 0 && args[0] == "-q" {
594591
quiet = true
@@ -597,14 +594,11 @@ func (ts *testScript) cmdCmp(want simpleStatus, args []string) {
597594
if len(args) != 2 {
598595
ts.fatalf("usage: cmp file1 file2")
599596
}
600-
ts.doCmdCmp(args, false, quiet)
597+
ts.doCmdCmp(want, args, false, quiet)
601598
}
602599

603600
// cmpenv compares two files with environment variable substitution.
604601
func (ts *testScript) cmdCmpenv(want simpleStatus, args []string) {
605-
if want != success {
606-
ts.fatalf("unsupported: %v cmpenv", want)
607-
}
608602
quiet := false
609603
if len(args) > 0 && args[0] == "-q" {
610604
quiet = true
@@ -613,17 +607,18 @@ func (ts *testScript) cmdCmpenv(want simpleStatus, args []string) {
613607
if len(args) != 2 {
614608
ts.fatalf("usage: cmpenv file1 file2")
615609
}
616-
ts.doCmdCmp(args, true, quiet)
610+
ts.doCmdCmp(want, args, true, quiet)
617611
}
618612

619-
func (ts *testScript) doCmdCmp(args []string, env, quiet bool) {
613+
func (ts *testScript) doCmdCmp(want simpleStatus, args []string, env, quiet bool) {
620614
name1, name2 := args[0], args[1]
621615
var text1, text2 string
622-
if name1 == "stdout" {
616+
switch name1 {
617+
case "stdout":
623618
text1 = ts.stdout
624-
} else if name1 == "stderr" {
619+
case "stderr":
625620
text1 = ts.stderr
626-
} else {
621+
default:
627622
data, err := os.ReadFile(ts.mkabs(name1))
628623
ts.check(err)
629624
text1 = string(data)
@@ -638,14 +633,28 @@ func (ts *testScript) doCmdCmp(args []string, env, quiet bool) {
638633
text2 = ts.expand(text2, false)
639634
}
640635

641-
if text1 == text2 {
642-
return
643-
}
644-
645-
if !quiet {
636+
eq := text1 == text2
637+
if !eq && !quiet && want != failure {
646638
fmt.Fprintf(&ts.log, "[diff -%s +%s]\n%s\n", name1, name2, diff(text1, text2))
647639
}
648-
ts.fatalf("%s and %s differ", name1, name2)
640+
switch want {
641+
case failure:
642+
if eq {
643+
ts.fatalf("%s and %s do not differ", name1, name2)
644+
}
645+
case success:
646+
if !eq {
647+
ts.fatalf("%s and %s differ", name1, name2)
648+
}
649+
case successOrFailure:
650+
if eq {
651+
fmt.Fprintf(&ts.log, "%s and %s do not differ", name1, name2)
652+
} else {
653+
fmt.Fprintf(&ts.log, "%s and %s differ", name1, name2)
654+
}
655+
default:
656+
ts.fatalf("unsupported: %v cmp", want)
657+
}
649658
}
650659

651660
// cp copies files, maybe eventually directories.
@@ -840,6 +849,16 @@ func (ts *testScript) cmdMkdir(want simpleStatus, args []string) {
840849
}
841850
}
842851

852+
func (ts *testScript) cmdMv(want simpleStatus, args []string) {
853+
if want != success {
854+
ts.fatalf("unsupported: %v mv", want)
855+
}
856+
if len(args) != 2 {
857+
ts.fatalf("usage: mv old new")
858+
}
859+
ts.check(os.Rename(ts.mkabs(args[0]), ts.mkabs(args[1])))
860+
}
861+
843862
// rm removes files or directories.
844863
func (ts *testScript) cmdRm(want simpleStatus, args []string) {
845864
if want != success {

src/cmd/go/testdata/script/README

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,15 @@ The commands are:
110110
Change the permissions of the files or directories named by the path arguments
111111
to be equal to perm. Only numerical permissions are supported.
112112

113-
- cmp file1 file2
114-
Check that the named files have the same content.
113+
- [! | ?] cmp file1 file2
114+
Check that the named files have (or do not have) the same content.
115115
By convention, file1 is the actual data and file2 the expected data.
116116
File1 can be "stdout" or "stderr" to use the standard output or standard error
117117
from the most recent exec or go command.
118-
(If the files have differing content, the failure prints a diff.)
118+
(If the file contents differ and the command is not negated,
119+
the failure prints a diff.)
119120

120-
- cmpenv file1 file2
121+
- [! | ?] cmpenv file1 file2
121122
Like cmp, but environment variables are substituted in the file contents
122123
before the comparison. For example, $GOOS is replaced by the target GOOS.
123124

@@ -163,6 +164,10 @@ The commands are:
163164
- mkdir path...
164165
Create the listed directories, if they do not already exists.
165166

167+
- mv path1 path2
168+
Rename path1 to path2. OS-specific restrictions may apply when path1 and path2
169+
are in different directories.
170+
166171
- rm file...
167172
Remove the listed files or directories.
168173

0 commit comments

Comments
 (0)