-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathusage_test.go
More file actions
93 lines (75 loc) · 3.19 KB
/
usage_test.go
File metadata and controls
93 lines (75 loc) · 3.19 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
82
83
84
85
86
87
88
89
90
91
92
93
package uconfig_test
import (
"bytes"
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/omeid/uconfig"
"github.com/omeid/uconfig/flat"
"github.com/omeid/uconfig/internal/f"
"github.com/omeid/uconfig/plugins"
"github.com/omeid/uconfig/plugins/secret"
)
const expectedUsageMessage = `Usage:
uconfig.test [flags] [command]
Configurations:
FIELD FLAG ENV DEFAULT GOODPLUGIN SECRET USAGE
----- ----- ----- ------- ---------- ------ -----
Version -version VERSION Version
GoHard -gohard GOHARD GoHard
Redis.Address -redis-address REDIS_ADDRESS Redis.Address
Redis.Port -redis-port REDIS_PORT Redis.Port
Rethink.Host.Address -rethink-host-address RETHINK_HOST_ADDRESS Rethink.Host.Address
Rethink.Host.Port -rethink-host-port RETHINK_HOST_PORT Rethink.Host.Port
Rethink.Db -rethink-db RETHINK_DB primary Rethink.Db main database used by our application
Rethink.Password -rethink-password RETHINK_PASSWORD Rethink.Password RETHINK_PASSWORD
Command [command] COMMAND run Command
Configuration Files:
/etc/app/config.yaml
config.json
`
type UselessPluginVisitor struct {
plugins.Plugin
}
func (*UselessPluginVisitor) Parse() error { return nil }
func (*UselessPluginVisitor) Visit(fields flat.Fields) error {
for _, f := range fields {
name, _ := f.Name("goodplugin")
f.Meta()["goodplugin"] = name
}
return nil
}
var files = uconfig.Files{
{Path: "/etc/app/config.yaml", Unmarshal: json.Unmarshal, Optional: true},
{Path: "config.json", Unmarshal: json.Unmarshal, Optional: true}, // just for testing of file listing.
}
func TestUsage(t *testing.T) {
var stdout bytes.Buffer
uconfig.UsageOutput = &stdout
defer func() {
if r := recover(); r != nil {
t.Errorf("Usage should not panic, but did: %v", r)
}
}()
// good plugin is used just so that we have more than
// one tag/field that isn't pre-weighted in "usage".
noopPlugin := &UselessPluginVisitor{}
secretProvider := func(name string) (string, error) { return "top secret token", nil }
conf := uconfig.Classic[f.Config](files, secret.New(secretProvider), noopPlugin)
_, err := conf.Parse()
if err != nil {
t.Fatal(err)
}
if size := stdout.Len(); size != 0 {
t.Fatalf(
"Expectedd nothing in UsageOutput before usage, got len: %v\n%s",
size,
stdout.String(),
)
}
conf.Usage()
output := stdout.String()
if diff := cmp.Diff(expectedUsageMessage, output); diff != "" {
t.Error(diff)
}
}