Add tests for config package (failing for now)
This commit is contained in:
parent
9b4a09677a
commit
72c9dac62c
5
Makefile
5
Makefile
|
@ -1,2 +1,7 @@
|
||||||
|
.PHONY: all test
|
||||||
|
|
||||||
all:
|
all:
|
||||||
go build -o telegabber
|
go build -o telegabber
|
||||||
|
|
||||||
|
test:
|
||||||
|
go test -v ./config
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
@ -47,18 +47,18 @@ type TelegramTdlibClientConfig struct {
|
||||||
UseChatInfoDatabase bool `yaml:":use_chat_info_database"`
|
UseChatInfoDatabase bool `yaml:":use_chat_info_database"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadConfig(path string) Config {
|
func ReadConfig(path string) (Config, error) {
|
||||||
var config Config
|
var config Config
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(path)
|
file, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
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)
|
err = yaml.Unmarshal(file, &config)
|
||||||
if err != nil {
|
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
26
config/config_test.go
Normal 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
1
go.mod
|
@ -3,6 +3,7 @@ module dev.narayana.im/narayana/telegabber
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/pkg/errors v0.8.1
|
||||||
gopkg.in/yaml.v2 v2.2.4
|
gopkg.in/yaml.v2 v2.2.4
|
||||||
gosrc.io/xmpp v0.1.3
|
gosrc.io/xmpp v0.1.3
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,4 +1,6 @@
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
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 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
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=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|
|
@ -10,7 +10,11 @@ import (
|
||||||
const CONFIG_PATH string = "config.yml"
|
const CONFIG_PATH string = "config.yml"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := config.ReadConfig(CONFIG_PATH)
|
config, err := config.ReadConfig(CONFIG_PATH)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
cm := xmpp.NewComponent(config.Xmpp)
|
cm := xmpp.NewComponent(config.Xmpp)
|
||||||
|
|
||||||
// reconnect automatically
|
// reconnect automatically
|
||||||
|
|
22
test/bad_config.yml
Normal file
22
test/bad_config.yml
Normal 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
23
test/good_config.yml
Normal 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'
|
Loading…
Reference in a new issue