Add tests for config package (failing for now)

This commit is contained in:
bodqhrohro 2019-10-22 22:55:43 +03:00
parent 9b4a09677a
commit 72c9dac62c
8 changed files with 89 additions and 6 deletions

View file

@ -1,2 +1,7 @@
.PHONY: all test
all:
go build -o telegabber
test:
go test -v ./config

View file

@ -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
}

26
config/config_test.go Normal file
View file

@ -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!")
}
}

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

View file

@ -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

22
test/bad_config.yml Normal file
View file

@ -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'

23
test/good_config.yml Normal file
View file

@ -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'