-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathoptions_test.go
More file actions
81 lines (67 loc) · 1.53 KB
/
options_test.go
File metadata and controls
81 lines (67 loc) · 1.53 KB
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
package goconfig
import (
"os"
"path/filepath"
"testing"
)
func TestLoadWithOptionsPrecedence(t *testing.T) {
dir, err := os.MkdirTemp("", "goconfig-load")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
file := filepath.Join(dir, "config.json")
if err := os.WriteFile(file, []byte(`{"value":"file"}`), 0644); err != nil {
t.Fatal(err)
}
lookup := func(name string) (string, bool) {
if name == "VALUE" {
return "env", true
}
return "", false
}
c := struct {
Value string
}{Value: "default"}
err = Load(&c,
WithProgramName("testbin"),
WithArgs([]string{"-value", "arg"}),
WithConfigFile(file),
WithEnvLookup(lookup),
)
AssertNil(t, err)
AssertEqual(t, c.Value, "arg")
}
func TestLoadWithoutImplicitConfigFile(t *testing.T) {
dir, err := os.MkdirTemp("", "goconfig-no-implicit")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
if err := os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"value":"file"}`), 0644); err != nil {
t.Fatal(err)
}
oldCwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
defer os.Chdir(oldCwd)
c := struct {
Value string
}{Value: "default"}
err = Load(&c,
WithArgs(nil),
WithoutImplicitConfigFile(),
WithEnvLookup(func(string) (string, bool) { return "", false }),
)
AssertNil(t, err)
AssertEqual(t, c.Value, "default")
}
func TestLoadInvalidTarget(t *testing.T) {
err := Load(struct{}{})
AssertNotNil(t, err)
AssertEqual(t, err.Error(), "config target must be a pointer")
}