github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/cmd/flags/flags_test.go (about) 1 /* 2 * Copyright 2022 The Gremlins Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package flags 18 19 import ( 20 "testing" 21 22 "github.com/spf13/cobra" 23 "github.com/spf13/viper" 24 ) 25 26 type unsupportedType int 27 28 func TestSet(t *testing.T) { 29 testCases := []struct { 30 flag Flag 31 expectError bool 32 }{ 33 { 34 flag: Flag{ 35 Name: "bool-flag-no-sh", 36 CfgKey: "test.cfg", 37 Shorthand: "", 38 DefaultV: true, 39 Usage: "test usage", 40 }, 41 }, 42 { 43 flag: Flag{ 44 Name: "bool-flag-sh", 45 CfgKey: "test.cfg", 46 Shorthand: "t", 47 DefaultV: true, 48 Usage: "test usage", 49 }, 50 }, 51 52 { 53 flag: Flag{ 54 Name: "string-flag-no-sh", 55 CfgKey: "test.cfg", 56 Shorthand: "", 57 DefaultV: "test", 58 Usage: "test usage", 59 }, 60 }, 61 { 62 flag: Flag{ 63 Name: "string-flag-sh", 64 CfgKey: "test.cfg", 65 Shorthand: "t", 66 DefaultV: "test", 67 Usage: "test usage", 68 }, 69 }, 70 { 71 flag: Flag{ 72 Name: "int-flag-no-sh", 73 CfgKey: "test.cfg", 74 Shorthand: "", 75 DefaultV: 0, 76 Usage: "test usage", 77 }, 78 }, 79 { 80 flag: Flag{ 81 Name: "int-flag-sh", 82 CfgKey: "test.cfg", 83 Shorthand: "t", 84 DefaultV: 0, 85 Usage: "test usage", 86 }, 87 }, 88 { 89 flag: Flag{ 90 Name: "float64-flag-no-sh", 91 CfgKey: "test.cfg", 92 Shorthand: "", 93 DefaultV: float64(0), 94 Usage: "test usage", 95 }, 96 }, 97 { 98 flag: Flag{ 99 Name: "float64-flag-sh", 100 CfgKey: "test.cfg", 101 Shorthand: "t", 102 DefaultV: float64(0), 103 Usage: "test usage", 104 }, 105 }, 106 { 107 flag: Flag{ 108 Name: "not-supported-type", 109 CfgKey: "test.cfg", 110 Shorthand: "t", 111 DefaultV: unsupportedType(0), 112 Usage: "test usage", 113 }, 114 expectError: true, 115 }, 116 } 117 118 for _, tc := range testCases { 119 t.Run(tc.flag.Name, func(t *testing.T) { 120 defer viper.Reset() 121 122 cmd := &cobra.Command{} 123 124 // #nosec G601 - We are in tests, we don't care 125 err := Set(cmd, &tc.flag) 126 if (tc.expectError && err == nil) || (!tc.expectError && err != nil) { 127 t.Fatal("error not expected") 128 } 129 if !tc.expectError { 130 if cmd.Flags().Lookup(tc.flag.Name) == nil { 131 t.Errorf("expected flag to be present") 132 } 133 } 134 135 tc.flag.Name += "_persistent" 136 // #nosec G601 - We are in tests, we don't care 137 err = SetPersistent(cmd, &tc.flag) 138 if (tc.expectError && err == nil) || (!tc.expectError && err != nil) { 139 t.Fatal("error not expected") 140 } 141 if !tc.expectError { 142 if cmd.Flag(tc.flag.Name) == nil { 143 t.Errorf("expected flag to be present") 144 } 145 } 146 147 }) 148 } 149 }