File: env_parser_test.go

package info (click to toggle)
golang-github-peterbourgon-ff 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 408 kB
  • sloc: sh: 9; makefile: 4
file content (66 lines) | stat: -rw-r--r-- 1,630 bytes parent folder | download
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
package ff_test

import (
	"path/filepath"
	"testing"
	"time"

	"github.com/peterbourgon/ff/v3"
	"github.com/peterbourgon/ff/v3/fftest"
)

func TestEnvFileParser(t *testing.T) {
	t.Parallel()

	for _, testcase := range []struct {
		file string
		opts []ff.Option
		want fftest.Vars
	}{
		{
			file: "testdata/empty.env",
			want: fftest.Vars{},
		},
		{
			file: "testdata/basic.env",
			want: fftest.Vars{S: "bar", I: 99, B: true, D: time.Hour},
		},
		{
			file: "testdata/prefix.env",
			opts: []ff.Option{ff.WithEnvVarPrefix("MYPROG")},
			want: fftest.Vars{S: "bingo", I: 123},
		},
		{
			file: "testdata/prefix-undef.env",
			opts: []ff.Option{ff.WithEnvVarPrefix("MYPROG"), ff.WithIgnoreUndefined(true)},
			want: fftest.Vars{S: "bango", I: 9},
		},
		{
			file: "testdata/quotes.env",
			want: fftest.Vars{S: "", I: 32, X: []string{"1", "2 2", "3 3 3"}},
		},
		{
			file: "testdata/no-value.env",
			want: fftest.Vars{WantParseErrorString: "invalid line: D="},
		},
		{
			file: "testdata/spaces.env",
			want: fftest.Vars{X: []string{"1", "2", "3", "4", "5", " 6", " 7 ", " 8 "}},
		},
		{
			file: "testdata/newlines.env",
			want: fftest.Vars{S: "one\ntwo\nthree\n\n", X: []string{`A\nB\n\n`}},
		},
		{
			file: "testdata/capitalization.env",
			want: fftest.Vars{S: "hello", I: 12345},
		},
	} {
		t.Run(filepath.Base(testcase.file), func(t *testing.T) {
			testcase.opts = append(testcase.opts, ff.WithConfigFile(testcase.file), ff.WithConfigFileParser(ff.EnvParser))
			fs, vars := fftest.Pair()
			vars.ParseError = ff.Parse(fs, []string{}, testcase.opts...)
			fftest.Compare(t, &testcase.want, vars)
		})
	}
}