Skip to content

Commit af73bbc

Browse files
authored
all: add support for txtar extension and prefer it (#159)
For backwards compatibility, both testscript and goproxytest, which used to glob on `*.txt`, now look for both file extensions. Note that this required a bit of a refactor in testscript, as we cannot use a single glob expression to accomplish this. Code which produces files, such as txtar-addmod, now produces `*.txtar` rather than `*.txt`. Similarly, the public docs now use `*.txtar` too. Note that we leave many txt files in tests untouched; it's unnecessary to change them given the backwards compatibility, and it has zero benefit to the user as they aren't public. Moreover, the diff churn would make this patch harder to review. If a future version of go-internal only supports txtar extensions, then it could replace all of those extensions accordingly. Fixes #126.
1 parent e9142ea commit af73bbc

File tree

14 files changed

+65
-40
lines changed

14 files changed

+65
-40
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
# example, we want to allow a .txt file to contain a CRLF line ending
88
*.go text eol=lf
99
*.txt text eol=lf
10+
*.txtar text eol=lf

cmd/testscript/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ script, and does not remove that directory when testscript exits.
3737
Examples
3838
========
3939
40-
The following example, fruit.txt, shows a simple reproduction that includes
40+
The following example, fruit.txtar, shows a simple reproduction that includes
4141
.gomodproxy supporting files:
4242
4343
go get -m fruit.com
@@ -58,7 +58,7 @@ The following example, fruit.txt, shows a simple reproduction that includes
5858
5959
const Name = "Apple"
6060
61-
Running testscript -v fruit.txt we get:
61+
Running testscript -v fruit.txtar we get:
6262
6363
...
6464
> go get -m fruit.com
@@ -76,7 +76,7 @@ Running testscript -v fruit.txt we get:
7676
PASS
7777
7878
79-
The following example, goimports.txt, shows a simple reproduction involving
79+
The following example, goimports.txtar, shows a simple reproduction involving
8080
goimports:
8181
8282
go install golang.org/x/tools/cmd/goimports
@@ -95,7 +95,7 @@ goimports:
9595
9696
const Pi = math.Pi
9797
98-
Running testscript -v goimports.txt we get:
98+
Running testscript -v goimports.txtar we get:
9999
100100
...
101101
> go install golang.org/x/tools/cmd/goimports

cmd/testscript/help.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ script, and does not remove that directory when testscript exits.
4848
Examples
4949
========
5050
51-
The following example, fruit.txt, shows a simple reproduction that includes
51+
The following example, fruit.txtar, shows a simple reproduction that includes
5252
.gomodproxy supporting files:
5353
5454
go get -m fruit.com
@@ -69,7 +69,7 @@ The following example, fruit.txt, shows a simple reproduction that includes
6969
7070
const Name = "Apple"
7171
72-
Running testscript -v fruit.txt we get:
72+
Running testscript -v fruit.txtar we get:
7373
7474
...
7575
> go get -m fruit.com
@@ -87,7 +87,7 @@ Running testscript -v fruit.txt we get:
8787
PASS
8888
8989
90-
The following example, goimports.txt, shows a simple reproduction involving
90+
The following example, goimports.txtar, shows a simple reproduction involving
9191
goimports:
9292
9393
go install golang.org/x/tools/cmd/goimports
@@ -106,7 +106,7 @@ goimports:
106106
107107
const Pi = math.Pi
108108
109-
Running testscript -v goimports.txt we get:
109+
Running testscript -v goimports.txtar we get:
110110
111111
...
112112
> go install golang.org/x/tools/cmd/goimports

cmd/testscript/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (tr *testRunner) run(runDir, filename string) error {
196196
return fmt.Errorf("failed to write .gomodproxy files: %v", err)
197197
}
198198

199-
scriptFile := filepath.Join(runDir, "script.txt")
199+
scriptFile := filepath.Join(runDir, "script.txtar")
200200

201201
if err := ioutil.WriteFile(scriptFile, txtar.Format(&script), 0666); err != nil {
202202
return fmt.Errorf("failed to write script for %v: %v", renderFilename(filename), err)

cmd/testscript/testdata/work.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ testscript -v -work file.txt dir/file.txt
1212
stderr '^temporary work directory: \Q'$WORK'\E[/\\]\.tmp[/\\]'
1313
stderr '^temporary work directory for file.txt: \Q'$WORK'\E[/\\]\.tmp[/\\]'
1414
stderr '^temporary work directory for dir[/\\]file.txt: \Q'$WORK'\E[/\\]\.tmp[/\\]'
15-
expandone $WORK/.tmp/testscript*/file.txt/script.txt
16-
expandone $WORK/.tmp/testscript*/file.txt1/script.txt
15+
expandone $WORK/.tmp/testscript*/file.txt/script.txtar
16+
expandone $WORK/.tmp/testscript*/file.txt1/script.txtar
1717

1818
-- file.txt --
1919
>exec true

cmd/txtar-addmod/addmod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func main1() int {
202202
break
203203
}
204204
} else {
205-
if err := ioutil.WriteFile(filepath.Join(targetDir, modDir+".txt"), data, 0666); err != nil {
205+
if err := ioutil.WriteFile(filepath.Join(targetDir, modDir+".txtar"), data, 0666); err != nil {
206206
log.Printf("%s: %v", arg, err)
207207
exitCode = 1
208208
continue

cmd/txtar-addmod/testdata/encode.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ mkdir $WORK/out
22
txtar-addmod $WORK/out github.com/shurcooL/[email protected]
33
! stdout .+
44
! stderr .+
5-
exists $WORK/out/github.com_shurcoo!l_httpfs_v0.0.0-20171119174359-809beceb2371.txt
5+
exists $WORK/out/github.com_shurcoo!l_httpfs_v0.0.0-20171119174359-809beceb2371.txtar

cmd/txtar-addmod/testdata/txtar-addmod-self.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ mkdir $WORK/out
22
txtar-addmod $WORK/out github.com/gobin-testrepos/simple-main
33
! stdout .+
44
! stderr .+
5-
exists $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txt
6-
! grep foobar $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txt
5+
exists $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txtar
6+
! grep foobar $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txtar
77

88
txtar-addmod -all $WORK/out github.com/gobin-testrepos/simple-main
9-
grep '-- foobar --' $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txt
9+
grep '-- foobar --' $WORK/out/github.com_gobin-testrepos_simple-main_v1.0.0.txtar

cmd/txtar-c/savedir.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Usage:
88
//
9-
// txtar-c /path/to/dir >saved.txt
9+
// txtar-c /path/to/dir >saved.txtar
1010
//
1111
// See https://godoc.org/github.com/rogpeppe/go-internal/txtar for details of the format
1212
// and how to parse a txtar file.
@@ -30,7 +30,7 @@ import (
3030
var flag = stdflag.NewFlagSet(os.Args[0], stdflag.ContinueOnError)
3131

3232
func usage() {
33-
fmt.Fprintf(os.Stderr, "usage: txtar-c dir >saved.txt\n")
33+
fmt.Fprintf(os.Stderr, "usage: txtar-c dir >saved.txtar\n")
3434
flag.PrintDefaults()
3535
}
3636

goproxytest/proxy.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Package goproxytest serves Go modules from a proxy server designed to run on
77
localhost during tests, both to make tests avoid requiring specific network
88
servers and also to make them significantly faster.
99
10-
Each module archive is either a file named path_vers.txt or a directory named
11-
path_vers, where slashes in path have been replaced with underscores. The
12-
archive or directory must contain two files ".info" and ".mod", to be served as
10+
Each module archive is either a file named path_vers.txtar or path_vers.txt, or
11+
a directory named path_vers, where slashes in path have been replaced with underscores.
12+
The archive or directory must contain two files ".info" and ".mod", to be served as
1313
the info and mod files in the proxy protocol (see
1414
https://research.swtch.com/vgo-module). The remaining files are served as the
1515
content of the module zip file. The path@vers prefix required of files in the
@@ -96,10 +96,15 @@ func (srv *Server) readModList() error {
9696
}
9797
for _, info := range infos {
9898
name := info.Name()
99-
if !strings.HasSuffix(name, ".txt") && !info.IsDir() {
99+
switch {
100+
case strings.HasSuffix(name, ".txt"):
101+
name = strings.TrimSuffix(name, ".txt")
102+
case strings.HasSuffix(name, ".txtar"):
103+
name = strings.TrimSuffix(name, ".txtar")
104+
case info.IsDir():
105+
default:
100106
continue
101107
}
102-
name = strings.TrimSuffix(name, ".txt")
103108
i := strings.LastIndex(name, "_v")
104109
if i < 0 {
105110
continue
@@ -281,13 +286,17 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
281286
}
282287

283288
prefix := strings.Replace(enc, "/", "_", -1)
284-
name := filepath.Join(srv.dir, prefix+"_"+encVers+".txt")
289+
name := filepath.Join(srv.dir, prefix+"_"+encVers)
290+
txtName := name + ".txt"
291+
txtarName := name + ".txtar"
285292
a := srv.archiveCache.Do(name, func() interface{} {
286-
a, err := txtar.ParseFile(name)
293+
a, err := txtar.ParseFile(txtarName)
287294
if os.IsNotExist(err) {
288-
// we fallback to trying a directory
289-
name = strings.TrimSuffix(name, ".txt")
290-
295+
// fall back to trying with the .txt extension
296+
a, err = txtar.ParseFile(txtName)
297+
}
298+
if os.IsNotExist(err) {
299+
// fall back to trying a directory
291300
a = new(txtar.Archive)
292301

293302
err = filepath.Walk(name, func(path string, info os.FileInfo, err error) error {

0 commit comments

Comments
 (0)