-
Notifications
You must be signed in to change notification settings - Fork 1
/
wonsz_test.go
82 lines (66 loc) · 1.59 KB
/
wonsz_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
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
package wonsz
import (
"fmt"
"github.com/spf13/cobra"
"os"
"testing"
)
func TestInitializeConfig(t *testing.T) {
var testConfig struct {
AlwaysInConfig string
TestField string
}
err := BindConfig(&testConfig, &cobra.Command{}, ConfigOpts{})
if err != nil {
t.Fatal(err)
}
}
func ExampleBindConfig() {
os.Setenv("EXAMPLE_FIELD", "this is my example config field")
var myConfig struct {
ExampleField string
}
err := BindConfig(&myConfig, nil, ConfigOpts{})
if err != nil {
panic(err)
}
fmt.Println(myConfig.ExampleField)
// Output: this is my example config field
}
func Test_BindConfig_withEnv(t *testing.T) {
os.Setenv("SLICE_FIELD", "some,text,here")
var testConfig struct {
SliceField []string
}
err := BindConfig(&testConfig, nil, ConfigOpts{})
if err != nil {
t.Fatal(err)
}
if testConfig.SliceField[0] != "some" ||
testConfig.SliceField[1] != "text" ||
testConfig.SliceField[2] != "here" {
t.Errorf("Expected %s, got %s", "some", testConfig.SliceField[0])
}
}
func Test_BindConfig_withFlag(t *testing.T) {
var testConfig struct {
SliceField []string
}
rootCmd := &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
if testConfig.SliceField[0] != "some" ||
testConfig.SliceField[1] != "text" ||
testConfig.SliceField[2] != "here" {
t.Errorf("Expected %s, got %s", "some", testConfig.SliceField[0])
}
},
}
os.Args = []string{"cmd", "--slice-field=some,text,here"}
err := BindConfig(&testConfig, rootCmd, ConfigOpts{})
if err != nil {
t.Fatal(err)
}
if err := rootCmd.Execute(); err != nil {
t.Fatal(err)
}
}