forked from anchore/fangs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
46 lines (33 loc) · 966 Bytes
/
config_test.go
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
package fangs
import (
"testing"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require"
"github.com/anchore/go-logger/adapter/discard"
)
func Test_BasicConfig(t *testing.T) {
c := NewConfig("appName")
cmd := cobra.Command{}
fs := NewPFlagSet(discard.New(), cmd.Flags())
c.AddFlags(fs)
require.NotNil(t, c.Logger)
require.Equal(t, "appName", c.AppName)
var flags []string
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
flags = append(flags, flag.Name)
})
require.Contains(t, flags, "config")
}
func Test_EnvVarConfig(t *testing.T) {
t.Setenv("APPNAME_CONFIG", "some/config.env")
c := NewConfig("appName").WithConfigEnvVar()
require.Equal(t, c.File, "some/config.env")
cmd := cobra.Command{}
fs := NewPFlagSet(discard.New(), cmd.Flags())
c.AddFlags(fs)
// simulate the flag set
err := cmd.Flags().Set("config", "a/config.flag")
require.NoError(t, err)
require.Equal(t, c.File, "a/config.flag")
}