Fix tests and make megacheck happy

This commit is contained in:
Wim 2018-03-05 00:30:46 +01:00
parent f2f1d874e1
commit 98027446c8
4 changed files with 29 additions and 34 deletions

View File

@ -68,12 +68,8 @@ func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map
return nil return nil
} }
func (b *Bridge) ReloadConfig() {
return
}
func (b *Bridge) GetBool(key string) bool { func (b *Bridge) GetBool(key string) bool {
if b.Config.GetBool(b.Account+"."+key) != false { if b.Config.GetBool(b.Account + "." + key) {
return b.Config.GetBool(b.Account + "." + key) return b.Config.GetBool(b.Account + "." + key)
} }
return b.Config.GetBool("general." + key) return b.Config.GetBool("general." + key)

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"bytes"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
"os" "os"
@ -192,6 +193,23 @@ func NewConfig(cfgfile string) *Config {
return mycfg return mycfg
} }
func NewConfigFromString(input []byte) *Config {
var cfg ConfigValues
viper.SetConfigType("toml")
err := viper.ReadConfig(bytes.NewBuffer(input))
if err != nil {
log.Fatal(err)
}
err = viper.Unmarshal(&cfg)
if err != nil {
log.Fatal(err)
}
mycfg := new(Config)
mycfg.v = viper.GetViper()
mycfg.ConfigValues = &cfg
return mycfg
}
func (c *Config) GetBool(key string) bool { func (c *Config) GetBool(key string) bool {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()

View File

@ -23,7 +23,6 @@ import (
"github.com/hashicorp/golang-lru" "github.com/hashicorp/golang-lru"
"github.com/peterhellberg/emojilib" "github.com/peterhellberg/emojilib"
"net/http" "net/http"
"reflect"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -450,14 +449,6 @@ func getChannelID(msg config.Message) string {
return msg.Channel + msg.Account return msg.Channel + msg.Account
} }
//getField returns the Protocol configuration for a specific protocol (field)
func getField(cfg *config.Config, field string) map[string]config.Protocol {
r := reflect.ValueOf(cfg)
f := reflect.Indirect(r).FieldByName(field)
i := f.Interface()
return i.(map[string]config.Protocol)
}
func isApi(account string) bool { func isApi(account string) bool {
return strings.HasPrefix(account, "api.") return strings.HasPrefix(account, "api.")
} }

View File

@ -3,14 +3,13 @@ package gateway
import ( import (
"fmt" "fmt"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"strconv" "strconv"
"testing" "testing"
) )
var testconfig = ` var testconfig = []byte(`
[irc.freenode] [irc.freenode]
[mattermost.test] [mattermost.test]
[gitter.42wim] [gitter.42wim]
@ -37,9 +36,9 @@ var testconfig = `
[[gateway.inout]] [[gateway.inout]]
account="slack.test" account="slack.test"
channel="testing" channel="testing"
` `)
var testconfig2 = ` var testconfig2 = []byte(`
[irc.freenode] [irc.freenode]
[mattermost.test] [mattermost.test]
[gitter.42wim] [gitter.42wim]
@ -80,8 +79,9 @@ var testconfig2 = `
[[gateway.out]] [[gateway.out]]
account = "discord.test" account = "discord.test"
channel = "general2" channel = "general2"
` `)
var testconfig3 = `
var testconfig3 = []byte(`
[irc.zzz] [irc.zzz]
[telegram.zzz] [telegram.zzz]
[slack.zzz] [slack.zzz]
@ -149,13 +149,10 @@ enable=true
[[gateway.inout]] [[gateway.inout]]
account="telegram.zzz" account="telegram.zzz"
channel="--333333333333" channel="--333333333333"
` `)
func maketestRouter(input string) *Router { func maketestRouter(input []byte) *Router {
var cfg *config.Config cfg := config.NewConfigFromString(input)
if _, err := toml.Decode(input, &cfg); err != nil {
fmt.Println(err)
}
r, err := NewRouter(cfg) r, err := NewRouter(cfg)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -163,14 +160,7 @@ func maketestRouter(input string) *Router {
return r return r
} }
func TestNewRouter(t *testing.T) { func TestNewRouter(t *testing.T) {
var cfg *config.Config r := maketestRouter(testconfig)
if _, err := toml.Decode(testconfig, &cfg); err != nil {
fmt.Println(err)
}
r, err := NewRouter(cfg)
if err != nil {
fmt.Println(err)
}
assert.Equal(t, 1, len(r.Gateways)) assert.Equal(t, 1, len(r.Gateways))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges)) assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels)) assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))