aboutsummaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-10-22 22:55:43 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-10-22 22:56:11 +0300
commit72c9dac62cb6282841d22d877852bcee26bff9dd (patch)
tree772c3bc19f4461f6ffd427a61476c53b8b75158d /config
parent9b4a09677a352d9938e4505bfc2d0c8d304567ac (diff)
Add tests for config package (failing for now)
Diffstat (limited to 'config')
-rw-r--r--config/config.go10
-rw-r--r--config/config_test.go26
2 files changed, 31 insertions, 5 deletions
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!")
+ }
+}