turbo-editors/turbo-corepublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on main · k33g · 4h ago
version_test.go · 230 lines · 7.2 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package version

import (
	"runtime/debug"
	"testing"
)

// buildInfo returns a *debug.BuildInfo describing a build, so that every case
// can be exercised without linker flags — which is the whole point of keeping
// resolve separate from Current.
func buildInfo(mainVersion string, settings map[string]string) *debug.BuildInfo {
	info := &debug.BuildInfo{}
	info.Main.Version = mainVersion
	for key, value := range settings {
		info.Settings = append(info.Settings, debug.BuildSetting{Key: key, Value: value})
	}
	return info
}

func TestAStampedBuildReportsWhatTheLinkerGaveIt(t *testing.T) {
	got := resolve("v0.2.0", "88a4c38ff11", "2026-08-31T18:04:05Z", buildInfo("(devel)", nil))

	want := Info{Number: "0.2.0", Commit: "88a4c38", Built: "2026-08-31T18:04:05Z"}
	if got != want {
		t.Errorf("resolve() = %+v, want %+v", got, want)
	}
}

func TestTheLinkerOutranksTheBuildSystem(t *testing.T) {
	// A release binary is built from a checkout, so both sources have an
	// answer; only the linker's knows the tag.
	got := resolve("v0.2.0", "", "", buildInfo("(devel)", map[string]string{
		"vcs.revision": "88a4c38ff11",
	}))

	if got.Number != "0.2.0" {
		t.Errorf("Number = %q, want the stamped 0.2.0", got.Number)
	}
}

func TestAnInstalledModuleReportsItsModuleVersion(t *testing.T) {
	// `go install <module>@v0.2.0` records the version and no VCS information
	// at all, because there was no repository to read.
	got := resolve("", "", "", buildInfo("v0.2.0", nil))

	want := Info{Number: "0.2.0"}
	if got != want {
		t.Errorf("resolve() = %+v, want %+v", got, want)
	}
}

func TestAnUnstampedCheckoutBuildIsCalledDevel(t *testing.T) {
	// The build system does not read tags, so no tag can be reported here.
	got := resolve("", "", "", buildInfo("(devel)", map[string]string{
		"vcs.revision": "88a4c38ff11",
		"vcs.modified": "false",
	}))

	want := Info{Number: "devel", Commit: "88a4c38"}
	if got != want {
		t.Errorf("resolve() = %+v, want %+v", got, want)
	}
}

func TestAnUnstampedDirtyCheckoutSaysSo(t *testing.T) {
	got := resolve("", "", "", buildInfo("(devel)", map[string]string{
		"vcs.revision": "88a4c38ff11",
		"vcs.modified": "true",
	}))

	if got.Number != "devel-dirty" {
		t.Errorf("Number = %q, want devel-dirty", got.Number)
	}
}

func TestABuildWithNothingRecordedIsUnknown(t *testing.T) {
	// Not "0.1.0": a stale number printed as fact is the defect this package
	// exists to remove.
	if got := resolve("", "", "", nil); got.Number != unknownNumber {
		t.Errorf("Number = %q, want %q", got.Number, unknownNumber)
	}
	if got := resolve("", "", "", buildInfo("", nil)); got.Number != unknownNumber {
		t.Errorf("with an empty build info, Number = %q, want %q", got.Number, unknownNumber)
	}
}

func TestTheCommitDoesNotComeFromNowhere(t *testing.T) {
	// An unstamped build with no revision must not invent one.
	if got := resolve("", "", "", buildInfo("v0.2.0", nil)); got.Commit != "" {
		t.Errorf("Commit = %q, want nothing", got.Commit)
	}
}

func TestTheCommitDateIsNotUsedAsABuildDate(t *testing.T) {
	// vcs.time is when the commit was made. Reporting it as "Built" would be
	// wrong on every binary built later than its own commit, which is all of
	// them.
	got := resolve("", "", "", buildInfo("(devel)", map[string]string{
		"vcs.revision": "88a4c38ff11",
		"vcs.time":     "2026-08-30T09:00:00Z",
	}))

	if got.Built != "" {
		t.Errorf("Built = %q, want nothing", got.Built)
	}
}

func TestOnlyAVersionsLeadingVIsDropped(t *testing.T) {
	cases := map[string]string{
		"v0.2.0":             "0.2.0",
		"v0.1.0-14-g88a4c38": "0.1.0-14-g88a4c38",
		"0.2.0":              "0.2.0",
		"devel":              "devel",   // not "el"
		"version":            "version", // not "ersion"
		"v":                  "v",
		"":                   "",
	}
	for given, want := range cases {
		if got := trimV(given); got != want {
			t.Errorf("trimV(%q) = %q, want %q", given, got, want)
		}
	}
}

func TestARevisionIsAbbreviatedTheWayGitDoes(t *testing.T) {
	if got := shorten("88a4c38ff1122334455"); got != "88a4c38" {
		t.Errorf("shorten() = %q, want 88a4c38", got)
	}
	if got := shorten("88a4c3"); got != "88a4c3" {
		t.Errorf("a revision shorter than the limit was changed: %q", got)
	}
	if got := shorten(""); got != "" {
		t.Errorf("shorten(\"\") = %q, want nothing", got)
	}
}

func TestStringShowsEveryPartThatIsKnown(t *testing.T) {
	cases := []struct {
		name string
		info Info
		want string
	}{
		{"everything", Info{"0.2.0", "88a4c38", "2026-08-31T18:04:05Z"}, "0.2.0 (88a4c38, built 2026-08-31T18:04:05Z)"},
		{"commit only", Info{Number: "0.2.0", Commit: "88a4c38"}, "0.2.0 (88a4c38)"},
		{"date only", Info{Number: "0.2.0", Built: "2026-08-31T18:04:05Z"}, "0.2.0 (built 2026-08-31T18:04:05Z)"},
		{"number only", Info{Number: "0.2.0"}, "0.2.0"},
	}
	for _, one := range cases {
		t.Run(one.name, func(t *testing.T) {
			if got := one.info.String(); got != one.want {
				t.Errorf("String() = %q, want %q", got, one.want)
			}
		})
	}
}

func TestBuiltAtIsReadable(t *testing.T) {
	if got := (Info{Built: "2026-08-31T18:04:05Z"}).BuiltAt(); got != "2026-08-31 18:04 UTC" {
		t.Errorf("BuiltAt() = %q", got)
	}
}

func TestBuiltAtNormalisesToUTC(t *testing.T) {
	// The Makefile stamps UTC, but a hand-run build might not.
	if got := (Info{Built: "2026-08-31T20:04:05+02:00"}).BuiltAt(); got != "2026-08-31 18:04 UTC" {
		t.Errorf("BuiltAt() = %q, want the same moment in UTC", got)
	}
}

func TestBuiltAtShowsAMalformedStampRatherThanHidingIt(t *testing.T) {
	if got := (Info{Built: "last Tuesday"}).BuiltAt(); got != "last Tuesday" {
		t.Errorf("BuiltAt() = %q, want the stamp as given", got)
	}
	if got := (Info{}).BuiltAt(); got != "" {
		t.Errorf("BuiltAt() with no stamp = %q, want nothing", got)
	}
}

func TestCurrentAnswersWithoutPanicking(t *testing.T) {
	// A test binary is one of the builds resolve has to cope with, and its
	// number is never empty whatever the build system recorded.
	if got := Current(); got.Number == "" {
		t.Error("Current() reported an empty version")
	}
}

func TestEveryFormOfPseudoVersionIsRecognised(t *testing.T) {
	// The three shapes the Go tool produces differ in what precedes the
	// timestamp: a dash when there is no base tag, a dot when the base is a
	// release or a pre-release.
	pseudo := []string{
		"0.0.0-20260831165958-88a4c3859bf3",
		"0.1.1-0.20260831165958-88a4c3859bf3",
		"0.1.1-0.20260831165958-88a4c3859bf3+dirty",
		"0.2.0-pre.0.20260831165958-88a4c3859bf3",
	}
	for _, number := range pseudo {
		if !isPseudoVersion(number) {
			t.Errorf("isPseudoVersion(%q) = false, want true", number)
		}
	}

	real := []string{
		"0.2.0",
		"0.2.0-rc.1",
		"devel",
		"0.1.0-14-g88a4c38",
		"",
		"0.0.0-20260831165958-88a4c3859bZ", // not hex
		"0.0.0-2026083116595-88a4c3859bf3", // 13 digits
	}
	for _, number := range real {
		if isPseudoVersion(number) {
			t.Errorf("isPseudoVersion(%q) = true, want false", number)
		}
	}
}

func TestAPseudoVersionIsReportedAsDevel(t *testing.T) {
	// "0.1.1" in a pseudo-version is a patch release that does not exist.
	got := resolve("", "", "", buildInfo("v0.1.1-0.20260831165958-88a4c3859bf3+dirty", map[string]string{
		"vcs.revision": "88a4c3859bf3e8cb",
		"vcs.modified": "true",
	}))

	want := Info{Number: "devel-dirty", Commit: "88a4c38"}
	if got != want {
		t.Errorf("resolve() = %+v, want %+v", got, want)
	}
}