-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_test.go
More file actions
138 lines (129 loc) · 3.07 KB
/
Copy pathusage_test.go
File metadata and controls
138 lines (129 loc) · 3.07 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package envstruct
import (
"bytes"
"net/url"
"strings"
"testing"
)
func TestUsageBasic(t *testing.T) {
type Config struct {
Host string
Port int
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "APP_HOST") {
t.Fatalf("expected APP_HOST in output: %s", out)
}
if !strings.Contains(out, "APP_PORT") {
t.Fatalf("expected APP_PORT in output: %s", out)
}
if !strings.Contains(out, "string") {
t.Fatalf("expected 'string' type in output: %s", out)
}
if !strings.Contains(out, "int") {
t.Fatalf("expected 'int' type in output: %s", out)
}
}
func TestUsageHeader(t *testing.T) {
type Config struct {
Host string
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
lines := strings.Split(out, "\n")
if len(lines) == 0 || !strings.Contains(lines[0], "KEY") || !strings.Contains(lines[0], "TYPE") {
t.Fatalf("expected header row with KEY and TYPE, got: %s", out)
}
}
func TestUsageRequired(t *testing.T) {
type Config struct {
Host string `env:"HOST,required"`
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "[required]") {
t.Fatalf("expected [required] in output: %s", out)
}
}
func TestUsageDefault(t *testing.T) {
type Config struct {
Host string `default:"localhost"`
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "[default: localhost]") {
t.Fatalf("expected [default: localhost] in output: %s", out)
}
}
func TestUsageDescription(t *testing.T) {
type Config struct {
Host string `desc:"Server hostname"`
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "Server hostname") {
t.Fatalf("expected 'Server hostname' in output: %s", out)
}
}
func TestUsageNested(t *testing.T) {
type DB struct {
Host string
}
type Config struct {
Database DB
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "APP_DATABASE_HOST") {
t.Fatalf("expected APP_DATABASE_HOST in output: %s", out)
}
}
func TestUsageIgnored(t *testing.T) {
type Config struct {
Host string
Secret string `env:"-"`
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if strings.Contains(out, "SECRET") {
t.Fatalf("expected no SECRET in output: %s", out)
}
}
func TestUsageURLType(t *testing.T) {
type Config struct {
Endpoint *url.URL `env:"URL,required" desc:"Database URL"`
}
var buf bytes.Buffer
if err := Usage("APP", &Config{}, &buf); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "*url.URL") {
t.Fatalf("expected '*url.URL' in output: %s", out)
}
if !strings.Contains(out, "[required]") {
t.Fatalf("expected [required] in output: %s", out)
}
}