From 72c9dac62cb6282841d22d877852bcee26bff9dd Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 22 Oct 2019 22:55:43 +0300 Subject: Add tests for config package (failing for now) --- config/config.go | 10 +++++----- config/config_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 config/config_test.go (limited to 'config') diff --git a/config/config.go b/config/config.go index be05cb5..2ec6706 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,8 @@ package config import ( + "github.com/pkg/errors" "io/ioutil" - "log" "gopkg.in/yaml.v2" ) @@ -47,18 +47,18 @@ type TelegramTdlibClientConfig struct { UseChatInfoDatabase bool `yaml:":use_chat_info_database"` } -func ReadConfig(path string) Config { +func ReadConfig(path string) (Config, error) { var config Config file, err := ioutil.ReadFile(path) if err != nil { - log.Fatalf("Can't open config file: %v", err) + return config, errors.Wrap(err, "Can't open config file") } err = yaml.Unmarshal(file, &config) if err != nil { - log.Fatalf("Error parsing config: %v", err) + return config, errors.Wrap(err, "Error parsing config") } - return config + return config, nil } diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..34009d7 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,26 @@ +package config + +import ( + "testing" +) + +func TestNoConfig(t *testing.T) { + _, err := ReadConfig("../test/sfklase.yml") + if err == nil { + t.Errorf("Non-existent config was successfully read") + } +} + +func TestGoodConfig(t *testing.T) { + _, err := ReadConfig("../test/good_config.yml") + if err != nil { + t.Errorf("Good config is not accepted: %v", err) + } +} + +func TestBadConfig(t *testing.T) { + _, err := ReadConfig("../test/bad_config.yml") + if err == nil { + t.Errorf("Bad config is accepted but it shoudn't!") + } +} -- cgit v1.2.3