Files
kandev/apps/backend/internal/system/updates/semver_test.go
T
João Salavisa 7e386561f2 fix: treat git-describe builds as ahead of latest release (#1787)
* fix: treat git-describe builds as ahead of latest release

The update checker flagged "Update available" for local builds whose version
is a git-describe string like v0.79.0-60-g8fae44fb1, even though such a build
is 60 commits AHEAD of the v0.79.0 release. parseSemver treated the
"-60-g8fae44fb1" suffix as a pre-release of 0.79.0, which sorts below the
plain tag, so the comparator reported the build as older than latest.

Recognize the git-describe "-<N>-g<hash>[-dirty]" suffix as a commits-ahead
marker: such a build now sorts at or above its base tag and above any real
pre-release, so an ahead-of-latest build is no longer flagged as
update-available. Genuine pre-releases (rc.1, alpha.2) are unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: require default-length hash for git-describe detection

Tighten the git-describe suffix detector to require the abbreviated commit
hash to be at least 7 hex digits (git's default core.abbrev). This prevents a
contrived pre-release such as v1.0.0-3-ga from being misclassified as a
git-describe "ahead" build. kandev's Makefile runs `git describe` with the
default abbreviation, so real builds always meet the floor.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-19 00:54:04 +01:00

76 lines
2.6 KiB
Go

package updates
import "testing"
func TestCompareSemver(t *testing.T) {
cases := []struct {
name string
a string
b string
want int
}{
{"equal", "v1.0.0", "v1.0.0", 0},
{"equal_no_prefix", "1.2.3", "v1.2.3", 0},
{"patch_less", "v1.0.0", "v1.0.1", -1},
{"minor_greater", "v1.2.0", "v1.1.9", 1},
{"major_greater", "v2.0.0", "v1.9.9", 1},
{"prerelease_below_release", "v1.0.0-rc.1", "v1.0.0", -1},
{"release_above_prerelease", "v1.0.0", "v1.0.0-rc.1", 1},
// git-describe "ahead" builds must sort ABOVE their base tag.
{"ahead_above_tag", "v0.79.0-60-g8fae44fb1", "v0.79.0", 1},
{"tag_below_ahead", "v0.79.0", "v0.79.0-60-g8fae44fb1", -1},
{"ahead_dirty_above_tag", "v0.79.0-60-g8fae44fb1-dirty", "v0.79.0", 1},
// An ahead build outranks a real pre-release of the same tag.
{"ahead_above_prerelease", "v1.0.0-3-gdeadbee", "v1.0.0-rc.1", 1},
// More commits ahead wins.
{"more_commits_ahead", "v1.0.0-10-gabc1234", "v1.0.0-2-gdef5678", 1},
{"fewer_commits_ahead", "v1.0.0-2-gdef5678", "v1.0.0-10-gabc1234", -1},
// A newer minor release still beats an ahead build of an older tag.
{"newer_minor_beats_ahead", "v0.80.0", "v0.79.0-60-g8fae44fb1", 1},
{"ahead_below_newer_minor", "v0.79.0-60-g8fae44fb1", "v0.80.0", -1},
// Invalid inputs sort below valid ones.
{"invalid_below_valid", "not-a-version", "v1.0.0", -1},
{"valid_above_invalid", "v1.0.0", "not-a-version", 1},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := compareSemver(tc.a, tc.b); got != tc.want {
t.Errorf("compareSemver(%q,%q)=%d want %d", tc.a, tc.b, got, tc.want)
}
})
}
}
func TestGitDescribeCommits(t *testing.T) {
cases := []struct {
name string
pre string
wantN int
wantOK bool
}{
{"standard", "60-g8fae44fb1", 60, true},
{"dirty", "60-g8fae44fb1-dirty", 60, true},
{"min_length_hash", "1-gabc1234", 1, true},
{"empty", "", 0, false},
{"real_prerelease", "rc.1", 0, false},
{"prerelease_with_dot", "alpha.2", 0, false},
{"missing_g_prefix", "60-8fae44fb1", 0, false},
{"non_hex_hash", "60-gzzzzzzz", 0, false},
{"non_numeric_count", "x-gabc1234", 0, false},
// Short hashes below git's default abbreviation are treated as ordinary
// pre-releases, not git-describe builds.
{"single_char_hash", "3-ga", 0, false},
{"below_min_hash", "3-gabc12", 0, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
gotN, gotOK := gitDescribeCommits(tc.pre)
if gotN != tc.wantN || gotOK != tc.wantOK {
t.Errorf("gitDescribeCommits(%q)=(%d,%v) want (%d,%v)", tc.pre, gotN, gotOK, tc.wantN, tc.wantOK)
}
})
}
}