Skip to content

Commit 4bbc89b

Browse files
authored
goproxytest: support fallback from txtar to reading from directory of files (#43)
1 parent d61f414 commit 4bbc89b

File tree

7 files changed

+106
-15
lines changed

7 files changed

+106
-15
lines changed

goproxytest/proxy.go

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
// license that can be found in the LICENSE file.
44

55
/*
6-
Package goproxytest serves Go modules from a proxy
7-
server designed to run on localhost during tests, both to make tests avoid
8-
requiring specific network servers and also to make them
9-
significantly faster.
6+
Package goproxytest serves Go modules from a proxy server designed to run on
7+
localhost during tests, both to make tests avoid requiring specific network
8+
servers and also to make them significantly faster.
109
11-
Each module archive is named path_vers.txt, where slashes in path
12-
have been replaced with underscores. The archive must contain
13-
two files ".info" and ".mod", to be served as the info and mod files
14-
in the proxy protocol (see https://research.swtch.com/vgo-module).
15-
The remaining files are served as the content of the module zip file.
16-
The path@vers prefix required of files in the zip file is added
17-
automatically by the proxy: the files in the archive have names without
18-
the prefix, like plain "go.mod", "x.go", and so on.
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
13+
the info and mod files in the proxy protocol (see
14+
https://research.swtch.com/vgo-module). The remaining files are served as the
15+
content of the module zip file. The path@vers prefix required of files in the
16+
zip file is added automatically by the proxy: the files in the archive have
17+
names without the prefix, like plain "go.mod", "x.go", and so on.
1918
20-
See ../cmd/txtar-addmod and ../cmd/txtar-savedir for tools
21-
generate txtar files, although it's fine to write them by hand.
19+
See ../cmd/txtar-addmod and ../cmd/txtar-savedir for tools generate txtar
20+
files, although it's fine to write them by hand.
2221
*/
2322
package goproxytest
2423

@@ -92,7 +91,7 @@ func (srv *Server) readModList() error {
9291
}
9392
for _, info := range infos {
9493
name := info.Name()
95-
if !strings.HasSuffix(name, ".txt") {
94+
if !strings.HasSuffix(name, ".txt") && !info.IsDir() {
9695
continue
9796
}
9897
name = strings.TrimSuffix(name, ".txt")
@@ -275,6 +274,34 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
275274
name := filepath.Join(srv.dir, prefix+"_"+encVers+".txt")
276275
a := srv.archiveCache.Do(name, func() interface{} {
277276
a, err := txtar.ParseFile(name)
277+
if os.IsNotExist(err) {
278+
// we fallback to trying a directory
279+
name = strings.TrimSuffix(name, ".txt")
280+
281+
a = new(txtar.Archive)
282+
283+
err = filepath.Walk(name, func(path string, info os.FileInfo, err error) error {
284+
if err != nil {
285+
return err
286+
}
287+
if path == name && !info.IsDir() {
288+
return fmt.Errorf("expected a directory root")
289+
}
290+
if info.IsDir() {
291+
return nil
292+
}
293+
arpath := filepath.ToSlash(strings.TrimPrefix(path, name+string(os.PathSeparator)))
294+
data, err := ioutil.ReadFile(path)
295+
if err != nil {
296+
return err
297+
}
298+
a.Files = append(a.Files, txtar.File{
299+
Name: arpath,
300+
Data: data,
301+
})
302+
return nil
303+
})
304+
}
278305
if err != nil {
279306
if !os.IsNotExist(err) {
280307
fmt.Fprintf(os.Stderr, "go proxy: %v\n", err)

goproxytest/proxy_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package goproxytest_test
6+
7+
import (
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/rogpeppe/go-internal/goproxytest"
12+
"github.com/rogpeppe/go-internal/gotooltest"
13+
"github.com/rogpeppe/go-internal/testscript"
14+
)
15+
16+
func TestScripts(t *testing.T) {
17+
srv, err := goproxytest.NewServer(filepath.Join("testdata", "mod"), "")
18+
if err != nil {
19+
t.Fatalf("cannot start proxy: %v", err)
20+
}
21+
p := testscript.Params{
22+
Dir: "testdata",
23+
Setup: func(e *testscript.Env) error {
24+
e.Vars = append(e.Vars,
25+
"GOPROXY="+srv.URL,
26+
)
27+
return nil
28+
},
29+
}
30+
if err := gotooltest.Setup(&p); err != nil {
31+
t.Fatal(err)
32+
}
33+
testscript.Run(t, p)
34+
}

goproxytest/testdata/list.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# prior to go 1.12 you cannot list a module without a requirement
2+
[!go1.12] go get fruit.com
3+
4+
go list -m -versions fruit.com
5+
stdout 'v1.0.0 v1.1.0'
6+
7+
8+
9+
10+
-- go.mod --
11+
module mod
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- .mod --
2+
module fruit.com
3+
4+
-- .info --
5+
{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"}
6+
7+
-- go.mod --
8+
module fruit.com
9+
10+
-- fruit/fruit.go --
11+
package main
12+
13+
const Name = "Orange"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Version":"v1.1.0","Time":"2018-10-22T18:45:39Z"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module fruit.com
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package fruit
2+
3+
const Name = "Apple"

0 commit comments

Comments
 (0)