Tests for settings
the build failed Details

This commit is contained in:
Antoine Bartuccio 2018-07-23 19:25:35 +02:00
parent 2c34fbefe2
commit 2cd142844d
Signed by: klmp200
GPG Key ID: E7245548C53F904B
4 changed files with 48 additions and 8 deletions

View File

@ -9,7 +9,6 @@ pipeline:
secrets: [ test_api_token ]
environment: [ test_api_token ]
commands:
- go get -v -d ./...
- go test ./...
publish:
image: plugins/docker

View File

@ -2,7 +2,7 @@
* @Author: Bartuccio Antoine
* @Date: 2018-07-23 15:24:22
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-23 17:25:09
* @Last Modified time: 2018-07-23 19:14:10
*/
package main
@ -15,7 +15,9 @@ import (
)
func main() {
settings.LoadSettings("settings.json", "settings_custom.json")
if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil {
log.Fatal(err)
}
log.Println("Bot initialisation")
b, err := tb.NewBot(tb.Settings{

View File

@ -2,7 +2,7 @@
* @Author: Bartuccio Antoine
* @Date: 2018-07-23 15:24:30
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-23 16:02:42
* @Last Modified time: 2018-07-23 19:14:44
*/
package settings
@ -27,18 +27,20 @@ func loadJson(path string) error {
}
// Load settings from given files paths
// Default settings are mandatory and program crash if there is an error at importation
// Default settings are mandatory and program should crash if there is an error at importation
// Custom settings are skipped if malformed or not found
func LoadSettings(settings_default_path, settings_custom_path string) {
func LoadSettings(settings_default_path, settings_custom_path string) error {
log.Println("Loading settings")
Settings = make(map[string]interface{})
if err := loadJson(settings_default_path); err != nil {
log.Println("error importing default settings")
log.Fatal(err)
log.Println(err)
return err
}
if err := loadJson(settings_custom_path); err != nil {
log.Println("error importing custom settings, skipping")
log.Println(err)
}
log.Println("Settings loaded")
return nil
}

View File

@ -2,7 +2,44 @@
* @Author: Bartuccio Antoine
* @Date: 2018-07-23 16:08:28
* @Last Modified by: klmp200
* @Last Modified time: 2018-07-23 16:08:37
* @Last Modified time: 2018-07-23 19:23:49
*/
package settings
import (
"testing"
)
func TestLoadSettings(t *testing.T) {
if LoadSettings("", "") == nil {
t.Error("no error for inexstant main settings file")
}
if LoadSettings("../settings.json", "") != nil {
t.Error("error for existant and well formated main settings file")
}
if LoadSettings("../settings.json", "settings.go") != nil {
t.Error("error for malformed custom settings json, should have ignored")
}
if LoadSettings("../settings.json", "../settings.json") != nil {
t.Error("error but everything should be fine")
}
if token, ok := Settings["token"]; !ok {
t.Error("token not loaded")
} else if token != "INSERT TOKEN HERE" {
t.Error("token is not \"INSERT TOKEN HERE\"")
}
}
func TestLoadJson(t *testing.T) {
t.Log("testing loadJson")
if loadJson("") == nil {
t.Error("no error for empty json")
}
if loadJson("../settings.json") != nil {
t.Error("error while loading settings.json")
}
if loadJson("settings.go") == nil {
t.Error("no error for malformed json")
}
}