Skip to content

Commit

Permalink
Fix lib version detection when the major ver is greater than 1
Browse files Browse the repository at this point in the history
  • Loading branch information
vasayxtx committed Nov 28, 2024
1 parent 9b4320a commit fd174a1
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 15 deletions.
4 changes: 2 additions & 2 deletions internal/libinfo/lib_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Released under MIT license.
package libinfo

func UserAgent() string {
return LibName + "/" + GetLibVersion()
return libShortName + "/" + GetLibVersion()
}

func LogPrefix() string {
return "[" + LibName + "/" + GetLibVersion() + "] "
return "[" + libShortName + "/" + GetLibVersion() + "] "
}
41 changes: 28 additions & 13 deletions internal/libinfo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,46 @@ Released under MIT license.
package libinfo

import (
"debug/buildinfo"
"regexp"
"sync"

"runtime/debug"
)

const LibName = "go-authkit"
const libShortName = "go-authkit"

const libPath = "github.com/acronis/" + LibName
const moduleName = "github.com/acronis/" + libShortName

var libVersion string
var libVersionOnce sync.Once

func GetLibVersion() string {
libVersionOnce.Do(initLibVersion)
return libVersion
}

func initLibVersion() {
if buildInfo, ok := debug.ReadBuildInfo(); ok && buildInfo != nil {
for _, dep := range buildInfo.Deps {
if dep.Path == libPath {
libVersion = dep.Version
return
}
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
libVersion = extractLibVersion(buildInfo, moduleName)
}
if libVersion == "" {
libVersion = "v0.0.0"
}
libVersion = "v0.0.0"
}

func GetLibVersion() string {
libVersionOnce.Do(initLibVersion)
return libVersion
func extractLibVersion(buildInfo *buildinfo.BuildInfo, modName string) string {
if buildInfo == nil {
return ""
}
re, err := regexp.Compile(`^` + regexp.QuoteMeta(modName) + `(/v[0-9]+)?$`)
if err != nil {
return "" // should never happen
}
for _, dep := range buildInfo.Deps {
if re.MatchString(dep.Path) {
return dep.Version
}
}
return ""
}
75 changes: 75 additions & 0 deletions internal/libinfo/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/

package libinfo

import (
"debug/buildinfo"
"runtime/debug"
"testing"

"github.com/stretchr/testify/require"
)

func TestExtractLibVersion(t *testing.T) {
tests := []struct {
name string
buildInfo *buildinfo.BuildInfo
moduleName string
expectedVer string
}{
{
name: "module found",
buildInfo: &buildinfo.BuildInfo{
Deps: []*debug.Module{
{Path: "github.com/acronis/go-authkit", Version: "v1.2.3"},
},
},
moduleName: "github.com/acronis/go-authkit",
expectedVer: "v1.2.3",
},
{
name: "module found, v2",
buildInfo: &buildinfo.BuildInfo{
Deps: []*debug.Module{
{Path: "github.com/acronis/go-authkit/v2", Version: "v2.0.0"},
},
},
moduleName: "github.com/acronis/go-authkit",
expectedVer: "v2.0.0",
},
{
name: "module not found",
buildInfo: &buildinfo.BuildInfo{
Deps: []*debug.Module{
{Path: "github.com/other/module", Version: "v1.0.0"},
},
},
moduleName: "github.com/acronis/go-authkit",
expectedVer: "",
},
{
name: "empty deps",
buildInfo: &buildinfo.BuildInfo{
Deps: []*debug.Module{},
},
moduleName: "github.com/acronis/go-authkit",
expectedVer: "",
},
{
name: "nil build info",
buildInfo: nil,
moduleName: "github.com/acronis/go-authkit",
expectedVer: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractLibVersion(tt.buildInfo, tt.moduleName)
require.Equal(t, tt.expectedVer, got)
})
}
}

0 comments on commit fd174a1

Please sign in to comment.