Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove var buildInfo #2358

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ lint: check-golangci-lint ## Run golangci-lint against code
.PHONY: unit-test
unit-test: ## Run unit tests for the go code
# We have to run the tests in the cmd package using `go test` because of a bug with the CLI library cobra. See https://github.com/spf13/cobra/issues/2104.
go test ./cmd/... -race -shuffle=on -coverprofile=cmd-coverage.out -covermode=atomic
go test -buildvcs ./cmd/... -race -shuffle=on -coverprofile=cmd-coverage.out -covermode=atomic
go run github.com/onsi/ginkgo/v2/ginkgo --randomize-all --randomize-suites --race --keep-going --fail-on-pending --trace --covermode=atomic --coverprofile=coverage.out -r internal
go tool cover -html=coverage.out -o cover.html
go tool cover -html=cmd-coverage.out -o cmd-cover.html
Expand Down
6 changes: 1 addition & 5 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,12 @@ func parseFlags(flags *pflag.FlagSet) ([]string, []string) {
return flagKeys, flagValues
}

type buildInfoFunc func() (info *debug.BuildInfo, ok bool)

var buildInfo buildInfoFunc = debug.ReadBuildInfo

func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
commitHash = "unknown"
commitTime = "unknown"
dirtyBuild = "unknown"

info, ok := buildInfo()
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
Expand Down
56 changes: 7 additions & 49 deletions cmd/gateway/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"runtime/debug"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -585,57 +584,16 @@ func TestParseFlags(t *testing.T) {
}

func TestGetBuildInfo(t *testing.T) {
t.Parallel()
g := NewWithT(t)
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.revision", Value: "abc123"},
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
{Key: "vcs.modified", Value: "true"},
},
}, true
}

buildInfo = stubBuildInfo

commitHash, commitTime, dirtyBuild := getBuildInfo()

g.Expect(commitHash).To(Equal("abc123"))
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
g.Expect(dirtyBuild).To(Equal("true"))
}

func TestGetBuildInfoNoBuildInfo(t *testing.T) {
g := NewWithT(t)
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
return nil, false
}

buildInfo = stubBuildInfo

commitHash, commitTime, dirtyBuild := getBuildInfo()

g.Expect(commitHash).To(Equal("unknown"))
g.Expect(commitTime).To(Equal("unknown"))
g.Expect(dirtyBuild).To(Equal("unknown"))
}

func TestGetBuildInfoMissingValue(t *testing.T) {
g := NewWithT(t)
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
{Key: "vcs.modified", Value: "true"},
},
}, true
}

buildInfo = stubBuildInfo

commitHash, commitTime, dirtyBuild := getBuildInfo()
g.Expect(commitHash).To(Not(BeEmpty()))
g.Expect(commitTime).To(Not(BeEmpty()))
g.Expect(dirtyBuild).To(Not(BeEmpty()))

g.Expect(commitHash).To(Equal("unknown"))
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
g.Expect(dirtyBuild).To(Equal("true"))
g.Expect(commitHash).To(Not(Equal("unknown")))
g.Expect(commitTime).To(Not(Equal("unknown")))
g.Expect(dirtyBuild).To(Not(Equal("unknown")))
lucacome marked this conversation as resolved.
Show resolved Hide resolved
}
Loading