whatcanGOwrong

This commit is contained in:
2024-09-19 21:38:24 -04:00
commit d0ae4d841d
17908 changed files with 4096831 additions and 0 deletions
@@ -0,0 +1,99 @@
// 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 provides functions for writing module data to a directory
// in proxy format, so that it can be used as a module proxy by setting
// GOPROXY="file://<dir>".
package proxydir
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/internal/testenv"
)
// WriteModuleVersion creates a directory in the proxy dir for a module.
func WriteModuleVersion(rootDir, module, ver string, files map[string][]byte) (rerr error) {
dir := filepath.Join(rootDir, module, "@v")
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
// The go command checks for versions by looking at the "list" file. Since
// we are supporting multiple versions, create this file if it does not exist
// or append the version number to the preexisting file.
f, err := os.OpenFile(filepath.Join(dir, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer checkClose("list file", f, &rerr)
if _, err := f.WriteString(ver + "\n"); err != nil {
return err
}
// Serve the go.mod file on the <version>.mod url, if it exists. Otherwise,
// serve a stub.
modContents, ok := files["go.mod"]
if !ok {
modContents = []byte("module " + module)
}
if err := os.WriteFile(filepath.Join(dir, ver+".mod"), modContents, 0644); err != nil {
return err
}
// info file, just the bare bones.
infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver))
if err := os.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil {
return err
}
// zip of all the source files.
f, err = os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer checkClose("zip file", f, &rerr)
z := zip.NewWriter(f)
defer checkClose("zip writer", z, &rerr)
for name, contents := range files {
zf, err := z.Create(module + "@" + ver + "/" + name)
if err != nil {
return err
}
if _, err := zf.Write(contents); err != nil {
return err
}
}
return nil
}
func checkClose(name string, closer io.Closer, err *error) {
if cerr := closer.Close(); cerr != nil && *err == nil {
*err = fmt.Errorf("closing %s: %v", name, cerr)
}
}
// ToURL returns the file uri for a proxy directory.
func ToURL(dir string) string {
if testenv.Go1Point() >= 13 {
// file URLs on Windows must start with file:///. See golang.org/issue/6027.
path := filepath.ToSlash(dir)
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return "file://" + path
} else {
// Prior to go1.13, the Go command on Windows only accepted GOPROXY file URLs
// of the form file://C:/path/to/proxy. This was incorrect: when parsed, "C:"
// is interpreted as the host. See golang.org/issue/6027. This has been
// fixed in go1.13, but we emit the old format for old releases.
return "file://" + filepath.ToSlash(dir)
}
}
@@ -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"
"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 := os.MkdirTemp("", "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 := os.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 := io.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 := os.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)
}
}
}