Vectrum
Sign In
48 lines · 1.8 KB · Go
package main

import (
	"runtime/debug"
	"testing"
)

func TestResolveVersionTriesStampThenModuleThenDefault(t *testing.T) {
	info := func(v string) func() (*debug.BuildInfo, bool) {
		return func() (*debug.BuildInfo, bool) {
			return &debug.BuildInfo{Main: debug.Module{Path: "github.com/johannSo/codey", Version: v}}, true
		}
	}
	none := func() (*debug.BuildInfo, bool) { return nil, false }

	cases := []struct {
		name    string
		stamped string
		info    func() (*debug.BuildInfo, bool)
		want    string
	}{
		{"goreleaser stamp, which has no v", "0.7.0", info("v0.7.0"), "v0.7.0"},
		{"a stamp wins over the module", "v0.8.0", info("v0.7.0"), "v0.8.0"},
		{"snapshot stamp", "0.7.1-SNAPSHOT-abc1234", none, "v0.7.1-SNAPSHOT-abc1234"},
		{"go install at a tag", "", info("v0.7.0"), "v0.7.0"},
		{"go install at a pre-release tag", "", info("v0.7.0-rc.1"), "v0.7.0-rc.1"},
		{"go build with no vcs info", "", info("(devel)"), defaultVersion},
		{"go build with no tag in reach", "", info("v0.0.0-20261002101500-219352e3b1c4"), defaultVersion},
		{"go build past a tag", "", info("v0.7.1-0.20261002101500-219352e3b1c4"), defaultVersion},
		{"go build with local changes", "", info("v0.7.0+dirty"), defaultVersion},
		{"no build info at all", "", none, defaultVersion},
	}
	for _, c := range cases {
		if got := resolveVersion(c.stamped, c.info); got != c.want {
			t.Errorf("%s: resolveVersion(%q) = %q, want %q", c.name, c.stamped, got, c.want)
		}
	}
}

// Every answer resolveVersion can give still has to satisfy the client's
// pattern, or a release would read to T3 Code as a broken install.
func TestEveryVersionShapeMatchesWhatTheClientLooksFor(t *testing.T) {
	for _, v := range []string{defaultVersion, "v0.7.0", "v0.7.0-rc.1", "v0.7.1-SNAPSHOT-abc1234"} {
		if !t3VersionPattern.MatchString("codey " + v) {
			t.Errorf("%q does not match %s", v, t3VersionPattern)
		}
	}
}