1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
package ff_test
import (
"context"
"embed"
"flag"
"os"
"testing"
"time"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/peterbourgon/ff/v3/fftest"
)
//go:embed testdata/*.conf
var testdataConfigFS embed.FS
func TestParseBasics(t *testing.T) {
t.Parallel()
for _, testcase := range []struct {
name string
env map[string]string
file string
args []string
opts []ff.Option
want fftest.Vars
}{
{
name: "empty",
args: []string{},
want: fftest.Vars{},
},
{
name: "args only",
args: []string{"-s", "foo", "-i", "123", "-b", "-d", "24m"},
want: fftest.Vars{S: "foo", I: 123, B: true, D: 24 * time.Minute},
},
{
name: "file only",
file: "testdata/1.conf",
want: fftest.Vars{S: "bar", I: 99, B: true, D: time.Hour},
},
{
name: "env only",
env: map[string]string{"TEST_PARSE_S": "baz", "TEST_PARSE_F": "0.99", "TEST_PARSE_D": "100s"},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE")},
want: fftest.Vars{S: "baz", F: 0.99, D: 100 * time.Second},
},
{
name: "file args",
file: "testdata/2.conf",
args: []string{"-s", "foo", "-i", "1234"},
want: fftest.Vars{S: "foo", I: 1234, D: 3 * time.Second},
},
{
name: "env args",
env: map[string]string{"TEST_PARSE_S": "should be overridden", "TEST_PARSE_B": "true"},
args: []string{"-s", "explicit wins", "-i", "7"},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE")},
want: fftest.Vars{S: "explicit wins", I: 7, B: true},
},
{
name: "file env",
env: map[string]string{"TEST_PARSE_S": "env takes priority", "TEST_PARSE_B": "true"},
file: "testdata/3.conf",
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE")},
want: fftest.Vars{S: "env takes priority", I: 99, B: true, D: 34 * time.Second},
},
{
name: "file env args",
file: "testdata/4.conf",
env: map[string]string{"TEST_PARSE_S": "from env", "TEST_PARSE_I": "300", "TEST_PARSE_F": "0.15", "TEST_PARSE_B": "true"},
args: []string{"-s", "from arg", "-i", "100"},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE")},
want: fftest.Vars{S: "from arg", I: 100, F: 0.15, B: true, D: time.Minute},
},
{
name: "repeated args",
args: []string{"-s", "foo", "-s", "bar", "-d", "1m", "-d", "1h", "-x", "1", "-x", "2", "-x", "3"},
want: fftest.Vars{S: "bar", D: time.Hour, X: []string{"1", "2", "3"}},
},
{
name: "priority repeats",
env: map[string]string{"TEST_PARSE_S": "s.env", "TEST_PARSE_X": "x.env.1"},
file: "testdata/5.conf",
args: []string{"-s", "s.arg.1", "-s", "s.arg.2", "-x", "x.arg.1", "-x", "x.arg.2"},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE")},
want: fftest.Vars{S: "s.arg.2", X: []string{"x.arg.1", "x.arg.2"}}, // highest prio wins and no others are called
},
{
name: "PlainParser solo bool",
file: "testdata/solo_bool.conf",
want: fftest.Vars{S: "x", B: true},
},
{
name: "PlainParser string with spaces",
file: "testdata/spaces.conf",
want: fftest.Vars{S: "i am the very model of a modern major general"},
},
{
name: "default comma behavior",
env: map[string]string{"TEST_PARSE_S": "one,two,three", "TEST_PARSE_X": "one,two,three"},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE")},
want: fftest.Vars{S: "one,two,three", X: []string{"one,two,three"}},
},
{
name: "WithEnvVarSplit",
env: map[string]string{"TEST_PARSE_S": "one,two,three", "TEST_PARSE_X": "one,two,three"},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE"), ff.WithEnvVarSplit(",")},
want: fftest.Vars{S: "three", X: []string{"one", "two", "three"}},
},
{
name: "WithEnvVars",
env: map[string]string{"TEST_PARSE_S": "foo", "S": "bar"},
opts: []ff.Option{ff.WithEnvVars()},
want: fftest.Vars{S: "bar"},
},
{
name: "WithIgnoreUndefined env",
env: map[string]string{"TEST_PARSE_UNDEFINED": "one", "TEST_PARSE_S": "one"},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE"), ff.WithIgnoreUndefined(true)},
want: fftest.Vars{S: "one"},
},
{
name: "WithIgnoreUndefined file true",
file: "testdata/undefined.conf",
opts: []ff.Option{ff.WithIgnoreUndefined(true)},
want: fftest.Vars{S: "one"},
},
{
name: "WithIgnoreUndefined file false",
file: "testdata/undefined.conf",
opts: []ff.Option{ff.WithIgnoreUndefined(false)},
want: fftest.Vars{WantParseErrorString: "config file flag"},
},
{
name: "env var split comma whitespace",
env: map[string]string{"TEST_PARSE_S": "one, two, three ", "TEST_PARSE_X": "one, two, three "},
opts: []ff.Option{ff.WithEnvVarPrefix("TEST_PARSE"), ff.WithEnvVarSplit(",")},
want: fftest.Vars{S: " three ", X: []string{"one", " two", " three "}},
},
{
name: "WithEnvVars",
env: map[string]string{"S": "xxx", "F": "9.87"},
opts: []ff.Option{ff.WithEnvVars()},
want: fftest.Vars{S: "xxx", F: 9.87},
},
{
name: "WithEnvVarNoPrefix", // make sure alias works
env: map[string]string{"S": "xxx", "F": "9.87"},
opts: []ff.Option{ff.WithEnvVarNoPrefix()},
want: fftest.Vars{S: "xxx", F: 9.87},
},
{
name: "WithFilesystem testdata/1.conf",
opts: []ff.Option{ff.WithFilesystem(testdataConfigFS), ff.WithConfigFile("testdata/1.conf"), ff.WithConfigFileParser(ff.PlainParser)},
want: fftest.Vars{S: "bar", I: 99, B: true, D: 1 * time.Hour},
},
} {
t.Run(testcase.name, func(t *testing.T) {
if testcase.file != "" {
testcase.opts = append(testcase.opts, ff.WithConfigFile(testcase.file), ff.WithConfigFileParser(ff.PlainParser))
}
if len(testcase.env) > 0 {
for k, v := range testcase.env {
defer os.Setenv(k, os.Getenv(k))
os.Setenv(k, v)
}
}
fs, vars := fftest.Pair()
vars.ParseError = ff.Parse(fs, testcase.args, testcase.opts...)
fftest.Compare(t, &testcase.want, vars)
})
}
}
func TestParseIssue16(t *testing.T) {
t.Parallel()
for _, testcase := range []struct {
name string
data string
want string
}{
{
name: "hash in value",
data: "s bar#baz",
want: "bar#baz",
},
{
name: "EOL comment with space",
data: "s bar # baz",
want: "bar",
},
{
name: "EOL comment no space",
data: "s bar #baz",
want: "bar",
},
{
name: "only comment with space",
data: "# foo bar\n",
want: "",
},
{
name: "only comment no space",
data: "#foo bar\n",
want: "",
},
} {
t.Run(testcase.name, func(t *testing.T) {
filename := fftest.TempFile(t, testcase.data)
fs, vars := fftest.Pair()
vars.ParseError = ff.Parse(fs, []string{},
ff.WithConfigFile(filename),
ff.WithConfigFileParser(ff.PlainParser),
)
want := fftest.Vars{S: testcase.want}
fftest.Compare(t, &want, vars)
})
}
}
func TestParseConfigFile(t *testing.T) {
t.Parallel()
for _, testcase := range []struct {
name string
missing bool
allowMissing bool
parseError error
}{
{
name: "has config file",
},
{
name: "config file missing",
missing: true,
parseError: os.ErrNotExist,
},
{
name: "config file missing + allow missing",
missing: true,
allowMissing: true,
},
} {
t.Run(testcase.name, func(t *testing.T) {
filename := "dummy"
if !testcase.missing {
filename = fftest.TempFile(t, "")
}
options := []ff.Option{ff.WithConfigFile(filename), ff.WithConfigFileParser(ff.PlainParser)}
if testcase.allowMissing {
options = append(options, ff.WithAllowMissingConfigFile(true))
}
fs, vars := fftest.Pair()
vars.ParseError = ff.Parse(fs, []string{}, options...)
want := fftest.Vars{WantParseErrorIs: testcase.parseError}
fftest.Compare(t, &want, vars)
})
}
}
func TestParseConfigFileVia(t *testing.T) {
t.Parallel()
var (
rootFS = flag.NewFlagSet("root", flag.ContinueOnError)
config = rootFS.String("config-file", "", "")
i = rootFS.Int("i", 0, "")
s = rootFS.String("s", "", "")
subFS = flag.NewFlagSet("subcommand", flag.ContinueOnError)
d = subFS.Duration("d", time.Second, "")
b = subFS.Bool("b", false, "")
)
subCommand := &ffcli.Command{
Name: "subcommand",
FlagSet: subFS,
Options: []ff.Option{
ff.WithConfigFileParser(ff.PlainParser),
ff.WithConfigFileVia(config),
ff.WithIgnoreUndefined(true),
},
Exec: func(ctx context.Context, args []string) error { return nil },
}
root := &ffcli.Command{
Name: "root",
FlagSet: rootFS,
Options: []ff.Option{
ff.WithConfigFileParser(ff.PlainParser),
ff.WithConfigFileFlag("config-file"),
ff.WithIgnoreUndefined(true),
},
Exec: func(ctx context.Context, args []string) error { return nil },
Subcommands: []*ffcli.Command{subCommand},
}
err := root.ParseAndRun(context.Background(), []string{"-config-file", "testdata/1.conf", "subcommand", "-b"})
if err != nil {
t.Fatal(err)
}
if want, have := time.Hour, *d; want != have {
t.Errorf("d: want %v, have %v", want, have)
}
if want, have := true, *b; want != have {
t.Errorf("b: want %v, have %v", want, have)
}
if want, have := "bar", *s; want != have {
t.Errorf("s: want %q, have %q", want, have)
}
if want, have := 99, *i; want != have {
t.Errorf("i: want %d, have %d", want, have)
}
}
|