Don't panic if the feature-gate flag was not used

pull/6/head
Tim Hockin 2016-08-22 17:37:07 -07:00
parent f77576bc2b
commit 34bd1e391e
2 changed files with 22 additions and 5 deletions

View File

@ -156,11 +156,10 @@ func (f *featureGate) Type() string {
func (f *featureGate) lookup(key string) bool {
defaultValue := f.known[key].enabled
if f.enabled == nil {
panic(fmt.Sprintf("--%s has not been parsed", flagName))
}
if v, ok := f.enabled[key]; ok {
return v
if f.enabled != nil {
if v, ok := f.enabled[key]; ok {
return v
}
}
return defaultValue

View File

@ -139,3 +139,21 @@ func TestFeatureGateFlag(t *testing.T) {
}
}
}
func TestFeatureGateFlagDefaults(t *testing.T) {
// gates for testing
const testAlphaGate = "TestAlpha"
const testBetaGate = "TestBeta"
// Don't parse the flag, assert defaults are used.
f := DefaultFeatureGate
f.known[testAlphaGate] = featureSpec{false, alpha}
f.known[testBetaGate] = featureSpec{true, beta}
if f.lookup(testAlphaGate) != false {
t.Errorf("Expected false")
}
if f.lookup(testBetaGate) != true {
t.Errorf("Expected true")
}
}