2024-01-28 15:15:32 +00:00
|
|
|
package feature
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestSetFlag temporarily sets a feature flag to the given value until the
|
|
|
|
// returned function is called.
|
|
|
|
//
|
|
|
|
// Usage
|
|
|
|
// ```
|
|
|
|
// defer TestSetFlag(t, features.Flags, features.ExampleFlag, true)()
|
|
|
|
// ```
|
|
|
|
func TestSetFlag(t *testing.T, f *FlagSet, flag FlagName, value bool) func() {
|
|
|
|
current := f.Enabled(flag)
|
|
|
|
|
2024-02-17 20:50:25 +00:00
|
|
|
panicIfCalled := func(msg string) {
|
|
|
|
panic(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Apply(fmt.Sprintf("%s=%v", flag, value), panicIfCalled); err != nil {
|
2024-01-28 15:15:32 +00:00
|
|
|
// not reachable
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
2024-02-17 20:50:25 +00:00
|
|
|
if err := f.Apply(fmt.Sprintf("%s=%v", flag, current), panicIfCalled); err != nil {
|
2024-01-28 15:15:32 +00:00
|
|
|
// not reachable
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|