From b4dc5d6841f7ded5995e5f308509b1a3a034cbed Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 24 Dec 2020 15:18:24 +0100 Subject: Began implementing config validation --- config/config_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 13 deletions(-) (limited to 'config/config_test.go') diff --git a/config/config_test.go b/config/config_test.go index adbfc7e..1fcc57c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,19 +1,58 @@ 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 menu in action menu entries, and duplicate, and that anonymous and logged_in exist + // 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 example + if _, err := LoadFile("test_data/invalid_app.yaml"); err == nil { + t.Fatal("Invalid App entry should fail to load") + } + // Not enough menus example + if _, err := LoadFile("test_data/not_enough_menus.yaml"); err == nil { + t.Fatal("not enough menu entries should fail to load") + } + // Invalid Menus example + if _, err := LoadFile("test_data/invalid_menus.yaml"); err == nil { + t.Fatal("Invalid menu entry should fail to load") + } + // no anonymous Menu example + if _, err := LoadFile("test_data/no_anonymous_menu.yaml"); err == nil { + t.Fatal("Invalid menu entry should fail to load") + } + // no logged_in Menu example + if _, err := LoadFile("test_data/no_logged_in_menu.yaml"); err == nil { + t.Fatal("Invalid menu entry should fail to load") + } + + // Complexe example config, err := LoadFile("../example/complete.yaml") want := Config{ App: App{ @@ -28,8 +67,8 @@ func TestLoadFile(t *testing.T) { "mkdir %w/userdata/%u/ttyrec", }, }, - Menus: []Menu{ - Menu{ + Menus: map[string]Menu{ + "anonymous": Menu{ Banner: "Shell Game Launcher - Anonymous access%n======================================", XOffset: 5, YOffset: 2, @@ -49,11 +88,6 @@ func TestLoadFile(t *testing.T) { Label: "watch", Action: "watch_menu", }, - MenuEntry{ - Key: "s", - Label: "scores", - Action: "scores", - }, MenuEntry{ Key: "q", Label: "quit", @@ -61,7 +95,7 @@ func TestLoadFile(t *testing.T) { }, }, }, - Menu{ + "logged_in": Menu{ Banner: "Shell Game Launcher%n===================", XOffset: 5, YOffset: 2, @@ -74,17 +108,27 @@ func TestLoadFile(t *testing.T) { MenuEntry{ Key: "o", Label: "edit game options", - Action: "options", + Action: "menu options", }, MenuEntry{ Key: "w", Label: "watch", - Action: "watch_menu", + Action: "watch", + }, + MenuEntry{ + Key: "r", + Label: "replay", + Action: "replay", }, MenuEntry{ - Key: "s", - Label: "scores", - Action: "scores", + Key: "c", + Label: "change password", + Action: "passwd", + }, + MenuEntry{ + Key: "m", + Label: "change email", + Action: "chmail", }, MenuEntry{ Key: "q", @@ -98,6 +142,17 @@ func TestLoadFile(t *testing.T) { "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", + }, }, }, } -- cgit v1.2.3