Skip to content

Commit aedc18d

Browse files
authored
Remove var buildInfo (#2358)
Problem: Running the tests in parallel would cause a data race, because the tests were all trying to write the same variable. Solution: Remove the var and type, that were only used for tests and use the actual build info to test that the function works correctly.
1 parent c732259 commit aedc18d

File tree

3 files changed

+9
-55
lines changed

3 files changed

+9
-55
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ lint: check-golangci-lint ## Run golangci-lint against code
181181
.PHONY: unit-test
182182
unit-test: ## Run unit tests for the go code
183183
# 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.
184-
go test ./cmd/... -race -shuffle=on -coverprofile=cmd-coverage.out -covermode=atomic
184+
go test -buildvcs ./cmd/... -race -shuffle=on -coverprofile=cmd-coverage.out -covermode=atomic
185185
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
186186
go tool cover -html=coverage.out -o cover.html
187187
go tool cover -html=cmd-coverage.out -o cmd-cover.html

cmd/gateway/commands.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,12 @@ func parseFlags(flags *pflag.FlagSet) ([]string, []string) {
501501
return flagKeys, flagValues
502502
}
503503

504-
type buildInfoFunc func() (info *debug.BuildInfo, ok bool)
505-
506-
var buildInfo buildInfoFunc = debug.ReadBuildInfo
507-
508504
func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
509505
commitHash = "unknown"
510506
commitTime = "unknown"
511507
dirtyBuild = "unknown"
512508

513-
info, ok := buildInfo()
509+
info, ok := debug.ReadBuildInfo()
514510
if !ok {
515511
return
516512
}

cmd/gateway/commands_test.go

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"io"
5-
"runtime/debug"
65
"testing"
76

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

587586
func TestGetBuildInfo(t *testing.T) {
587+
t.Parallel()
588588
g := NewWithT(t)
589-
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
590-
return &debug.BuildInfo{
591-
Settings: []debug.BuildSetting{
592-
{Key: "vcs.revision", Value: "abc123"},
593-
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
594-
{Key: "vcs.modified", Value: "true"},
595-
},
596-
}, true
597-
}
598-
599-
buildInfo = stubBuildInfo
600-
601-
commitHash, commitTime, dirtyBuild := getBuildInfo()
602-
603-
g.Expect(commitHash).To(Equal("abc123"))
604-
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
605-
g.Expect(dirtyBuild).To(Equal("true"))
606-
}
607-
608-
func TestGetBuildInfoNoBuildInfo(t *testing.T) {
609-
g := NewWithT(t)
610-
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
611-
return nil, false
612-
}
613-
614-
buildInfo = stubBuildInfo
615589

616590
commitHash, commitTime, dirtyBuild := getBuildInfo()
617591

618-
g.Expect(commitHash).To(Equal("unknown"))
619-
g.Expect(commitTime).To(Equal("unknown"))
620-
g.Expect(dirtyBuild).To(Equal("unknown"))
621-
}
622-
623-
func TestGetBuildInfoMissingValue(t *testing.T) {
624-
g := NewWithT(t)
625-
stubBuildInfo := func() (info *debug.BuildInfo, ok bool) {
626-
return &debug.BuildInfo{
627-
Settings: []debug.BuildSetting{
628-
{Key: "vcs.time", Value: "2024-07-30T12:34:56Z"},
629-
{Key: "vcs.modified", Value: "true"},
630-
},
631-
}, true
632-
}
633-
634-
buildInfo = stubBuildInfo
635-
636-
commitHash, commitTime, dirtyBuild := getBuildInfo()
592+
g.Expect(commitHash).To(Not(BeEmpty()))
593+
g.Expect(commitTime).To(Not(BeEmpty()))
594+
g.Expect(dirtyBuild).To(Not(BeEmpty()))
637595

638-
g.Expect(commitHash).To(Equal("unknown"))
639-
g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z"))
640-
g.Expect(dirtyBuild).To(Equal("true"))
596+
g.Expect(commitHash).To(Not(Equal("unknown")))
597+
g.Expect(commitTime).To(Not(Equal("unknown")))
598+
g.Expect(dirtyBuild).To(Not(Equal("unknown")))
641599
}

0 commit comments

Comments
 (0)