Skip to content

Commit

Permalink
refactor build info collection to unit test, add unit tests for using…
Browse files Browse the repository at this point in the history
… debug.ReadBuildInfo for release
  • Loading branch information
DAcodedBEAT committed Aug 23, 2023
1 parent 968d9ca commit 9a81cfb
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
23 changes: 23 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@ package sentry

import (
"github.com/getsentry/sentry-go/internal/testutils"
"os"
)

var assertEqual = testutils.AssertEqual
var assertNotEqual = testutils.AssertNotEqual
var assertBaggageStringsEqual = testutils.AssertBaggageStringsEqual

func envSetter(envs map[string]string) (closer func()) {
originalEnvs := map[string]string{}

for name, value := range envs {
if originalValue, ok := os.LookupEnv(name); ok {
originalEnvs[name] = originalValue
}
_ = os.Setenv(name, value)
}

return func() {
for name := range envs {
origValue, has := originalEnvs[name]
if has {
_ = os.Setenv(name, origValue)
} else {
_ = os.Unsetenv(name)
}
}
}
}
19 changes: 14 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ func defaultRelease() (release string) {
}

if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" && setting.Value != "" {
Logger.Printf("Using release from debug info: %s", setting.Value)
return setting.Value
}
buildInfoVcsRevision := revisionFromBuildInfo(info)
if len(buildInfoVcsRevision) > 0 {
return buildInfoVcsRevision
}
}

Expand All @@ -99,3 +97,14 @@ func defaultRelease() (release string) {
Logger.Printf("Using release from Git: %s", release)
return release
}

func revisionFromBuildInfo(info *debug.BuildInfo) string {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" && setting.Value != "" {
Logger.Printf("Using release from debug info: %s", setting.Value)
return setting.Value
}
}

return ""
}
78 changes: 78 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sentry

import (
"runtime/debug"
"testing"
)

Expand All @@ -27,3 +28,80 @@ func TestFileExistsReturnsFalseForNonExistingFiles(t *testing.T) {
assertEqual(t, fileExists(("util_nope.go")), false)
assertEqual(t, fileExists(("util_nope_test.go")), false)
}

func TestDefaultReleaseSentryReleaseEnvvar(t *testing.T) {
releaseVersion := "1.2.3"
cleanup := envSetter(map[string]string{
"SENTRY_RELEASE": releaseVersion,
})

assertEqual(t, defaultRelease(), releaseVersion)

t.Cleanup(cleanup)
}

func TestDefaultReleaseSentryReleaseEnvvarPrecedence(t *testing.T) {
releaseVersion := "1.2.3"
cleanup := envSetter(map[string]string{
"SOURCE_VERSION": "3.2.1",
"SENTRY_RELEASE": releaseVersion,
})

assertEqual(t, defaultRelease(), releaseVersion)

t.Cleanup(cleanup)
}

func TestRevisionFromBuildInfo(t *testing.T) {
releaseVersion := "deadbeef"

info := &debug.BuildInfo{
Main: debug.Module{
Path: "my/module",
Version: "(devel)",
},
Deps: []*debug.Module{
{
Path: "github.com/getsentry/sentry-go",
Version: "v0.23.1",
Replace: &debug.Module{
Path: "pkg/sentry",
},
},
},
Settings: []debug.BuildSetting{
{
Key: "vcs.revision",
Value: releaseVersion,
},
},
}
cleanup := envSetter(map[string]string{})

assertEqual(t, revisionFromBuildInfo(info), releaseVersion)

t.Cleanup(cleanup)
}

func TestRevisionFromBuildInfoNoVcsInformation(t *testing.T) {
info := &debug.BuildInfo{
Main: debug.Module{
Path: "my/module",
Version: "(devel)",
},
Deps: []*debug.Module{
{
Path: "github.com/getsentry/sentry-go",
Version: "v0.23.1",
Replace: &debug.Module{
Path: "pkg/sentry",
},
},
},
}
cleanup := envSetter(map[string]string{})

assertEqual(t, revisionFromBuildInfo(info), "")

t.Cleanup(cleanup)
}

0 comments on commit 9a81cfb

Please sign in to comment.