Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): add GoVersion package variable #283

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion v2/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@

package gax

import "bytes"
import (
"bytes"
"runtime"
"strings"
"unicode"
)

// XGoogHeader is for use by the Google Cloud Libraries only.
//
Expand All @@ -51,3 +56,52 @@ func XGoogHeader(keyval ...string) string {
}
return buf.String()[1:]
}

// version is a package internal global variable for testing purposes.
var version = runtime.Version

// VersionUnknown is only used when the runtime version cannot be determined.
const VersionUnknown = "UNKNOWN"
noahdietz marked this conversation as resolved.
Show resolved Hide resolved

// GoVersion returns a Go runtime version derived from the runtime environment
// that is modified to be suitable for reporting in a header, meaning it has no
// whitespace. If it is unable to determine the Go runtime version, it returns
// VersionUnknown.
func GoVersion() string {
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
const develPrefix = "devel +"

s := version()
if strings.HasPrefix(s, develPrefix) {
s = s[len(develPrefix):]
if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 {
s = s[:p]
}
return s
}

notSemverRune := func(r rune) bool {
return !strings.ContainsRune("0123456789.", r)
}

if strings.HasPrefix(s, "go1") {
s = s[2:]
var prerelease string
if p := strings.IndexFunc(s, notSemverRune); p >= 0 {
s, prerelease = s[:p], s[p:]
}
if strings.HasSuffix(s, ".") {
s += "0"
} else if strings.Count(s, ".") < 2 {
s += ".0"
}
if prerelease != "" {
// Some release candidates already have a dash in them.
if !strings.HasPrefix(prerelease, "-") {
prerelease = "-" + prerelease
}
s += prerelease
}
return s
}
return "UNKNOWN"
}
41 changes: 40 additions & 1 deletion v2/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

package gax

import "testing"
import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestXGoogHeader(t *testing.T) {
for _, tst := range []struct {
Expand All @@ -46,3 +50,38 @@ func TestXGoogHeader(t *testing.T) {
}
}
}

func TestGoVersion(t *testing.T) {
testVersion := func(v string) func() string {
return func() string {
return v
}
}
for _, tst := range []struct {
v func() string
want string
}{
{
testVersion("go1.19"),
"1.19.0",
},
{
testVersion("go1.21-20230317-RC01"),
"1.21.0-20230317-RC01",
},
{
testVersion("devel +abc1234"),
"abc1234",
},
{
testVersion("this should be unknown"),
VersionUnknown,
},
} {
version = tst.v
got := GoVersion()
if diff := cmp.Diff(got, tst.want); diff != "" {
t.Errorf("got(-),want(+):\n%s", diff)
}
}
}