kubo/version_test.go
Marcin Rataj 1e5e6e35cf fix(version): produce shorter user agent for tagged release builds
when building from a version-tagged commit with a clean tree, omit the
redundant git commit hash from the libp2p user agent string, saving
bytes in HTTP requests and libp2p identify. `ipfs version --commit`
still reports the full commit hash.

before (tagged release): kubo/0.37.0/6898472
after  (tagged release): kubo/0.37.0

non-tagged and dirty builds are unaffected.
2026-02-20 18:21:31 +01:00

80 lines
2.0 KiB
Go

package ipfs
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestGetUserAgentVersion verifies the user agent string used in libp2p
// identify and HTTP requests. Tagged release builds (where the commit matches
// the tag) skip the commit hash from the agent version, since the version
// number already identifies the exact source.
func TestGetUserAgentVersion(t *testing.T) {
origCommit := CurrentCommit
origTagged := taggedRelease
origSuffix := userAgentSuffix
t.Cleanup(func() {
CurrentCommit = origCommit
taggedRelease = origTagged
userAgentSuffix = origSuffix
})
tests := []struct {
name string
commit string
tagged string
suffix string
expected string
}{
// dev builds without ldflags
{
name: "no commit, no suffix",
expected: "kubo/" + CurrentVersionNumber,
},
// dev builds with commit set via ldflags
{
name: "with commit",
commit: "abc1234",
expected: "kubo/" + CurrentVersionNumber + "/abc1234",
},
{
name: "with suffix, no commit",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
},
{
name: "with commit and suffix",
commit: "abc1234",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/abc1234/test-suffix",
},
// tagged release builds: commit is redundant because the version
// number already maps to an exact git tag, so it is omitted to
// save bytes in identify and HTTP user-agent headers.
{
name: "tagged release ignores commit",
commit: "abc1234",
tagged: "1",
expected: "kubo/" + CurrentVersionNumber,
},
{
name: "tagged release with suffix ignores commit",
commit: "abc1234",
tagged: "1",
suffix: "test-suffix",
expected: "kubo/" + CurrentVersionNumber + "/test-suffix",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
CurrentCommit = tt.commit
taggedRelease = tt.tagged
SetUserAgentSuffix(tt.suffix)
assert.Equal(t, tt.expected, GetUserAgentVersion())
})
}
}