diff options
author | Rob Findley <rfindley@google.com> | 2020-04-10 09:56:06 -0400 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2020-04-13 22:35:07 +0000 |
commit | 07bb9fb2f99e307be6c86c80aa098b592aede436 (patch) | |
tree | 74f305db08c5e4434e180b6aa92f157590c6fd13 /internal/proxydir/proxydir_test.go | |
parent | 250b2131eb8b6093aad664eb9012cf44c9f66b68 (diff) | |
download | golang-x-tools-07bb9fb2f99e307be6c86c80aa098b592aede436.tar.gz |
internal/proxydir: add an internal package for file-based proxies
Both packagestest and the gopls regtests need to write module data to
the filesystem in proxy structure. Since this seems like a common and
self-contained concerned, factor this out into a shared package.
Change-Id: I5275dbc0cd7b13290061e8bb559d6dd287fbb275
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227841
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Diffstat (limited to 'internal/proxydir/proxydir_test.go')
-rw-r--r-- | internal/proxydir/proxydir_test.go | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/internal/proxydir/proxydir_test.go b/internal/proxydir/proxydir_test.go new file mode 100644 index 000000000..54401fb16 --- /dev/null +++ b/internal/proxydir/proxydir_test.go @@ -0,0 +1,112 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package proxydir + +import ( + "archive/zip" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestWriteModuleVersion(t *testing.T) { + tests := []struct { + modulePath, version string + files map[string][]byte + }{ + { + modulePath: "mod.test/module", + version: "v1.2.3", + files: map[string][]byte{ + "go.mod": []byte("module mod.com\n\ngo 1.12"), + "const.go": []byte("package module\n\nconst Answer = 42"), + }, + }, + { + modulePath: "mod.test/module", + version: "v1.2.4", + files: map[string][]byte{ + "go.mod": []byte("module mod.com\n\ngo 1.12"), + "const.go": []byte("package module\n\nconst Answer = 43"), + }, + }, + { + modulePath: "mod.test/nogomod", + version: "v0.9.0", + files: map[string][]byte{ + "const.go": []byte("package module\n\nconst Other = \"Other\""), + }, + }, + } + dir, err := ioutil.TempDir("", "proxydirtest-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + for _, test := range tests { + // Since we later assert on the contents of /list, don't use subtests. + if err := WriteModuleVersion(dir, test.modulePath, test.version, test.files); err != nil { + t.Fatal(err) + } + rootDir := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v") + gomod, err := ioutil.ReadFile(filepath.Join(rootDir, test.version+".mod")) + if err != nil { + t.Fatal(err) + } + wantMod, ok := test.files["go.mod"] + if !ok { + wantMod = []byte("module " + test.modulePath) + } + if got, want := string(gomod), string(wantMod); got != want { + t.Errorf("reading %s/@v/%s.mod: got %q, want %q", test.modulePath, test.version, got, want) + } + zr, err := zip.OpenReader(filepath.Join(rootDir, test.version+".zip")) + if err != nil { + t.Fatal(err) + } + defer zr.Close() + + for _, zf := range zr.File { + r, err := zf.Open() + if err != nil { + t.Fatal(err) + } + defer r.Close() + content, err := ioutil.ReadAll(r) + if err != nil { + t.Fatal(err) + } + name := strings.TrimPrefix(zf.Name, fmt.Sprintf("%s@%s/", test.modulePath, test.version)) + if got, want := string(content), string(test.files[name]); got != want { + t.Errorf("unzipping %q: got %q, want %q", zf.Name, got, want) + } + delete(test.files, name) + } + for name := range test.files { + t.Errorf("file %q not present in the module zip", name) + } + } + + lists := []struct { + modulePath, want string + }{ + {"mod.test/module", "v1.2.3\nv1.2.4\n"}, + {"mod.test/nogomod", "v0.9.0\n"}, + } + + for _, test := range lists { + fp := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v", "list") + list, err := ioutil.ReadFile(fp) + if err != nil { + t.Fatal(err) + } + if got := string(list); got != test.want { + t.Errorf("%q/@v/list: got %q, want %q", test.modulePath, got, test.want) + } + } +} |