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
|
package config
import (
"os"
"reflect"
"testing"
)
func TestLoadFile(t *testing.T) {
// Non existant file
_, err := LoadFile("test_data/non-existant")
if err == nil {
t.Fatal("non-existant config file failed without error")
}
// Invalid yaml file
_, err = LoadFile("test_data/invalid_yaml")
if err == nil {
t.Fatal("invalid_yaml config file failed without error")
}
// TODO test non existant game in play actions, and duplicate
//menuEntry = MenuEntry{
//Key: "p",
//Label: "play non existant game",
//Action: "play nonexistant",
//}
//if err := menuEntry.validate(); err == nil {
//t.Fatal("An inexistant game cannot be played")
//}
t.Cleanup(func() { os.RemoveAll("var/") })
// Invalid App
if _, err := LoadFile("test_data/invalid_app.yaml"); err == nil {
t.Fatal("Invalid App entry should fail to load")
}
// Not enough menus
if _, err := LoadFile("test_data/not_enough_menus.yaml"); err == nil {
t.Fatal("not enough menu entries should fail to load")
}
// Invalid Menus
if _, err := LoadFile("test_data/invalid_menus.yaml"); err == nil {
t.Fatal("Invalid menu entry should fail to load")
}
// no anonymous Menu
if _, err := LoadFile("test_data/no_anonymous_menu.yaml"); err == nil {
t.Fatal("Invalid menu entry should fail to load")
}
// no logged_in Menu
if _, err := LoadFile("test_data/no_logged_in_menu.yaml"); err == nil {
t.Fatal("Invalid menu entry should fail to load")
}
// duplicate menu
if _, err := LoadFile("test_data/duplicate_menu.yaml"); err == nil {
t.Fatal("duplicate menu should fail to load")
}
// non existant menu action referenced
if _, err := LoadFile("test_data/non_existant_menu.yaml"); err == nil {
t.Fatal("menu entry referencing a non existant menu should fail to load")
}
// non existant game referenced in play action
if _, err := LoadFile("test_data/non_existant_game.yaml"); err == nil {
t.Fatal("menu entry referencing a non existant play action should fail to load")
}
// unreachable menu
if _, err := LoadFile("test_data/unreachable_menu.yaml"); err == nil {
t.Fatal("unreachable menu should fail to load")
}
// unreachable game
if _, err := LoadFile("test_data/unreachable_game.yaml"); err == nil {
t.Fatal("unreachable game should fail to load")
}
// Complexe example
config, err := LoadFile("../example/complete.yaml")
want := Config{
App: App{
WorkingDirectory: "var/",
MaxUsers: 512,
AllowRegistration: true,
MaxNickLen: 15,
MenuMaxIdleTime: 600,
PostLoginCommands: []string{
"mkdir %w/userdata/%u",
"mkdir %w/userdata/%u/dumplog",
"mkdir %w/userdata/%u/ttyrec",
},
},
Menus: map[string]Menu{
"anonymous": Menu{
Banner: "Shell Game Launcher - Anonymous access%n======================================",
XOffset: 5,
YOffset: 2,
MenuEntries: []MenuEntry{
MenuEntry{
Key: "l",
Label: "login",
Action: "login",
},
MenuEntry{
Key: "r",
Label: "register",
Action: "register",
},
MenuEntry{
Key: "w",
Label: "watch",
Action: "watch_menu",
},
MenuEntry{
Key: "q",
Label: "quit",
Action: "quit",
},
},
},
"logged_in": Menu{
Banner: "Shell Game Launcher%n===================",
XOffset: 5,
YOffset: 2,
MenuEntries: []MenuEntry{
MenuEntry{
Key: "p",
Label: "play Nethack 3.7",
Action: "play nethack3.7",
},
MenuEntry{
Key: "o",
Label: "edit game options",
Action: "menu options",
},
MenuEntry{
Key: "w",
Label: "watch",
Action: "watch",
},
MenuEntry{
Key: "r",
Label: "replay",
Action: "replay",
},
MenuEntry{
Key: "c",
Label: "change password",
Action: "passwd",
},
MenuEntry{
Key: "m",
Label: "change email",
Action: "chmail",
},
MenuEntry{
Key: "q",
Label: "quit",
Action: "quit",
},
},
},
"options": Menu{
Banner: "Options%n=======",
XOffset: 5,
YOffset: 2,
MenuEntries: []MenuEntry{
MenuEntry{
Key: "z",
Label: "back",
Action: "menu logged_in",
},
},
},
},
Games: map[string]Game{
"nethack3.7": Game{
ChrootPath: "/opt/nethack",
FileMode: "0666",
ScoreCommands: []string{
"exec /games/nethack -s all",
"wait",
},
Commands: []string{
"cp /games/var/save/%u%n.gz /games/var/save/%u%n.gz.bak",
"exec /games/nethack -u %n",
},
Env: map[string]string{
"NETHACKOPTIONS": "@%ruserdata/%n/%n.nhrc",
},
},
},
}
if err != nil || !reflect.DeepEqual(want, config) {
t.Fatalf("complete example failed:\nerror %v\nwant:%+v\ngot: %+v", err, want, config)
}
}
|