-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
255 lines (226 loc) · 6.38 KB
/
config_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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright © 2019, 2024 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <support@mwat.de>
*/
package nele
import (
"os"
"testing"
ht "github.com/mwat56/hashtags"
)
//lint:file-ignore ST1017 - I prefer Yoda conditions
// `prep4Tests()` prepares the environment for testing.
//
// It sets the binary storage flag to false, initializes the configuration,
// sets the persistence to filesystem-based, and sets the posting base
// directory.
//
// This function is meant for unit testing only.
func prep4Tests() {
ht.UseBinaryStorage = false
// `InitConfig()` calls `flag.parse()` which in turn will cause
// errors when run with `go test …`.
InitConfig()
// make sure we've got a clean slate for every test:
tmpDir := "/tmp/postings/"
os.RemoveAll(tmpDir)
SetPostingBaseDirectory(tmpDir)
} // prep4Tests()
// --------------------------------------------------------------------------
/*
// `parseAppArgsDebug()` calls `parseAppArgs()` and returns `AppArgs`.
//
// This function is meant for unit testing only.
func parseAppArgsDebug() *TAppArgs {
flag.CommandLine = flag.NewFlagSet(`Nele`, flag.ExitOnError)
// Define some flags used by `testing` to avoid
// bailing out during the test.
var coverprofile, paniconexit, run, testlogfile, timeout, v string
flag.CommandLine.StringVar(&coverprofile, `test.coverprofile`, coverprofile,
"coverprofile for tests")
flag.CommandLine.StringVar(&paniconexit, `test.paniconexit0`, paniconexit,
"paniconexit for tests")
flag.CommandLine.StringVar(&run, `test.run`, run,
"run for tests")
flag.CommandLine.StringVar(&testlogfile, `test.testlogfile`, testlogfile,
"testlogfile for tests")
flag.CommandLine.StringVar(&timeout, `test.timeout`, timeout,
"timeout for tests")
flag.CommandLine.StringVar(&v, `test.v`, v, `test.v`)
parseCmdlineArgs()
return &AppArgs
} // parseAppArgsDebug()
// `readAppArgDebug()` calls `readAppArgs()` and returns `AppArgs`.
//
// This function is meant for unit testing only.
func readAppArgsDebug() *TAppArgs {
flag.CommandLine = flag.NewFlagSet(`Nele`, flag.ExitOnError)
AppArgs = TAppArgs{}
InitConfig()
return &AppArgs
} // readAppArgsDebug()
// `setAppArgsDebug()` calls `setAppArgs()` and returns `AppArgs`.
//
// This function is meant for unit testing only.
func setAppArgsDebug() *TAppArgs {
flag.CommandLine = flag.NewFlagSet(`Nele`, flag.ExitOnError)
AppArgs = TAppArgs{}
var ini1 ini.TSectionList
// Clear/reset the INI values to simulate missing INI file(s):
iniValues = ini1.GetSection("")
readCmdlineArgs()
return &AppArgs
} // setAppArgsDebug()
*/
func Test_absolute(t *testing.T) {
bd := "/var/tmp"
type args struct {
aBaseDir string
aDir string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
{" 1", args{bd, ``}, `/home/matthias/devel/Go/src/github.com/mwat56/nele`},
{" 2", args{bd, "/opt/"}, "/opt"},
{" 3", args{bd, "./dir/"}, "/var/tmp/dir"},
{" 4", args{bd, "../opt/file.txt"}, "/var/opt/file.txt"},
{" 5", args{bd, "./bla.doc"}, "/var/tmp/bla.doc"},
{" 6", args{"", "dir"}, "/home/matthias/devel/Go/src/github.com/mwat56/nele/dir"},
{" 7", args{"", "../../../dir"}, "/home/matthias/devel/Go/src/dir"},
{" 8", args{"/", "../../../dir"}, "/dir"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := absolute(tt.args.aBaseDir, tt.args.aDir); got != tt.want {
t.Errorf("absolute() = '%v',\nwant '%v'", got, tt.want)
}
})
}
} // Test_absolute()
func Test_kmg2Num(t *testing.T) {
type args struct {
aString string
}
tests := []struct {
name string
args args
wantRInt int64
}{
// TODO: Add test cases.
{"0", args{""}, 0},
{"1", args{"1"}, 1},
{"2", args{"2kb"}, 2048},
{"3", args{"3 MB"}, 3145728},
{"4", args{"4 B"}, 4},
{"5", args{"5gb"}, 5368709120},
{"6", args{"10 Mb"}, 10485760},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotRInt := kmg2Num(tt.args.aString); gotRInt != tt.wantRInt {
t.Errorf("kmg2Num() = %v, want %v", gotRInt, tt.wantRInt)
}
})
}
} // Test_kmg2Num()
/*
func Test_parseAppArgsDebug(t *testing.T) {
expected := &TAppArgs{}
tests := []struct {
name string
want *TAppArgs
}{
// TODO: Add test cases.
{" 1", expected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseAppArgsDebug(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseAppArgsDebug() = %s,\nwant %s", got, tt.want)
}
})
}
} // Test_parseAppArgsDebug()
func Test_readAppArgsDebug(t *testing.T) {
expected := &TAppArgs{
Addr: `127.0.0.1:8181`,
BlogName: `<! BlogName not configured !>`,
DataDir: `/home/matthias/devel/Go/src/github.com/mwat56/nele`,
delWhitespace: true,
GZip: true,
HashFile: "/home/matthias/devel/Go/src/github.com/mwat56/nele/HashFile.db",
Lang: `en`,
listen: `127.0.0.1`,
MaxFileSize: 10485760,
mfs: `10485760`,
port: 8181,
Realm: `My Blog`,
Theme: `dark`,
}
tests := []struct {
name string
want *TAppArgs
}{
// TODO: Add test cases.
{" 1", expected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := readAppArgsDebug(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("readAppArgsDebug() = %s,\nwant %s", got, tt.want)
}
})
}
} // Test_readAppArgsDebug()
func Test_setAppArgsDebug(t *testing.T) {
expected := &TAppArgs{
BlogName: `<! BlogName not configured !>`,
DataDir: `/home/matthias/devel/Go/src/github.com/mwat56/nele`,
delWhitespace: true,
GZip: true,
HashFile: "/home/matthias/devel/Go/src/github.com/mwat56/nele/HashFile.db",
Lang: `en`,
listen: `127.0.0.1`,
mfs: `10485760`,
port: 8181,
Realm: `My Blog`,
Theme: `dark`,
}
tests := []struct {
name string
want *TAppArgs
}{
// TODO: Add test cases.
{" 1", expected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := setAppArgsDebug(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("setAppArgsDebug() = %s,\nwant %s", got, tt.want)
}
})
}
} // Test_setAppArgsDebug()
*/
/*
func TestShowHelp(t *testing.T) {
_ = setAppArgsDebug()
tests := []struct {
name string
}{
// TODO: Add test cases.
{" 1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ShowHelp()
})
}
} // TestShowHelp()
*/
/* _EoF_ */