cuelang.org/go@v0.13.0/internal/envflag/flag_test.go (about) 1 package envflag 2 3 import ( 4 "testing" 5 6 "github.com/go-quicktest/qt" 7 ) 8 9 type testFlags struct { 10 Foo bool 11 BarBaz bool 12 13 DefaultFalse bool `envflag:"default:false"` 14 DefaultTrue bool `envflag:"default:true"` 15 } 16 17 type testTypes struct { 18 StringDefaultFoo string `envflag:"default:foo"` 19 IntDefault5 int `envflag:"default:5"` 20 } 21 22 type deprecatedFlags struct { 23 Foo bool `envflag:"deprecated"` 24 Bar bool `envflag:"deprecated,default:true"` 25 } 26 27 func success[T comparable](want T) func(t *testing.T) { 28 return func(t *testing.T) { 29 var x T 30 err := Init(&x, "TEST_VAR") 31 qt.Assert(t, qt.IsNil(err)) 32 qt.Assert(t, qt.Equals(x, want)) 33 } 34 } 35 36 func failure[T comparable](want T, wantError string) func(t *testing.T) { 37 return func(t *testing.T) { 38 var x T 39 err := Init(&x, "TEST_VAR") 40 qt.Assert(t, qt.ErrorMatches(err, wantError)) 41 qt.Assert(t, qt.Equals(x, want)) 42 } 43 } 44 45 func invalid[T comparable](want T) func(t *testing.T) { 46 return func(t *testing.T) { 47 var x T 48 err := Init(&x, "TEST_VAR") 49 qt.Assert(t, qt.ErrorIs(err, ErrInvalid)) 50 qt.Assert(t, qt.Equals(x, want)) 51 } 52 } 53 54 var tests = []struct { 55 testName string 56 envVal string 57 test func(t *testing.T) 58 }{{ 59 testName: "Empty", 60 envVal: "", 61 test: success(testFlags{ 62 DefaultTrue: true, 63 }), 64 }, { 65 testName: "JustCommas", 66 envVal: ",,", 67 test: success(testFlags{ 68 DefaultTrue: true, 69 }), 70 }, { 71 testName: "Unknown", 72 envVal: "ratchet", 73 test: failure(testFlags{DefaultTrue: true}, 74 "cannot parse TEST_VAR: unknown flag \"ratchet\""), 75 }, { 76 testName: "Set", 77 envVal: "foo", 78 test: success(testFlags{ 79 Foo: true, 80 DefaultTrue: true, 81 }), 82 }, { 83 testName: "SetExtraCommas", 84 envVal: ",foo,", 85 test: success(testFlags{ 86 Foo: true, 87 DefaultTrue: true, 88 }), 89 }, { 90 testName: "SetTwice", 91 envVal: "foo,foo", 92 test: success(testFlags{ 93 Foo: true, 94 DefaultTrue: true, 95 }), 96 }, { 97 testName: "SetWithUnknown", 98 envVal: "foo,other", 99 test: failure(testFlags{ 100 Foo: true, 101 DefaultTrue: true, 102 }, "cannot parse TEST_VAR: unknown flag \"other\""), 103 }, { 104 testName: "TwoFlags", 105 envVal: "barbaz,foo", 106 test: success(testFlags{ 107 Foo: true, 108 BarBaz: true, 109 DefaultTrue: true, 110 }), 111 }, { 112 testName: "ToggleDefaultFieldsNumeric", 113 envVal: "defaulttrue=0,defaultfalse=1", 114 test: success(testFlags{ 115 DefaultFalse: true, 116 }), 117 }, { 118 testName: "ToggleDefaultFieldsWords", 119 envVal: "defaulttrue=false,defaultfalse=true", 120 test: success(testFlags{ 121 DefaultFalse: true, 122 }), 123 }, { 124 testName: "MultipleUnknown", 125 envVal: "other1,other2,foo", 126 test: failure(testFlags{ 127 Foo: true, 128 DefaultTrue: true, 129 }, "cannot parse TEST_VAR: unknown flag \"other1\"\nunknown flag \"other2\""), 130 }, { 131 testName: "InvalidIntForBool", 132 envVal: "foo=2", 133 test: invalid(testFlags{DefaultTrue: true}), 134 }, { 135 testName: "StringValue", 136 envVal: "stringdefaultfoo=bar", 137 test: success(testTypes{ 138 StringDefaultFoo: "bar", 139 IntDefault5: 5, 140 }), 141 }, { 142 testName: "StringEmpty", 143 envVal: "stringdefaultfoo=", 144 test: success(testTypes{ 145 StringDefaultFoo: "", 146 IntDefault5: 5, 147 }), 148 }, { 149 testName: "FailureStringAlone", 150 envVal: "stringdefaultfoo", 151 test: failure(testTypes{ 152 StringDefaultFoo: "foo", 153 IntDefault5: 5, 154 }, "cannot parse TEST_VAR: value needed for string flag \"stringdefaultfoo\""), 155 }, { 156 testName: "IntValue", 157 envVal: "intdefault5=123", 158 test: success(testTypes{ 159 StringDefaultFoo: "foo", 160 IntDefault5: 123, 161 }), 162 }, { 163 testName: "IntEmpty", 164 envVal: "intdefault5=", 165 test: invalid(testTypes{ 166 StringDefaultFoo: "foo", 167 IntDefault5: 5, 168 }), 169 }, { 170 testName: "DeprecatedWithFalseDefault", 171 envVal: "foo=1", 172 test: failure(deprecatedFlags{ 173 Bar: true, 174 }, `cannot parse TEST_VAR: cannot change default value of deprecated flag "foo"`), 175 }, { 176 testName: "DeprecatedNoopWhenSameAndFalseDefault", 177 envVal: "foo=false", 178 test: success(deprecatedFlags{ 179 Bar: true, 180 }), 181 }, { 182 testName: "DeprecatedWithTrueDefault", 183 envVal: "bar=0", 184 test: failure(deprecatedFlags{ 185 Bar: true, 186 }, `cannot parse TEST_VAR: cannot change default value of deprecated flag "bar"`), 187 }, { 188 testName: "DeprecatedNoopWhenSameAndTrueDefault", 189 envVal: "bar=1", 190 test: success(deprecatedFlags{ 191 Bar: true, 192 }), 193 }} 194 195 func TestInit(t *testing.T) { 196 for _, test := range tests { 197 t.Run(test.testName, func(t *testing.T) { 198 t.Setenv("TEST_VAR", test.envVal) 199 test.test(t) 200 }) 201 } 202 }