vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "runtime/debug" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // A release build stamps the version with -ldflags, and `vole version` must |
| 10 | // print it. The v is stripped because the release workflow stamps the tag |
| 11 | // without it. |
| 12 | func TestBuildVersionStamped(t *testing.T) { |
| 13 | previous := version |
| 14 | t.Cleanup(func() { version = previous }) |
| 15 | |
| 16 | for in, want := range map[string]string{ |
| 17 | "1.2.3": "1.2.3", |
| 18 | "v1.2.3": "1.2.3", |
| 19 | "0.1.0-rc5": "0.1.0-rc5", |
| 20 | } { |
| 21 | version = in |
| 22 | if got := buildVersion(); got != want { |
| 23 | t.Fatalf("buildVersion() with stamp %q = %q, want %q", in, got, want) |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // An installation from a module proxy carries the module version in build info. |
| 29 | // It is the release tag, and it prints in the same form as a stamped build. |
| 30 | func TestVersionFromModule(t *testing.T) { |
| 31 | got := versionFromBuildInfo(&debug.BuildInfo{Main: debug.Module{Version: "v0.1.0-rc5"}}) |
| 32 | if got != "0.1.0-rc5" { |
| 33 | t.Fatalf("version = %q, want 0.1.0-rc5", got) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // A source checkout reports the commit Go recorded, shortened and marked dirty |
| 38 | // when the tree is not clean. The pseudo-version Go also stamps into Main.Version |
| 39 | // must not win: it is neither the tag nor the commit. |
| 40 | func TestVersionFromCommit(t *testing.T) { |
| 41 | revision := strings.Repeat("a", 40) |
| 42 | got := versionFromBuildInfo(&debug.BuildInfo{ |
| 43 | Main: debug.Module{Version: "v0.1.0-rc4.0.20261202222536-" + revision[:12] + "+dirty"}, |
| 44 | Settings: []debug.BuildSetting{ |
| 45 | {Key: "vcs.revision", Value: revision}, |
| 46 | {Key: "vcs.modified", Value: "true"}, |
| 47 | }, |
| 48 | }) |
| 49 | if got != strings.Repeat("a", 12)+".dirty" { |
| 50 | t.Fatalf("version = %q, want the short revision with .dirty", got) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Without a stamp, a module version, or VCS metadata (an exported tree built by |
| 55 | // hand), there is no release to claim. |
| 56 | func TestVersionWithoutMetadata(t *testing.T) { |
| 57 | got := versionFromBuildInfo(&debug.BuildInfo{Main: debug.Module{Version: "(devel)"}}) |
| 58 | if got != "devel" { |
| 59 | t.Fatalf("version = %q, want devel", got) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // buildVersion itself must never print the old hardcoded source version. It |
| 64 | // either has a stamp, a module version, or a commit from the test binary. |
| 65 | func TestBuildVersionSourceBuildIsNotAVersionClaim(t *testing.T) { |
| 66 | previous := version |
| 67 | version = "" |
| 68 | t.Cleanup(func() { version = previous }) |
| 69 | |
| 70 | got := buildVersion() |
| 71 | if got == "" || strings.Contains(got, "-devel+") || strings.Contains(got, ".0.20") { |
| 72 | t.Fatalf("buildVersion() = %q, want a commit or devel", got) |
| 73 | } |
| 74 | } |
| 75 |