github.com/secure-build/gitlab-runner@v12.5.0+incompatible/helpers/featureflags/flags_test.go (about) 1 package featureflags 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func mockFlags(newFlags ...FeatureFlag) func() { 10 oldFlags := flags 11 flags = newFlags 12 13 return func() { 14 flags = oldFlags 15 } 16 } 17 18 func TestGetAll(t *testing.T) { 19 testFlag := FeatureFlag{Name: "TEST_FLAG", DefaultValue: "value"} 20 21 defer mockFlags(testFlag)() 22 23 f := GetAll() 24 assert.Len(t, f, 1) 25 assert.Contains(t, f, testFlag) 26 } 27 28 func TestIsOn(t *testing.T) { 29 testCases := map[string]struct { 30 testValue string 31 expectedResult bool 32 expectedError string 33 }{ 34 "empty value": { 35 testValue: "", 36 expectedResult: false, 37 }, 38 "non boolean value": { 39 testValue: "a", 40 expectedResult: false, 41 expectedError: `strconv.ParseBool: parsing "a": invalid syntax`, 42 }, 43 "true value": { 44 testValue: "1", 45 expectedResult: true, 46 }, 47 "false value": { 48 testValue: "f", 49 expectedResult: false, 50 }, 51 } 52 53 for testName, testCase := range testCases { 54 t.Run(testName, func(t *testing.T) { 55 result, err := IsOn(testCase.testValue) 56 assert.Equal(t, testCase.expectedResult, result) 57 if testCase.expectedError != "" { 58 assert.EqualError(t, err, testCase.expectedError) 59 } else { 60 assert.NoError(t, err) 61 } 62 }) 63 } 64 }