-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This CL factors out the version-determination logic from cmd/cue. This will enable it to be used by other packages, including putting inside the User-Agent header when making HTTP requests. This does make one of the existing cmd/cue tests not possible to write, but the logic is simple enough (and it doesn't take any additional code paths) that this seems like it should be OK. Signed-off-by: Roger Peppe <rogpeppe@gmail.com> Change-Id: Ic2accbe29d64bf9cc62cdca413c66edd1ce19e4b Dispatch-Trailer: {"type":"trybot","CL":1178030,"patchset":2,"ref":"refs/changes/30/1178030/2","targetBranch":"master"}
- Loading branch information
Showing
5 changed files
with
69 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package cueversion provides access to the version of the | ||
// cuelang.org/go module. | ||
package cueversion | ||
|
||
import ( | ||
"runtime/debug" | ||
"sync" | ||
) | ||
|
||
// fallbackVersion is used when there isn't a recorded main module | ||
// version, for example when building via `go install ./cmd/cue`. It | ||
// should reflect the last release in the current branch. | ||
// | ||
// TODO: remove once Go stamps local builds with a main module version | ||
// derived from the local VCS information per | ||
// https://go.dev/issue/50603. | ||
const fallbackVersion = "v0.8.0-alpha.5" | ||
|
||
// Version returns the version of the cuelang.org/go module as best | ||
// as can reasonably be determined. The result is always a valid Go | ||
// semver version. | ||
func Version() string { | ||
return versionOnce() | ||
} | ||
|
||
var versionOnce = sync.OnceValue(func() string { | ||
bi, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return fallbackVersion | ||
} | ||
switch bi.Main.Version { | ||
case "": // missing version | ||
case "(devel)": // local build | ||
case "v0.0.0-00010101000000-000000000000": // build via a directory replace directive | ||
default: | ||
return bi.Main.Version | ||
} | ||
return fallbackVersion | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cueversion | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-quicktest/qt" | ||
"golang.org/x/mod/semver" | ||
) | ||
|
||
func TestVersion(t *testing.T) { | ||
// This is just a smoke test to make sure that things | ||
// are wired up OK. It would be possible to unit | ||
// test the logic inside Version, but it's simple | ||
// enough that that would amount to creating invariants | ||
// that just match the code, not providing any more | ||
// assurance of correctness. | ||
vers := Version() | ||
qt.Assert(t, qt.Satisfies(vers, semver.IsValid)) | ||
} |