-
Notifications
You must be signed in to change notification settings - Fork 9
/
usage_test.go
80 lines (64 loc) · 2.71 KB
/
usage_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
package uconfig_test
import (
"bytes"
"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 = `
Supported Fields:
FIELD FLAG ENV DEFAULT GOODPLUGIN SECRET USAGE
----- ----- ----- ------- ---------- ------ -----
Version -version VERSION Version
GoHard -gohard GOHARD GoHard
Redis.Host -redis-host REDIS_HOST Redis.Host
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
`
type UselessPluginVisitor struct {
plugins.Plugin
}
func (*UselessPluginVisitor) Parse() error { return nil }
func (*UselessPluginVisitor) Visit(fields flat.Fields) error {
for _, f := range fields {
f.Meta()["goodplugin"] = f.Name()
}
return nil
}
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{}
value := f.Config{}
secretProvider := func(name string) (string, error) { return "top secret token", nil }
c, err := uconfig.Classic(&value, nil, secret.New(secretProvider), noopPlugin)
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(),
)
}
c.Usage()
output := stdout.String()
if diff := cmp.Diff(expectedUsageMessage, output); diff != "" {
t.Error(diff)
}
}