// SPDX-License-Identifier: GPL-3.0-or-later package version import ( "runtime/debug" "testing" ) func TestStampedVersionWins(t *testing.T) { old := Version t.Cleanup(func() { Version = old }) Version = "1.2.3" if got := String(); got != "1.2.3" { t.Errorf("String() = %q, want the stamped version", got) } } func TestDevVersion(t *testing.T) { read := func(settings ...debug.BuildSetting) func() (*debug.BuildInfo, bool) { return func() (*debug.BuildInfo, bool) { return &debug.BuildInfo{Settings: settings}, true } } if got := devVersion(func() (*debug.BuildInfo, bool) { return nil, false }); got != "0.0.0-dev" { t.Errorf("no build info = %q", got) } if got := devVersion(read()); got != "0.0.0-dev" { t.Errorf("no vcs = %q", got) } if got := devVersion(read(debug.BuildSetting{Key: "vcs.revision", Value: "0123456789abcdef"})); got != "0.0.0-dev+0123456" { t.Errorf("with revision = %q", got) } if got := devVersion(read(debug.BuildSetting{Key: "vcs.revision", Value: "0123456789abcdef"}, debug.BuildSetting{Key: "vcs.modified", Value: "true"})); got != "0.0.0-dev+0123456.dirty" { t.Errorf("dirty = %q", got) } } func TestVersionIsNotHardcoded(t *testing.T) { // An unstamped test binary must report a dev version: a literal release number in source would break "one source". if Version != "" { t.Fatalf("Version must default to empty, got %q", Version) } if got := String(); len(got) < 9 || got[:9] != "0.0.0-dev" { t.Errorf("unstamped String() = %q", got) } }