internal/frontend: fix major version for std

Display the major version as "go1", not "v1".

Also, fix the tooltip.

Fixes b/143529585.
Fixes b/143613230.

Change-Id: I3379e8295a90f79c375f6e54e3593eee2391a335
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/589902
CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Julie Qiu <julieqiu@google.com>
This commit is contained in:
Jonathan Amsterdam 2019-11-01 16:30:21 -04:00 коммит произвёл Julie Qiu
Родитель 34a56cf3dc
Коммит b322ae09c0
4 изменённых файлов: 52 добавлений и 8 удалений

Просмотреть файл

@ -160,6 +160,13 @@ func buildVersionDetails(currentModulePath string, versions []*internal.VersionI
// we detect a +incompatible version (when the path version does not match
// the sematic version), we prefer the path version.
major := semver.Major(v.Version)
if v.ModulePath == stdlib.ModulePath {
var err error
major, err = stdlib.MajorVersionForVersion(v.Version)
if err != nil {
panic(err)
}
}
if _, pathMajor, ok := module.SplitPathVersion(v.ModulePath); ok {
// We prefer the path major version except for v1 import paths where the
// semver major version is v0. In this case, we prefer the more specific
@ -169,7 +176,7 @@ func buildVersionDetails(currentModulePath string, versions []*internal.VersionI
// Trim both '/' and '.' from the path major version to account for
// standard and gopkg.in module paths.
major = strings.TrimLeft(pathMajor, "/.")
} else if major != "v0" {
} else if major != "v0" && !strings.HasPrefix(major, "go") {
major = "v1"
}
}
@ -187,14 +194,15 @@ func buildVersionDetails(currentModulePath string, versions []*internal.VersionI
majorTree.forEach(func(_ string, minorTree *versionTree) {
patches := []*VersionSummary{}
minorTree.forEach(func(_ string, patchTree *versionTree) {
var formattedVersion string
version := patchTree.versionInfo.Version
formattedVersion := formatVersion(version)
if patchTree.versionInfo.ModulePath == stdlib.ModulePath {
formattedVersion = goTagForVersion(patchTree.versionInfo.Version)
} else {
formattedVersion = formatVersion(patchTree.versionInfo.Version)
version = formattedVersion
}
patches = append(patches, &VersionSummary{
Version: patchTree.versionInfo.Version,
Version: version,
Link: linkify(patchTree.versionInfo),
CommitTime: elapsedTime(patchTree.versionInfo.CommitTime),
FormattedVersion: formattedVersion,

Просмотреть файл

@ -12,7 +12,6 @@ import (
"golang.org/x/discovery/internal"
"golang.org/x/discovery/internal/postgres"
"golang.org/x/discovery/internal/sample"
"golang.org/x/discovery/internal/stdlib"
"golang.org/x/discovery/internal/version"
)
@ -40,7 +39,7 @@ func versionSummaries(path string, versions [][]string, linkify func(path, versi
for j, version := range pointVersions {
var semver, formattedVersion string
if inStdLib(path) {
semver = stdlib.VersionForTag(version)
semver = version
formattedVersion = version
} else {
semver = version
@ -238,7 +237,7 @@ func TestFetchPackageVersionsDetails(t *testing.T) {
wantDetails: &VersionsDetails{
ThisModule: []*MajorVersionGroup{
{
Major: "v1",
Major: "go1",
ModulePath: "std",
Versions: packageVersionSummaries("net/http", "std", [][]string{
{"go1.12.5"},

Просмотреть файл

@ -105,6 +105,22 @@ func TagForVersion(version string) (_ string, err error) {
return goVersion, nil
}
// MajorVersionForVersion returns the Go major version for version.
// E.g. "v1.13.3" => "go1".
func MajorVersionForVersion(version string) (_ string, err error) {
defer derrors.Wrap(&err, "MajorTagForVersion(%q)", version)
tag, err := TagForVersion(version)
if err != nil {
return "", err
}
i := strings.IndexRune(tag, '.')
if i < 0 {
return "", fmt.Errorf("no '.' in go tag %q", tag)
}
return tag[:i], nil
}
// finalDigitsIndex returns the index of the first digit in the sequence of digits ending s.
// If s doesn't end in digits, it returns -1.
func finalDigitsIndex(s string) int {

Просмотреть файл

@ -83,6 +83,27 @@ func TestTagForVersion(t *testing.T) {
}
}
func TestMajorVersionForVersion(t *testing.T) {
for _, test := range []struct {
in string
want string // empty => error
}{
{"", ""},
{"garbage", ""},
{"v1.13.3", "go1"},
{"v1.9.0-rc.2", "go1"},
{"v2.1.3", "go2"},
} {
got, err := MajorVersionForVersion(test.in)
if (err != nil) != (test.want == "") {
t.Errorf("%q: err: got %v, wanted error: %t", test.in, err, test.want == "")
}
if err == nil && got != test.want {
t.Errorf("%q: got %q, want %q", test.in, got, test.want)
}
}
}
func TestZip(t *testing.T) {
UseTestData = true
defer func() { UseTestData = false }()