-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathflags_test.go
More file actions
223 lines (207 loc) · 6.62 KB
/
Copy pathflags_test.go
File metadata and controls
223 lines (207 loc) · 6.62 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package xlog
import (
"flag"
"os"
"testing"
)
func TestConfigurationDefaults(t *testing.T) {
// Reset flags to ensure clean test state
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Re-initialize flags
cwd, _ := os.Getwd()
flag.StringVar(&Config.Source, "source", cwd, "Directory that will act as a storage")
flag.StringVar(&Config.Build, "build", "", "Build all pages as static site in this directory")
flag.StringVar(&Config.Sitename, "sitename", "XLOG", "Site name is the name that appears on the header beside the logo and in the title tag")
flag.StringVar(&Config.Index, "index", "index", "Index file name used as home page")
flag.StringVar(&Config.NotFoundPage, "notfoundpage", "404", "Custom not found page")
flag.BoolVar(&Config.Readonly, "readonly", false, "Should xlog hide write operations, read-only means all write operations will be disabled")
flag.StringVar(&Config.BindAddress, "bind", "127.0.0.1:3000", "IP and port to bind the web server to")
flag.BoolVar(&Config.ServeInsecure, "serve-insecure", false, "Accept http connections and forward crsf cookie over non secure connections")
flag.StringVar(&Config.CsrfCookieName, "csrf-cookie", "xlog_csrf", "CSRF cookie name")
flag.StringVar(&Config.DisabledExtensions, "disabled-extensions", "", "disable list of extensions by name, comma separated, `all` will disable all extensions")
flag.StringVar(&Config.CodeStyle, "codestyle", "dracula", "code highlighting style name from the list supported by https://pkg.go.dev/github.com/alecthomas/chroma/v2/styles")
flag.StringVar(&Config.Theme, "theme", "", "bulma theme to use. (light, dark). empty value means system preference is used")
tests := []struct {
name string
field string
expected interface{}
}{
{"Default source should be cwd", "Source", cwd},
{"Default build should be empty", "Build", ""},
{"Default sitename should be XLOG", "Sitename", "XLOG"},
{"Default index should be index", "Index", "index"},
{"Default notfoundpage should be 404", "NotFoundPage", "404"},
{"Default readonly should be false", "Readonly", false},
{"Default bind address should be 127.0.0.1:3000", "BindAddress", "127.0.0.1:3000"},
{"Default serve-insecure should be false", "ServeInsecure", false},
{"Default csrf cookie should be xlog_csrf", "CsrfCookieName", "xlog_csrf"},
{"Default disabled-extensions should be empty", "DisabledExtensions", ""},
{"Default codestyle should be dracula", "CodeStyle", "dracula"},
{"Default theme should be empty", "Theme", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var actual interface{}
switch tt.field {
case "Source":
actual = Config.Source
case "Build":
actual = Config.Build
case "Sitename":
actual = Config.Sitename
case "Index":
actual = Config.Index
case "NotFoundPage":
actual = Config.NotFoundPage
case "Readonly":
actual = Config.Readonly
case "BindAddress":
actual = Config.BindAddress
case "ServeInsecure":
actual = Config.ServeInsecure
case "CsrfCookieName":
actual = Config.CsrfCookieName
case "DisabledExtensions":
actual = Config.DisabledExtensions
case "CodeStyle":
actual = Config.CodeStyle
case "Theme":
actual = Config.Theme
}
if actual != tt.expected {
t.Errorf("%s = %v, want %v", tt.field, actual, tt.expected)
}
})
}
}
func TestConfigurationStructFields(t *testing.T) {
config := Configuration{
Source: "/path/to/source",
Build: "/path/to/build",
Sitename: "My Site",
Index: "home",
NotFoundPage: "error",
BindAddress: "0.0.0.0:8080",
Theme: "dark",
CodeStyle: "monokai",
CsrfCookieName: "my_csrf",
DisabledExtensions: "ext1,ext2",
Readonly: true,
ServeInsecure: true,
}
tests := []struct {
name string
field string
expected interface{}
}{
{"Source field", "Source", "/path/to/source"},
{"Build field", "Build", "/path/to/build"},
{"Sitename field", "Sitename", "My Site"},
{"Index field", "Index", "home"},
{"NotFoundPage field", "NotFoundPage", "error"},
{"BindAddress field", "BindAddress", "0.0.0.0:8080"},
{"Theme field", "Theme", "dark"},
{"CodeStyle field", "CodeStyle", "monokai"},
{"CsrfCookieName field", "CsrfCookieName", "my_csrf"},
{"DisabledExtensions field", "DisabledExtensions", "ext1,ext2"},
{"Readonly field", "Readonly", true},
{"ServeInsecure field", "ServeInsecure", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var actual interface{}
switch tt.field {
case "Source":
actual = config.Source
case "Build":
actual = config.Build
case "Sitename":
actual = config.Sitename
case "Index":
actual = config.Index
case "NotFoundPage":
actual = config.NotFoundPage
case "BindAddress":
actual = config.BindAddress
case "Theme":
actual = config.Theme
case "CodeStyle":
actual = config.CodeStyle
case "CsrfCookieName":
actual = config.CsrfCookieName
case "DisabledExtensions":
actual = config.DisabledExtensions
case "Readonly":
actual = config.Readonly
case "ServeInsecure":
actual = config.ServeInsecure
}
if actual != tt.expected {
t.Errorf("%s = %v, want %v", tt.field, actual, tt.expected)
}
})
}
}
func TestVersionVariable(t *testing.T) {
tests := []struct {
name string
version string
want string
}{
{
name: "default version is dev",
version: "dev",
want: "dev",
},
{
name: "version can be set to release version",
version: "v1.2.3",
want: "v1.2.3",
},
{
name: "version can be set to commit hash",
version: "abc123",
want: "abc123",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Save and restore original version
origVersion := Version
defer func() { Version = origVersion }()
Version = tc.version
if Version != tc.want {
t.Errorf("Version = %q, want %q", Version, tc.want)
}
})
}
}
func TestShowVersionFlag(t *testing.T) {
tests := []struct {
name string
value bool
expected bool
}{
{
name: "ShowVersion can be true",
value: true,
expected: true,
},
{
name: "ShowVersion can be false",
value: false,
expected: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Save and restore original value
origValue := Config.ShowVersion
defer func() { Config.ShowVersion = origValue }()
Config.ShowVersion = tc.value
if Config.ShowVersion != tc.expected {
t.Errorf("Config.ShowVersion = %v, want %v", Config.ShowVersion, tc.expected)
}
})
}
}