Skip to content

Commit 782714e

Browse files
Bryan C. Millsromaindoumenc
Bryan C. Mills
authored andcommitted
cmd/go: replace the 'addcrlf' script command with a more general 'replace' command
This allows the "reuse_git" test to avoid depending on exact JSON blobs, which will be important when the URLs start referring to test-local vcweb servers. For golang#27494. Change-Id: I22fde5110b3267b8fb9fb9c59fabc3b8a8b492c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/427094 Reviewed-by: Russ Cox <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]>
1 parent b8731e5 commit 782714e

File tree

5 files changed

+55
-99
lines changed

5 files changed

+55
-99
lines changed

src/cmd/go/internal/script/cmds.go

+38
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func DefaultCmds() map[string]Cmd {
4343
"mkdir": Mkdir(),
4444
"mv": Mv(),
4545
"rm": Rm(),
46+
"replace": Replace(),
4647
"sleep": Sleep(),
4748
"stderr": Stderr(),
4849
"stdout": Stdout(),
@@ -867,6 +868,43 @@ func Program(name string, interrupt os.Signal, gracePeriod time.Duration) Cmd {
867868
})
868869
}
869870

871+
// Replace replaces all occurrences of a string in a file with another string.
872+
func Replace() Cmd {
873+
return Command(
874+
CmdUsage{
875+
Summary: "replace strings in a file",
876+
Args: "[old new]... file",
877+
Detail: []string{
878+
"The 'old' and 'new' arguments are unquoted as if in quoted Go strings.",
879+
},
880+
},
881+
func(s *State, args ...string) (WaitFunc, error) {
882+
if len(args)%2 != 1 {
883+
return nil, ErrUsage
884+
}
885+
886+
oldNew := make([]string, 0, len(args)-1)
887+
for _, arg := range args[:len(args)-1] {
888+
s, err := strconv.Unquote(`"` + arg + `"`)
889+
if err != nil {
890+
return nil, err
891+
}
892+
oldNew = append(oldNew, s)
893+
}
894+
895+
r := strings.NewReplacer(oldNew...)
896+
file := s.Path(args[len(args)-1])
897+
898+
data, err := os.ReadFile(file)
899+
if err != nil {
900+
return nil, err
901+
}
902+
replaced := r.Replace(string(data))
903+
904+
return nil, os.WriteFile(file, []byte(replaced), 0666)
905+
})
906+
}
907+
870908
// Rm removes a file or directory.
871909
//
872910
// If a directory, Rm also recursively removes that directory's

src/cmd/go/scriptcmds_test.go

-30
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package main_test
66

77
import (
8-
"bytes"
98
"cmd/go/internal/script"
109
"cmd/go/internal/script/scripttest"
1110
"cmd/go/internal/work"
@@ -30,7 +29,6 @@ func scriptCommands(interrupt os.Signal, gracePeriod time.Duration) map[string]s
3029
cmds[name] = cmd
3130
}
3231

33-
add("addcrlf", scriptAddCRLF())
3432
add("cc", scriptCC(cmdExec))
3533
cmdGo := scriptGo(interrupt, gracePeriod)
3634
add("go", cmdGo)
@@ -39,34 +37,6 @@ func scriptCommands(interrupt os.Signal, gracePeriod time.Duration) map[string]s
3937
return cmds
4038
}
4139

42-
// scriptAddCRLF adds CRLF line endings to the named files.
43-
func scriptAddCRLF() script.Cmd {
44-
return script.Command(
45-
script.CmdUsage{
46-
Summary: "convert line endings to CRLF",
47-
Args: "file...",
48-
},
49-
func(s *script.State, args ...string) (script.WaitFunc, error) {
50-
if len(args) == 0 {
51-
return nil, script.ErrUsage
52-
}
53-
54-
for _, file := range args {
55-
file = s.Path(file)
56-
data, err := os.ReadFile(file)
57-
if err != nil {
58-
return nil, err
59-
}
60-
err = os.WriteFile(file, bytes.ReplaceAll(data, []byte("\n"), []byte("\r\n")), 0666)
61-
if err != nil {
62-
return nil, err
63-
}
64-
}
65-
66-
return nil, nil
67-
})
68-
}
69-
7040
// scriptCC runs the C compiler along with platform specific options.
7141
func scriptCC(cmdExec script.Cmd) script.Cmd {
7242
return script.Command(

src/cmd/go/testdata/script/README

+6-4
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,6 @@ for manual debugging of failing tests:
204204
$
205205

206206
The available commands are:
207-
addcrlf file...
208-
convert line endings to CRLF
209-
210-
211207
cat files...
212208
concatenate files and print to the script's stdout buffer
213209

@@ -304,6 +300,12 @@ mv old new
304300
OS-specific restrictions may apply when old and new are in
305301
different directories.
306302

303+
replace [old new]... file
304+
replace strings in a file
305+
306+
The 'old' and 'new' arguments are unquoted as if in quoted
307+
Go strings.
308+
307309
rm path...
308310
remove a file or directory
309311

src/cmd/go/testdata/script/mod_find.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ stderr 'module x'
88

99
# Import comment works even with CRLF line endings.
1010
rm go.mod
11-
addcrlf x.go
11+
replace '\n' '\r\n' x.go
1212
go mod init
1313
stderr 'module x'
1414

src/cmd/go/testdata/script/reuse_git.txt

+10-64
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ stdout '"Reuse": true'
321321
! stdout '"(Dir|Info|GoMod|Zip)"'
322322

323323
# reuse attempt with stale hash should reinvoke git, not report reuse
324+
cp tagtestsv022.json tagtestsv022badhash.json
325+
replace '57952' '56952XXX' tagtestsv022badhash.json
324326
go mod download -reuse=tagtestsv022badhash.json -x -json vcs-test.golang.org/git/[email protected]
325327
stderr 'git fetch'
326328
! stdout '"Reuse": true'
@@ -337,6 +339,8 @@ stdout '"GoMod"'
337339
stdout '"Zip"'
338340

339341
# reuse with stale repo URL
342+
cp tagtestsv022.json tagtestsv022badurl.json
343+
replace 'git/tagtests\"' 'git/tagtestsXXX\"' tagtestsv022badurl.json
340344
go mod download -reuse=tagtestsv022badurl.json -x -json vcs-test.golang.org/git/[email protected]
341345
! stdout '"Reuse": true'
342346
stdout '"URL": "https://vcs-test.golang.org/git/tagtests"'
@@ -346,80 +350,22 @@ stdout '"GoMod"'
346350
stdout '"Zip"'
347351

348352
# reuse with stale VCS
353+
cp tagtestsv022.json tagtestsv022badvcs.json
354+
replace '\"git\"' '\"gitXXX\"' tagtestsv022badvcs.json
349355
go mod download -reuse=tagtestsv022badvcs.json -x -json vcs-test.golang.org/git/[email protected]
350356
! stdout '"Reuse": true'
351357
stdout '"URL": "https://vcs-test.golang.org/git/tagtests"'
352358

353359
# reuse with stale Dir
360+
cp tagtestsv022.json tagtestsv022baddir.json
361+
replace '\t\t\"Ref\":' '\t\t\"Subdir\": \"subdir\",\n\t\t\"Ref\":' tagtestsv022baddir.json
354362
go mod download -reuse=tagtestsv022baddir.json -x -json vcs-test.golang.org/git/[email protected]
355363
! stdout '"Reuse": true'
356364
stdout '"URL": "https://vcs-test.golang.org/git/tagtests"'
357365

358366
# reuse with stale TagSum
367+
cp tagtests.json tagtestsbadtagsum.json
368+
replace 'sMEOGo=' 'sMEoGo=XXX' tagtestsbadtagsum.json
359369
go mod download -reuse=tagtestsbadtagsum.json -x -json vcs-test.golang.org/git/tagtests.git@latest
360370
! stdout '"Reuse": true'
361371
stdout '"TagSum": "t1:Dp7yRKDuE8WjG0429PN9hYWjqhy2te7P9Oki/sMEOGo="'
362-
363-
-- tagtestsv022badhash.json --
364-
{
365-
"Path": "vcs-test.golang.org/git/tagtests.git",
366-
"Version": "v0.2.2",
367-
"Origin": {
368-
"VCS": "git",
369-
"URL": "https://vcs-test.golang.org/git/tagtests",
370-
"Ref": "refs/tags/v0.2.2",
371-
"Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952XXX"
372-
}
373-
}
374-
375-
-- tagtestsbadtagsum.json --
376-
{
377-
"Path": "vcs-test.golang.org/git/tagtests.git",
378-
"Version": "v0.2.2",
379-
"Query": "latest",
380-
"Origin": {
381-
"VCS": "git",
382-
"URL": "https://vcs-test.golang.org/git/tagtests",
383-
"TagSum": "t1:Dp7yRKDuE8WjG0429PN9hYWjqhy2te7P9Oki/sMEOGo=XXX",
384-
"Ref": "refs/tags/v0.2.2",
385-
"Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
386-
},
387-
"Reuse": true
388-
}
389-
390-
-- tagtestsv022badvcs.json --
391-
{
392-
"Path": "vcs-test.golang.org/git/tagtests.git",
393-
"Version": "v0.2.2",
394-
"Origin": {
395-
"VCS": "gitXXX",
396-
"URL": "https://vcs-test.golang.org/git/tagtests",
397-
"Ref": "refs/tags/v0.2.2",
398-
"Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
399-
}
400-
}
401-
402-
-- tagtestsv022baddir.json --
403-
{
404-
"Path": "vcs-test.golang.org/git/tagtests.git",
405-
"Version": "v0.2.2",
406-
"Origin": {
407-
"VCS": "git",
408-
"URL": "https://vcs-test.golang.org/git/tagtests",
409-
"Subdir": "subdir",
410-
"Ref": "refs/tags/v0.2.2",
411-
"Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
412-
}
413-
}
414-
415-
-- tagtestsv022badurl.json --
416-
{
417-
"Path": "vcs-test.golang.org/git/tagtests.git",
418-
"Version": "v0.2.2",
419-
"Origin": {
420-
"VCS": "git",
421-
"URL": "https://vcs-test.golang.org/git/tagtestsXXX",
422-
"Ref": "refs/tags/v0.2.2",
423-
"Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
424-
}
425-
}

0 commit comments

Comments
 (0)