From 90e81d89eb17bf3e57e203a958d2edca88988421 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Mon, 26 Jun 2017 21:04:11 +0200 Subject: [PATCH] add config_test.go --- config/config.go | 12 ++++++++---- config/config_test.go | 15 +++++++++++++++ main.go | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 config/config_test.go diff --git a/config/config.go b/config/config.go index dc94600..ad58a30 100644 --- a/config/config.go +++ b/config/config.go @@ -77,10 +77,14 @@ type Config struct { var config *Config -// LoadConfiguration loads the configuration from the disk. -func LoadConfiguration() error { - viper.SetConfigName("config") - viper.AddConfigPath(".") +func LoadConfiguration(path *string) error { + if path != nil { + viper.SetConfigFile(*path) + } else { + viper.AddConfigPath("./") + viper.SetConfigName("config") + } + // Find and read the config file if err := viper.ReadInConfig(); err != nil { return err diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..8261c91 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,15 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +var configFile = "../config.example.json" + +func TestLoadConfiguraiton(t *testing.T) { + err := LoadConfiguration(&configFile) + assert.Nil(t, err) + assert.Equal(t, Get().Web.ListenHost, "0.0.0.0") +} diff --git a/main.go b/main.go index 18f37d2..76f1d24 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ const ( func main() { fmt.Println("Loading configuration") - if err := config.LoadConfiguration(); err != nil { + if err := config.LoadConfiguration(nil); err != nil { log.WithError(err).Fatal("Failed to find configuration file") } tools.ConfigureLogging()