Skip to content

Commit 59750f3

Browse files
author
Russ Egan
committed
Wrap verbose and disabled in setters/getters
Turns out, we shouldn't set these from the environment in an init() function. This gets called before consuming code has a chance to run anything, so other code doesn't have a chance to make changes to the environment before its read. So for example, code using a library to load a .env file can't get that done before this package's init() function would be called. So now we put these variables behind getters which lazily read these settings from the environment.
1 parent f14aa63 commit 59750f3

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

v2/flumetest/flumetest.go

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,53 @@ import (
2626
"github.com/ThalesGroup/flume/v2"
2727
)
2828

29-
var Disable bool
30-
var Verbose bool
31-
32-
//nolint:gochecknoinits
33-
func init() {
34-
if s, ok := os.LookupEnv("FLUMETEST_DISABLE"); ok {
35-
Disable, _ = strconv.ParseBool(s)
36-
} else {
37-
Disable, _ = strconv.ParseBool(os.Getenv("FLUME_TEST_DISABLE"))
38-
}
39-
Verbose, _ = strconv.ParseBool(os.Getenv("FLUMETEST_VERBOSE"))
29+
var (
30+
disabledPtr *bool
31+
verbosePtr *bool
32+
33+
initializeOnce sync.Once
34+
)
35+
36+
func Disabled() bool {
37+
initialize()
38+
return disabledPtr != nil && *disabledPtr
39+
}
40+
41+
func SetDisabled(disabled bool) {
42+
*disabledPtr = disabled
43+
}
44+
45+
func Verbose() bool {
46+
initialize()
47+
return verbosePtr != nil && *verbosePtr
48+
}
49+
50+
func SetVerbose(verbose bool) {
51+
*verbosePtr = verbose
52+
}
53+
54+
// do not read the environment in init(). Using init() to the read the environment
55+
// doesn't give consumers a chance to load .env files first, or otherwise set up
56+
// the environment.
57+
func initialize() {
58+
initializeOnce.Do(func() {
59+
// only read these from the env if they weren't already set by from the command
60+
// line args
61+
if disabledPtr == nil {
62+
var b bool
63+
if s, ok := os.LookupEnv("FLUMETEST_DISABLE"); ok {
64+
b, _ = strconv.ParseBool(s)
65+
} else {
66+
b, _ = strconv.ParseBool(os.Getenv("FLUME_TEST_DISABLE"))
67+
}
68+
disabledPtr = &b
69+
}
70+
if verbosePtr == nil {
71+
var b bool
72+
b, _ = strconv.ParseBool(os.Getenv("FLUMETEST_VERBOSE"))
73+
verbosePtr = &b
74+
}
75+
})
4076
}
4177

4278
// RegisterFlags registers command line flag options related flume:
@@ -48,8 +84,8 @@ func init() {
4884
//
4985
// If you wish to use these flags in your tests, you should call this in TestMain().
5086
func RegisterFlags() {
51-
flag.BoolVar(&Disable, "disable-flumetest", false, "Disables all flumetest features: logging will happen as normal")
52-
flag.BoolVar(&Verbose, "vv", false, "During tests, forwards all logs immediately to t.Log()")
87+
disabledPtr = flag.Bool("disable-flumetest", false, "Disables all flumetest features: logging will happen as normal")
88+
verbosePtr = flag.Bool("vv", false, "During tests, forwards all logs immediately to t.Log()")
5389
}
5490

5591
// Start captures all logs written during the test. If the test succeeds, the
@@ -70,13 +106,13 @@ func RegisterFlags() {
70106
// useful to flush ear logs from setup code, then starting a new buffer for the
71107
// body of the test.
72108
func Start(t testingTB) func() {
73-
if Disable {
109+
if Disabled() {
74110
// no op
75111
return func() {}
76112
}
77113

78114
revertToSnapshot := Snapshot(flume.Default())
79-
if Verbose {
115+
if Verbose() {
80116
t.Cleanup(revertToSnapshot)
81117
flume.Default().SetOut(flume.LogFuncWriter(t.Log, true))
82118
return revertToSnapshot

v2/flumetest/flumetest_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestStart(t *testing.T) {
122122
failTest: false,
123123
expect: "color=red",
124124
testFunc: func(tb testingTB) {
125-
Verbose = true
125+
SetVerbose(true)
126126
Start(tb)
127127

128128
log.Info("Hi", "color", "red")
@@ -133,7 +133,7 @@ func TestStart(t *testing.T) {
133133
failTest: true,
134134
expect: "",
135135
testFunc: func(tb testingTB) {
136-
Disable = true
136+
SetDisabled(true)
137137
Start(tb)
138138

139139
log.Info("Hi", "color", "red")
@@ -167,11 +167,11 @@ func TestStart(t *testing.T) {
167167
}
168168

169169
// restore the original values after the test
170-
oldDisabled := Disable
171-
oldVerbose := Verbose
170+
oldDisabled := Disabled()
171+
oldVerbose := Verbose()
172172
defer func() {
173-
Disable = oldDisabled
174-
Verbose = oldVerbose
173+
SetDisabled(oldDisabled)
174+
SetVerbose(oldVerbose)
175175
}()
176176

177177
m := mockT{

0 commit comments

Comments
 (0)