From 72c9dac62cb6282841d22d877852bcee26bff9dd Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 22 Oct 2019 22:55:43 +0300 Subject: [PATCH] Add tests for config package (failing for now) --- Makefile | 5 +++++ config/config.go | 10 +++++----- config/config_test.go | 26 ++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ telegabber.go | 6 +++++- test/bad_config.yml | 22 ++++++++++++++++++++++ test/good_config.yml | 23 +++++++++++++++++++++++ 8 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 config/config_test.go create mode 100644 test/bad_config.yml create mode 100644 test/good_config.yml diff --git a/Makefile b/Makefile index d401472..6493cca 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,7 @@ +.PHONY: all test + all: go build -o telegabber + +test: + go test -v ./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!") + } +} diff --git a/go.mod b/go.mod index 0af1591..0193f42 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module dev.narayana.im/narayana/telegabber go 1.13 require ( + github.com/pkg/errors v0.8.1 gopkg.in/yaml.v2 v2.2.4 gosrc.io/xmpp v0.1.3 ) diff --git a/go.sum b/go.sum index e3d167a..e273364 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/telegabber.go b/telegabber.go index dd1e187..6c17c91 100644 --- a/telegabber.go +++ b/telegabber.go @@ -10,7 +10,11 @@ import ( const CONFIG_PATH string = "config.yml" func main() { - config := config.ReadConfig(CONFIG_PATH) + config, err := config.ReadConfig(CONFIG_PATH) + if err != nil { + log.Fatal(err) + } + cm := xmpp.NewComponent(config.Xmpp) // reconnect automatically diff --git a/test/bad_config.yml b/test/bad_config.yml new file mode 100644 index 0000000..83dbdf1 --- /dev/null +++ b/test/bad_config.yml @@ -0,0 +1,22 @@ +:telegram: + :loglevel: :warn + :content: + :path: '/var/www/telegabber/content' # webserver workdir + :link: 'http://tlgrm.localhost/content' # webserver public address + :upload: 'https:///xmppfiles.localhost' # xmpp http upload address + :tdlib_verbosity: 1 + :tdlib: + :lib_path: 'lib/' + :client: + :api_id: '17349' + :api_hash: '344583e45741c457fe1862106095a5eb' + :device_model: 'telegabber' + :application_version: '2.0' + :use_cat_info_database: false + +:xmpp: + :loglevel: :warn + :host: '127.0.0.1' + :port: 8899 + :password: 'password' + :db: 'sessions.dat' diff --git a/test/good_config.yml b/test/good_config.yml new file mode 100644 index 0000000..3ea257c --- /dev/null +++ b/test/good_config.yml @@ -0,0 +1,23 @@ +:telegram: + :loglevel: :warn + :content: + :path: '' # webserver workdir + :link: '' # webserver public address + :upload: '' # xmpp http upload address + :tdlib_verbosity: 1 + :tdlib: + :lib_path: 'lib/' + :client: + :api_id: '17349' + :api_hash: '344583e45741c457fe1862106095a5eb' + :device_model: 'telegabber' + :application_version: '2.0' + :use_chat_info_database: false + +:xmpp: + :loglevel: :warn + :jid: 'tlgrm.localhost' + :host: '127.0.0.1' + :port: 8899 + :password: 'password' + :db: 'sessions.dat'