Suppose I have a package like this:
package my_package
var feature = os.Getenv("FEATURE_NAME")
func whatever(input string) {
if input == feature {
doSomething()
} else {
doSomethingElse()
}
}
Where the feature
variable may be used several more times.
While testing, I’d like to stub the environment:
func TestSomething(t *testing.T) {
t.Setenv("FEATURE_NAME", "a_feature")
// run assertions
}
However, t.Setenv
will not work here, because the feature
assignment will be evaluated before stubbing the environment in the test.
What would be the best solution for this scenario? I can think of:
- creating a function to lazy-load the env variable
- initialize the variable inside the function
Are there better options?
1