-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set consistently and eventually defaults on init (#443)
Using environment variables Closes #434 Signed-off-by: toby lorne <toby@toby.codes>
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gomega | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/onsi/gomega/internal/defaults" | ||
) | ||
|
||
const ( | ||
ConsistentlyDurationEnvVarName = "GOMEGA_DEFAULT_CONSISTENTLY_DURATION" | ||
ConsistentlyPollingIntervalEnvVarName = "GOMEGA_DEFAULT_CONSISTENTLY_POLLING_INTERVAL" | ||
EventuallyTimeoutEnvVarName = "GOMEGA_DEFAULT_EVENTUALLY_TIMEOUT" | ||
EventuallyPollingIntervalEnvVarName = "GOMEGA_DEFAULT_EVENTUALLY_POLLING_INTERVAL" | ||
) | ||
|
||
func init() { | ||
defaults.SetDurationFromEnv( | ||
os.Getenv, | ||
SetDefaultConsistentlyDuration, | ||
ConsistentlyDurationEnvVarName, | ||
) | ||
|
||
defaults.SetDurationFromEnv( | ||
os.Getenv, | ||
SetDefaultConsistentlyPollingInterval, | ||
ConsistentlyPollingIntervalEnvVarName, | ||
) | ||
|
||
defaults.SetDurationFromEnv( | ||
os.Getenv, | ||
SetDefaultEventuallyTimeout, | ||
EventuallyTimeoutEnvVarName, | ||
) | ||
|
||
defaults.SetDurationFromEnv( | ||
os.Getenv, | ||
SetDefaultEventuallyPollingInterval, | ||
EventuallyPollingIntervalEnvVarName, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package defaults_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestDefaults(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Gomega Defaults Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package defaults | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func SetDurationFromEnv(getDurationFromEnv func(string) string, varSetter func(time.Duration), name string) { | ||
durationFromEnv := getDurationFromEnv(name) | ||
|
||
if len(durationFromEnv) == 0 { | ||
return | ||
} | ||
|
||
duration, err := time.ParseDuration(durationFromEnv) | ||
|
||
if err != nil { | ||
panic(fmt.Sprintf("Expected a duration when using %s! Parse error %v", name, err)) | ||
} | ||
|
||
varSetter(duration) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package defaults_test | ||
|
||
import ( | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
d "github.com/onsi/gomega/internal/defaults" | ||
) | ||
|
||
var _ = Describe("Durations", func() { | ||
var ( | ||
duration *time.Duration | ||
envVarGot string | ||
envVarToReturn string | ||
|
||
getDurationFromEnv = func(name string) string { | ||
envVarGot = name | ||
return envVarToReturn | ||
} | ||
|
||
setDuration = func(t time.Duration) { | ||
duration = &t | ||
} | ||
|
||
setDurationCalled = func() bool { | ||
return duration != nil | ||
} | ||
|
||
resetDuration = func() { | ||
duration = nil | ||
} | ||
) | ||
|
||
BeforeEach(func() { | ||
resetDuration() | ||
}) | ||
|
||
Context("When the environment has a duration", func() { | ||
Context("When the duration is valid", func() { | ||
BeforeEach(func() { | ||
envVarToReturn = "10m" | ||
|
||
d.SetDurationFromEnv(getDurationFromEnv, setDuration, "MY_ENV_VAR") | ||
}) | ||
|
||
It("sets the duration", func() { | ||
Expect(envVarGot).To(Equal("MY_ENV_VAR")) | ||
Expect(setDurationCalled()).To(Equal(true)) | ||
Expect(*duration).To(Equal(10 * time.Minute)) | ||
}) | ||
}) | ||
|
||
Context("When the duration is not valid", func() { | ||
BeforeEach(func() { | ||
envVarToReturn = "10" | ||
}) | ||
|
||
It("panics with a helpful error message", func() { | ||
Expect(func() { | ||
d.SetDurationFromEnv(getDurationFromEnv, setDuration, "MY_ENV_VAR") | ||
}).To(PanicWith(MatchRegexp("Expected a duration when using MY_ENV_VAR"))) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("When the environment does not have a duration", func() { | ||
BeforeEach(func() { | ||
envVarToReturn = "" | ||
|
||
d.SetDurationFromEnv(getDurationFromEnv, setDuration, "MY_ENV_VAR") | ||
}) | ||
|
||
It("does not set the duration", func() { | ||
Expect(envVarGot).To(Equal("MY_ENV_VAR")) | ||
Expect(setDurationCalled()).To(Equal(false)) | ||
Expect(duration).To(BeNil()) | ||
}) | ||
}) | ||
}) |