aboutsummaryrefslogtreecommitdiff
path: root/internal/stdlib
diff options
context:
space:
mode:
authorJulie Qiu <julie@golang.org>2021-06-23 16:36:40 -0700
committerJulie Qiu <julie@golang.org>2021-07-12 21:13:20 +0000
commite6b30bf2f78149bbea1ef607321cdeed1010abc5 (patch)
treefe76ed9ca0f6a83124a1130e797c2da10c04fcfc /internal/stdlib
parent738f3de8876e8c3c274b30b830ab079466e44ae7 (diff)
downloadgo-x-pkgsite-e6b30bf2f78149bbea1ef607321cdeed1010abc5.tar.xz
internal: support std@dev.fuzz
It is now possible to fetch std@dev.fuzz and view /std@dev.fuzz on the frontend. For golang/go#46910 Change-Id: I2fd7259840ba32ff35ca323d07412988e533ffba Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/330413 Trust: Julie Qiu <julie@golang.org> Run-TryBot: Julie Qiu <julie@golang.org> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/stdlib')
-rw-r--r--internal/stdlib/stdlib.go63
-rw-r--r--internal/stdlib/stdlib_test.go17
-rw-r--r--internal/stdlib/testdata/dev.fuzz/LICENSE27
-rw-r--r--internal/stdlib/testdata/dev.fuzz/README.md1
-rw-r--r--internal/stdlib/testdata/dev.fuzz/src/README.vendor54
-rw-r--r--internal/stdlib/testdata/dev.fuzz/src/cmd/README.vendor2
-rw-r--r--internal/stdlib/testdata/dev.fuzz/src/errors/errors.go20
-rw-r--r--internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go53
8 files changed, 208 insertions, 29 deletions
diff --git a/internal/stdlib/stdlib.go b/internal/stdlib/stdlib.go
index 9d9e34e2..faa62a2b 100644
--- a/internal/stdlib/stdlib.go
+++ b/internal/stdlib/stdlib.go
@@ -34,8 +34,13 @@ import (
"github.com/go-git/go-git/v5/storage/memory"
)
-// ModulePath is the name of the module for the standard library.
-const ModulePath = "std"
+const (
+ // ModulePath is the name of the module for the standard library.
+ ModulePath = "std"
+
+ // DevFuzz is the branch name for fuzzing in beta.
+ DevFuzz = "dev.fuzz"
+)
var (
// Regexp for matching go tags. The groups are:
@@ -47,6 +52,12 @@ var (
tagRegexp = regexp.MustCompile(`^go(\d+\.\d+)(\.\d+|)((beta|rc)(\d+))?$`)
)
+// SupportedBranches are the branches of the stdlib repo supported by pkgsite.
+var SupportedBranches = map[string]bool{
+ version.Master: true,
+ DevFuzz: true,
+}
+
// VersionForTag returns the semantic version for the Go tag, or "" if
// tag doesn't correspond to a Go release or beta tag. In special cases,
// when the tag specified is either `latest` or `master` it will return the tag.
@@ -66,7 +77,7 @@ func VersionForTag(tag string) string {
return ""
}
// Special case for latest and master.
- if tag == version.Latest || tag == version.Master {
+ if tag == version.Latest || SupportedBranches[tag] {
return tag
}
m := tagRegexp.FindStringSubmatch(tag)
@@ -91,8 +102,11 @@ func VersionForTag(tag string) string {
func TagForVersion(v string) (_ string, err error) {
defer derrors.Wrap(&err, "TagForVersion(%q)", v)
- // Special case: master => master
- if v == version.Master || strings.HasPrefix(v, "v0.0.0") {
+ // Special case: master => master or dev.fuzz => dev.fuzz
+ if SupportedBranches[v] {
+ return v, nil
+ }
+ if strings.HasPrefix(v, "v0.0.0") {
return version.Master, nil
}
// Special case: v1.0.0 => go1.
@@ -173,8 +187,9 @@ var UseTestData = false
// TestCommitTime is the time used for all commits when UseTestData is true.
var (
- TestCommitTime = time.Date(2019, 9, 4, 1, 2, 3, 0, time.UTC)
- TestVersion = "v0.0.0-20190904010203-89fb59e2e920"
+ TestCommitTime = time.Date(2019, 9, 4, 1, 2, 3, 0, time.UTC)
+ TestMasterVersion = "v0.0.0-20190904010203-89fb59e2e920"
+ TestDevFuzzVersion = "v0.0.0-20190904010203-12de34vf56uz"
)
// getGoRepo returns a repo object for the Go repo at version.
@@ -184,6 +199,8 @@ func getGoRepo(v string) (_ *git.Repository, err error) {
var ref plumbing.ReferenceName
if v == version.Master {
ref = plumbing.HEAD
+ } else if v == DevFuzz {
+ ref = "refs/heads/dev.fuzz"
} else {
tag, err := TagForVersion(v)
if err != nil {
@@ -203,22 +220,24 @@ func getGoRepo(v string) (_ *git.Repository, err error) {
// getTestGoRepo gets a Go repo for testing.
func getTestGoRepo(v string) (_ *git.Repository, err error) {
defer derrors.Wrap(&err, "getTestGoRepo(%q)", v)
- if strings.HasPrefix(v, "v0.0.0") {
+ if v == TestMasterVersion {
v = version.Master
}
-
+ if v == TestDevFuzzVersion {
+ v = DevFuzz
+ }
fs := osfs.New(filepath.Join(testhelper.TestDataPath("testdata"), v))
repo, err := git.Init(memory.NewStorage(), fs)
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("git.Initi: %v", err)
}
wt, err := repo.Worktree()
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("repo.Worktree: %v", err)
}
// Add all files in the directory.
if _, err := wt.Add(""); err != nil {
- return nil, err
+ return nil, fmt.Errorf("wt.Add(): %v", err)
}
_, err = wt.Commit("", &git.CommitOptions{All: true, Author: &object.Signature{
Name: "Joe Random",
@@ -226,7 +245,7 @@ func getTestGoRepo(v string) (_ *git.Repository, err error) {
When: TestCommitTime,
}})
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("wt.Commit: %v", err)
}
return repo, nil
}
@@ -267,7 +286,7 @@ func Versions() (_ []string, err error) {
// Directory returns the directory of the standard library relative to the repo root.
func Directory(v string) string {
if semver.Compare(v, "v1.4.0-beta.1") >= 0 ||
- v == version.Master || strings.HasPrefix(v, "v0.0.0") {
+ SupportedBranches[v] || strings.HasPrefix(v, "v0.0.0") {
return "src"
}
// For versions older than v1.4.0-beta.1, the stdlib is in src/pkg.
@@ -303,7 +322,7 @@ func ZipInfo(requestedVersion string) (resolvedVersion string, err error) {
//
// Zip ignores go.mod files in the standard library, treating it as if it were a
// single module named "std" at the given version.
-func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion2 string, commitTime time.Time, err error) {
+func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion string, commitTime time.Time, err error) {
// This code taken, with modifications, from
// https://github.com/shurcooL/play/blob/master/256/moduleproxy/std/std.go.
defer derrors.Wrap(&err, "stdlib.Zip(%q)", requestedVersion)
@@ -333,8 +352,9 @@ func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion2 string, commi
if err != nil {
return nil, "", time.Time{}, err
}
- if requestedVersion == version.Master {
- requestedVersion = newPseudoVersion("v0.0.0", commit.Committer.When, commit.Hash)
+ resolvedVersion = requestedVersion
+ if SupportedBranches[requestedVersion] {
+ resolvedVersion = newPseudoVersion("v0.0.0", commit.Committer.When, commit.Hash)
}
root, err := repo.TreeObject(commit.TreeHash)
if err != nil {
@@ -347,7 +367,7 @@ func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion2 string, commi
}
// Add files from the stdlib directory.
libdir := root
- for _, d := range strings.Split(Directory(requestedVersion), "/") {
+ for _, d := range strings.Split(Directory(resolvedVersion), "/") {
libdir, err = subTree(repo, libdir, d)
if err != nil {
return nil, "", time.Time{}, err
@@ -364,7 +384,7 @@ func Zip(requestedVersion string) (_ *zip.Reader, resolvedVersion2 string, commi
if err != nil {
return nil, "", time.Time{}, err
}
- return zr, requestedVersion, commit.Committer.When, nil
+ return zr, resolvedVersion, commit.Committer.When, nil
}
func newPseudoVersion(version string, commitTime time.Time, hash plumbing.Hash) string {
@@ -378,8 +398,8 @@ func newPseudoVersion(version string, commitTime time.Time, hash plumbing.Hash)
func semanticVersion(requestedVersion string) (_ string, err error) {
defer derrors.Wrap(&err, "semanticVersion(%q)", requestedVersion)
- if requestedVersion == version.Master {
- return version.Master, nil
+ if SupportedBranches[requestedVersion] {
+ return requestedVersion, nil
}
knownVersions, err := Versions()
@@ -532,6 +552,7 @@ var testRefs = []plumbing.ReferenceName{
"refs/tags/go1.13",
"refs/tags/go1.13beta1",
"refs/tags/go1.14.6",
+ "refs/heads/dev.fuzz",
"refs/heads/master",
// other tags
"refs/changes/56/93156/13",
diff --git a/internal/stdlib/stdlib_test.go b/internal/stdlib/stdlib_test.go
index 0e99c900..812e29af 100644
--- a/internal/stdlib/stdlib_test.go
+++ b/internal/stdlib/stdlib_test.go
@@ -62,11 +62,6 @@ func TestTagForVersion(t *testing.T) {
want: "master",
},
{
- name: "master version",
- version: TestVersion,
- want: "master",
- },
- {
name: "bad std semver",
version: "v1.x",
wantErr: true,
@@ -125,13 +120,19 @@ func TestMajorVersionForVersion(t *testing.T) {
func TestZip(t *testing.T) {
UseTestData = true
defer func() { UseTestData = false }()
- for _, resolvedVersion := range []string{"v1.14.6", "v1.12.5", "v1.3.2", TestVersion} {
+ for _, resolvedVersion := range []string{
+ "v1.3.2",
+ "v1.12.5",
+ "v1.14.6",
+ DevFuzz,
+ version.Master,
+ } {
t.Run(resolvedVersion, func(t *testing.T) {
zr, gotResolvedVersion, gotTime, err := Zip(resolvedVersion)
if err != nil {
t.Fatal(err)
}
- if resolvedVersion == "master" {
+ if SupportedBranches[resolvedVersion] {
if !version.IsPseudo(gotResolvedVersion) {
t.Errorf("resolved version: %s is not a pseudo-version", gotResolvedVersion)
}
@@ -146,7 +147,7 @@ func TestZip(t *testing.T) {
"errors/errors.go": true,
"errors/errors_test.go": true,
}
- if semver.Compare(resolvedVersion, "v1.13.0") > 0 || resolvedVersion == TestVersion {
+ if semver.Compare(resolvedVersion, "v1.13.0") > 0 || resolvedVersion == TestMasterVersion {
wantFiles["cmd/README.vendor"] = true
}
diff --git a/internal/stdlib/testdata/dev.fuzz/LICENSE b/internal/stdlib/testdata/dev.fuzz/LICENSE
new file mode 100644
index 00000000..6a66aea5
--- /dev/null
+++ b/internal/stdlib/testdata/dev.fuzz/LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) 2009 The Go Authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ * Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+ * Neither the name of Google Inc. nor the names of its
+contributors may be used to endorse or promote products derived from
+this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/internal/stdlib/testdata/dev.fuzz/README.md b/internal/stdlib/testdata/dev.fuzz/README.md
new file mode 100644
index 00000000..d6d2b9d2
--- /dev/null
+++ b/internal/stdlib/testdata/dev.fuzz/README.md
@@ -0,0 +1 @@
+# The Go Programming Language
diff --git a/internal/stdlib/testdata/dev.fuzz/src/README.vendor b/internal/stdlib/testdata/dev.fuzz/src/README.vendor
new file mode 100644
index 00000000..e74fc2f3
--- /dev/null
+++ b/internal/stdlib/testdata/dev.fuzz/src/README.vendor
@@ -0,0 +1,54 @@
+Vendoring in std and cmd
+========================
+
+The Go command maintains copies of external packages needed by the
+standard library in the src/vendor and src/cmd/vendor directories.
+
+In GOPATH mode, imports of vendored packages are resolved to these
+directories following normal vendor directory logic
+(see golang.org/s/go15vendor).
+
+In module mode, std and cmd are modules (defined in src/go.mod and
+src/cmd/go.mod). When a package outside std or cmd is imported
+by a package inside std or cmd, the import path is interpreted
+as if it had a "vendor/" prefix. For example, within "crypto/tls",
+an import of "golang.org/x/crypto/cryptobyte" resolves to
+"vendor/golang.org/x/crypto/cryptobyte". When a package with the
+same path is imported from a package outside std or cmd, it will
+be resolved normally. Consequently, a binary may be built with two
+copies of a package at different versions if the package is
+imported normally and vendored by the standard library.
+
+Vendored packages are internally renamed with a "vendor/" prefix
+to preserve the invariant that all packages have distinct paths.
+This is necessary to avoid compiler and linker conflicts. Adding
+a "vendor/" prefix also maintains the invariant that standard
+library packages begin with a dotless path element.
+
+The module requirements of std and cmd do not influence version
+selection in other modules. They are only considered when running
+module commands like 'go get' and 'go mod vendor' from a directory
+in GOROOT/src.
+
+Maintaining vendor directories
+==============================
+
+Before updating vendor directories, ensure that module mode is enabled.
+Make sure GO111MODULE=off is not set ('on' or 'auto' should work).
+
+Requirements may be added, updated, and removed with 'go get'.
+The vendor directory may be updated with 'go mod vendor'.
+A typical sequence might be:
+
+ cd src
+ go get -d golang.org/x/net@latest
+ go mod tidy
+ go mod vendor
+
+Use caution when passing '-u' to 'go get'. The '-u' flag updates
+modules providing all transitively imported packages, not only
+the module providing the target package.
+
+Note that 'go mod vendor' only copies packages that are transitively
+imported by packages in the current module. If a new package is needed,
+it should be imported before running 'go mod vendor'.
diff --git a/internal/stdlib/testdata/dev.fuzz/src/cmd/README.vendor b/internal/stdlib/testdata/dev.fuzz/src/cmd/README.vendor
new file mode 100644
index 00000000..ac0df5e9
--- /dev/null
+++ b/internal/stdlib/testdata/dev.fuzz/src/cmd/README.vendor
@@ -0,0 +1,2 @@
+See src/README.vendor for information on loading vendored packages
+and updating the vendor directory.
diff --git a/internal/stdlib/testdata/dev.fuzz/src/errors/errors.go b/internal/stdlib/testdata/dev.fuzz/src/errors/errors.go
new file mode 100644
index 00000000..b8a46921
--- /dev/null
+++ b/internal/stdlib/testdata/dev.fuzz/src/errors/errors.go
@@ -0,0 +1,20 @@
+// Copyright 2011 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 errors implements functions to manipulate errors.
+package errors
+
+// New returns an error that formats as the given text.
+func New(text string) error {
+ return &errorString{text}
+}
+
+// errorString is a trivial implementation of error.
+type errorString struct {
+ s string
+}
+
+func (e *errorString) Error() string {
+ return e.s
+}
diff --git a/internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go b/internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go
new file mode 100644
index 00000000..cf4df90b
--- /dev/null
+++ b/internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go
@@ -0,0 +1,53 @@
+// Copyright 2011 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 errors_test
+
+import (
+ "errors"
+ "fmt"
+ "testing"
+)
+
+func TestNewEqual(t *testing.T) {
+ // Different allocations should not be equal.
+ if errors.New("abc") == errors.New("abc") {
+ t.Errorf(`New("abc") == New("abc")`)
+ }
+ if errors.New("abc") == errors.New("xyz") {
+ t.Errorf(`New("abc") == New("xyz")`)
+ }
+
+ // Same allocation should be equal to itself (not crash).
+ err := errors.New("jkl")
+ if err != err {
+ t.Errorf(`err != err`)
+ }
+}
+
+func TestErrorMethod(t *testing.T) {
+ err := errors.New("abc")
+ if err.Error() != "abc" {
+ t.Errorf(`New("abc").Error() = %q, want %q`, err.Error(), "abc")
+ }
+}
+
+func ExampleNew() {
+ err := errors.New("emit macho dwarf: elf header corrupted")
+ if err != nil {
+ fmt.Print(err)
+ }
+ // Output: emit macho dwarf: elf header corrupted
+}
+
+// The fmt package's Errorf function lets us use the package's formatting
+// features to create descriptive error messages.
+func ExampleNew_errorf() {
+ const name, id = "bimmler", 17
+ err := fmt.Errorf("user %q (id %d) not found", name, id)
+ if err != nil {
+ fmt.Print(err)
+ }
+ // Output: user "bimmler" (id 17) not found
+}