1 // Copyright 2020 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.
17 func TestWriteModuleVersion(t *testing.T) {
19 modulePath, version string
20 files map[string][]byte
23 modulePath: "mod.test/module",
25 files: map[string][]byte{
26 "go.mod": []byte("module mod.com\n\ngo 1.12"),
27 "const.go": []byte("package module\n\nconst Answer = 42"),
31 modulePath: "mod.test/module",
33 files: map[string][]byte{
34 "go.mod": []byte("module mod.com\n\ngo 1.12"),
35 "const.go": []byte("package module\n\nconst Answer = 43"),
39 modulePath: "mod.test/nogomod",
41 files: map[string][]byte{
42 "const.go": []byte("package module\n\nconst Other = \"Other\""),
46 dir, err := ioutil.TempDir("", "proxydirtest-")
50 defer os.RemoveAll(dir)
51 for _, test := range tests {
52 // Since we later assert on the contents of /list, don't use subtests.
53 if err := WriteModuleVersion(dir, test.modulePath, test.version, test.files); err != nil {
56 rootDir := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v")
57 gomod, err := ioutil.ReadFile(filepath.Join(rootDir, test.version+".mod"))
61 wantMod, ok := test.files["go.mod"]
63 wantMod = []byte("module " + test.modulePath)
65 if got, want := string(gomod), string(wantMod); got != want {
66 t.Errorf("reading %s/@v/%s.mod: got %q, want %q", test.modulePath, test.version, got, want)
68 zr, err := zip.OpenReader(filepath.Join(rootDir, test.version+".zip"))
74 for _, zf := range zr.File {
80 content, err := ioutil.ReadAll(r)
84 name := strings.TrimPrefix(zf.Name, fmt.Sprintf("%s@%s/", test.modulePath, test.version))
85 if got, want := string(content), string(test.files[name]); got != want {
86 t.Errorf("unzipping %q: got %q, want %q", zf.Name, got, want)
88 delete(test.files, name)
90 for name := range test.files {
91 t.Errorf("file %q not present in the module zip", name)
96 modulePath, want string
98 {"mod.test/module", "v1.2.3\nv1.2.4\n"},
99 {"mod.test/nogomod", "v0.9.0\n"},
102 for _, test := range lists {
103 fp := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v", "list")
104 list, err := ioutil.ReadFile(fp)
108 if got := string(list); got != test.want {
109 t.Errorf("%q/@v/list: got %q, want %q", test.modulePath, got, test.want)