-
Notifications
You must be signed in to change notification settings - Fork 382
/
usage_test.go
155 lines (138 loc) · 3.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2016 Kelsey Hightower and others. All rights reserved.
// Use of this source code is governed by the MIT License that can be found in
// the LICENSE file.
package envconfig
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"strings"
"testing"
"text/tabwriter"
)
var testUsageTableResult, testUsageListResult, testUsageCustomResult, testUsageBadFormatResult string
func TestMain(m *testing.M) {
// Load the expected test results from a text file
data, err := ioutil.ReadFile("testdata/default_table.txt")
if err != nil {
log.Fatal(err)
}
testUsageTableResult = string(data)
data, err = ioutil.ReadFile("testdata/default_list.txt")
if err != nil {
log.Fatal(err)
}
testUsageListResult = string(data)
data, err = ioutil.ReadFile("testdata/custom.txt")
if err != nil {
log.Fatal(err)
}
testUsageCustomResult = string(data)
data, err = ioutil.ReadFile("testdata/fault.txt")
if err != nil {
log.Fatal(err)
}
testUsageBadFormatResult = string(data)
retCode := m.Run()
os.Exit(retCode)
}
func compareUsage(want, got string, t *testing.T) {
got = strings.ReplaceAll(got, " ", ".")
if want != got {
shortest := len(want)
if len(got) < shortest {
shortest = len(got)
}
if len(want) != len(got) {
t.Errorf("expected result length of %d, found %d", len(want), len(got))
}
for i := 0; i < shortest; i++ {
if want[i] != got[i] {
t.Errorf("difference at index %d, expected '%c' (%v), found '%c' (%v)\n",
i, want[i], want[i], got[i], got[i])
break
}
}
t.Errorf("Complete Expected:\n'%s'\nComplete Found:\n'%s'\n", want, got)
}
}
func TestUsageDefault(t *testing.T) {
var s Specification
os.Clearenv()
save := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err := Usage("env_config", &s)
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = save // restoring the real stdout
out := <-outC
if err != nil {
t.Error(err.Error())
}
compareUsage(testUsageTableResult, out, t)
}
func TestUsageTable(t *testing.T) {
var s Specification
os.Clearenv()
buf := new(bytes.Buffer)
tabs := tabwriter.NewWriter(buf, 1, 0, 4, ' ', 0)
err := Usagef("env_config", &s, tabs, DefaultTableFormat)
tabs.Flush()
if err != nil {
t.Error(err.Error())
}
compareUsage(testUsageTableResult, buf.String(), t)
}
func TestUsageList(t *testing.T) {
var s Specification
os.Clearenv()
buf := new(bytes.Buffer)
err := Usagef("env_config", &s, buf, DefaultListFormat)
if err != nil {
t.Error(err.Error())
}
compareUsage(testUsageListResult, buf.String(), t)
}
func TestUsageCustomFormat(t *testing.T) {
var s Specification
os.Clearenv()
buf := new(bytes.Buffer)
err := Usagef("env_config", &s, buf, "{{range .}}{{usage_key .}}={{usage_description .}}\n{{end}}")
if err != nil {
t.Error(err.Error())
}
compareUsage(testUsageCustomResult, buf.String(), t)
}
func TestUsageUnknownKeyFormat(t *testing.T) {
var s Specification
unknownError := "template: envconfig:1:2: executing \"envconfig\" at <.UnknownKey>"
os.Clearenv()
buf := new(bytes.Buffer)
err := Usagef("env_config", &s, buf, "{{.UnknownKey}}")
if err == nil {
t.Errorf("expected 'unknown key' error, but got no error")
}
if !strings.Contains(err.Error(), unknownError) {
t.Errorf("expected '%s', but got '%s'", unknownError, err.Error())
}
}
func TestUsageBadFormat(t *testing.T) {
var s Specification
os.Clearenv()
// If you don't use two {{}} then you get a lieteral
buf := new(bytes.Buffer)
err := Usagef("env_config", &s, buf, "{{range .}}{.Key}\n{{end}}")
if err != nil {
t.Error(err.Error())
}
compareUsage(testUsageBadFormatResult, buf.String(), t)
}