-
Notifications
You must be signed in to change notification settings - Fork 3
/
argument_handler_test.go
146 lines (106 loc) · 3.54 KB
/
argument_handler_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
package andrew_test
import (
"bytes"
"strings"
"testing"
"github.com/playtechnique/andrew"
)
func TestMainCalledWithHelpOptionDisplaysHelp(t *testing.T) {
t.Parallel()
args := []string{"--help"}
received := new(bytes.Buffer)
exit := andrew.Main(args, received)
if exit != 0 {
t.Error("Expected exit value 0, received %i", exit)
}
if !strings.Contains(received.String(), "Usage") {
t.Errorf("Expected help message containing 'Usage', received %s", received)
}
}
func TestMainCalledWithNoArgsUsesDefaults(t *testing.T) {
t.Parallel()
contentRoot, address, baseUrl := andrew.ParseArgs([]string{})
if contentRoot != andrew.DefaultContentRoot {
t.Errorf("contentroot should be %s, received %q", andrew.DefaultContentRoot, contentRoot)
}
if address != andrew.DefaultAddress {
t.Errorf("address should be %s, received %q", andrew.DefaultAddress, address)
}
if baseUrl != andrew.DefaultBaseUrl {
t.Errorf("baseUrl should be %s, received %q", andrew.DefaultBaseUrl, baseUrl)
}
}
func TestMainCalledWithArgsOverridesDefaults(t *testing.T) {
t.Parallel()
contentRoot, address, baseUrl := andrew.ParseArgs([]string{"1", "2", "3"})
if contentRoot != "1" {
t.Errorf("contentroot should be %s, received %q", "1", contentRoot)
}
if address != "2" {
t.Errorf("address should be %s, received %q", "2", address)
}
if baseUrl != "3" {
t.Errorf("baseUrl should be %s, received %q", "3", baseUrl)
}
}
func TestMainCalledWithInvalidAddressPanics(t *testing.T) {
t.Parallel()
args := []string{".", "notanipaddress"}
nullLogger := new(bytes.Buffer)
// No need to check whether `recover()` is nil. Just turn off the panic.
defer func() {
err := recover()
if err == nil {
t.Fatalf("Expected panic with invalid address, received %v", err)
}
}()
andrew.Main(args, nullLogger)
}
func TestMainCalledWithCertOptionWithoutPathFails(t *testing.T) {
t.Parallel()
args := []string{"--cert"}
received := new(bytes.Buffer)
exit := andrew.Main(args, received)
if exit != 1 {
t.Error("Expected exit value 1, received %i", exit)
}
if !strings.Contains(received.String(), "missing certificate path") {
t.Errorf("Expected help message containing 'missing certificate path', received %s", received)
}
}
func TestMainCalledWithCertOptionWithoutPrivateKeyFails(t *testing.T) {
t.Parallel()
args := []string{"--cert", "testdata/test-cert.crt"}
received := new(bytes.Buffer)
exit := andrew.Main(args, received)
if exit != 1 {
t.Error("Expected exit value 1, received %i", exit)
}
if !strings.Contains(received.String(), "must be provided together") {
t.Errorf("Expected help message containing 'must be provided together', received %s", received)
}
}
func TestMainCalledWithPrivateKeyOptionWithoutPathFails(t *testing.T) {
t.Parallel()
args := []string{"--privatekey"}
received := new(bytes.Buffer)
exit := andrew.Main(args, received)
if exit != 1 {
t.Error("Expected exit value 1, received %i", exit)
}
if !strings.Contains(received.String(), "missing private key path") {
t.Errorf("Expected help message containing 'missing private key path', received %s", received)
}
}
func TestMainCalledWithPrivateKeyOptionWithoutCertFails(t *testing.T) {
t.Parallel()
args := []string{"--privatekey", "testdata/test-cert.crt"}
received := new(bytes.Buffer)
exit := andrew.Main(args, received)
if exit != 1 {
t.Error("Expected exit value 1, received %i", exit)
}
if !strings.Contains(received.String(), "must be provided together") {
t.Errorf("Expected help message containing 'must be provided together', received %s", received)
}
}