Take the version from the git tag, not a constant (closes #80)
check / check (push) Waiting to run

`mfer version`, `mfer --version` and the banner print the revision the
build stamps into main.Gitrev or, when it stamped none (as after
`go install` of a tag), the main module's version from
runtime/debug.ReadBuildInfo. The banner no longer shows a release date.

mfer.Version and mfer.ReleaseDate go, with main.Version and the version
field and parameter in internal/cli that nothing read. A program using
the library reads the module version from its own build information.
The README says where the version comes from.

Judgement call: a binary with no build information prints "unknown",
the word script/build already uses when git describe gives nothing.

Model: opus-5-5
This commit is contained in:
2026-10-13 16:53:03 +00:00
parent 61238e4071
commit c0fdb15082
7 changed files with 56 additions and 36 deletions
+24 -6
View File
@@ -11,6 +11,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"runtime/debug"
"slices"
"strings"
"sync"
@@ -27,6 +28,7 @@ import (
const (
testApp = "mfer"
testGitrev = "abc123"
testDir = "/testdir"
testFile1 = "/testdir/file1.txt"
testMF = "/testdir/test.mf"
@@ -93,8 +95,7 @@ func TestBuild(t *testing.T) {
func testOpts(args []string, fs afero.Fs) *RunOptions {
return &RunOptions{
Appname: testApp,
Version: "1.0.0",
Gitrev: "abc123",
Gitrev: testGitrev,
Args: args,
Stdin: &bytes.Buffer{},
Stdout: &bytes.Buffer{},
@@ -136,10 +137,24 @@ func TestVersionCommand(t *testing.T) {
exitCode := runCLI(opts)
assert.Equal(t, 0, exitCode)
assert.Equal(t, "mfer version "+testGitrev+"\n", testStdout(t, opts))
}
stdout := testStdout(t, opts)
assert.Contains(t, stdout, mfer.Version)
assert.Contains(t, stdout, "abc123")
// TestVersionFromBuildInfo covers a binary whose build stamped no Gitrev,
// as `go install` builds it: mfer version prints the main module's version
// from the build information instead.
func TestVersionFromBuildInfo(t *testing.T) {
t.Parallel()
info, ok := debug.ReadBuildInfo()
require.True(t, ok)
require.NotEmpty(t, info.Main.Version)
opts := testOpts([]string{testApp, cmdVersion}, afero.NewMemMapFs())
opts.Gitrev = ""
require.Equal(t, 0, runCLI(opts))
assert.Equal(t, "mfer version "+info.Main.Version+"\n", testStdout(t, opts))
}
// TestVFlagCollision covers the -v/--verbose vs --version flag interaction
@@ -165,7 +180,7 @@ func TestVFlagCollision(t *testing.T) {
exitCode := runCLI(opts)
assert.Equal(t, 0, exitCode, "stderr: %s", testStderr(t, opts))
assert.Contains(t, testStdout(t, opts), mfer.Version)
assert.Contains(t, testStdout(t, opts), testGitrev)
assert.NotContains(t, testStderr(t, opts), "two forms of the same flag")
})
}
@@ -739,6 +754,9 @@ func TestBannerOutput(t *testing.T) {
stdout := testStdout(t, opts)
assert.Contains(t, stdout, "___")
assert.Contains(t, stdout, "\\")
// The banner shows the version mfer version prints, and nothing after it.
assert.Contains(t, stdout, "mfer by @sneak: "+testGitrev+"\n")
}
func TestUnknownCommand(t *testing.T) {