| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package proxy |
| 6 |
|
| 7 |
import ( |
| 8 |
"archive/zip" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
) |
| 16 |
|
| 17 |
func TestWriteModuleVersion(t *testing.T) { |
| 18 |
tests := []struct { |
| 19 |
modulePath, version string |
| 20 |
files map[string][]byte |
| 21 |
}{ |
| 22 |
{ |
| 23 |
modulePath: "mod.test/module", |
| 24 |
version: "v1.2.3", |
| 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"), |
| 28 |
}, |
| 29 |
}, |
| 30 |
{ |
| 31 |
modulePath: "mod.test/module", |
| 32 |
version: "v1.2.4", |
| 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"), |
| 36 |
}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
modulePath: "mod.test/nogomod", |
| 40 |
version: "v0.9.0", |
| 41 |
files: map[string][]byte{ |
| 42 |
"const.go": []byte("package module\n\nconst Other = \"Other\""), |
| 43 |
}, |
| 44 |
}, |
| 45 |
} |
| 46 |
dir := t.TempDir() |
| 47 |
defer os.RemoveAll(dir) |
| 48 |
for _, test := range tests { |
| 49 |
|
| 50 |
if err := writeModuleVersion(dir, test.modulePath, test.version, test.files); err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
rootDir := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v") |
| 54 |
gomod, err := os.ReadFile(filepath.Join(rootDir, test.version+".mod")) |
| 55 |
if err != nil { |
| 56 |
t.Fatal(err) |
| 57 |
} |
| 58 |
wantMod, ok := test.files["go.mod"] |
| 59 |
if !ok { |
| 60 |
wantMod = []byte("module " + test.modulePath) |
| 61 |
} |
| 62 |
if got, want := string(gomod), string(wantMod); got != want { |
| 63 |
t.Errorf("reading %s/@v/%s.mod: got %q, want %q", test.modulePath, test.version, got, want) |
| 64 |
} |
| 65 |
zr, err := zip.OpenReader(filepath.Join(rootDir, test.version+".zip")) |
| 66 |
if err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
defer zr.Close() |
| 70 |
|
| 71 |
for _, zf := range zr.File { |
| 72 |
r, err := zf.Open() |
| 73 |
if err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} |
| 76 |
defer r.Close() |
| 77 |
content, err := io.ReadAll(r) |
| 78 |
if err != nil { |
| 79 |
t.Fatal(err) |
| 80 |
} |
| 81 |
name := strings.TrimPrefix(zf.Name, fmt.Sprintf("%s@%s/", test.modulePath, test.version)) |
| 82 |
if got, want := string(content), string(test.files[name]); got != want { |
| 83 |
t.Errorf("unzipping %q: got %q, want %q", zf.Name, got, want) |
| 84 |
} |
| 85 |
delete(test.files, name) |
| 86 |
} |
| 87 |
for name := range test.files { |
| 88 |
t.Errorf("file %q not present in the module zip", name) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
lists := []struct { |
| 93 |
modulePath, want string |
| 94 |
}{ |
| 95 |
{"mod.test/module", "v1.2.3\nv1.2.4\n"}, |
| 96 |
{"mod.test/nogomod", "v0.9.0\n"}, |
| 97 |
} |
| 98 |
|
| 99 |
for _, test := range lists { |
| 100 |
fp := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v", "list") |
| 101 |
list, err := os.ReadFile(fp) |
| 102 |
if err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
if got := string(list); got != test.want { |
| 106 |
t.Errorf("%q/@v/list: got %q, want %q", test.modulePath, got, test.want) |
| 107 |
} |
| 108 |
} |
| 109 |
} |