Skip to content

Commit

Permalink
Fix tests and make megacheck happy
Browse files Browse the repository at this point in the history
  • Loading branch information
42wim committed Mar 4, 2018
1 parent f2f1d87 commit 9802744
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
6 changes: 1 addition & 5 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map
return nil
}

func (b *Bridge) ReloadConfig() {
return
}

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("general." + key)
Expand Down
18 changes: 18 additions & 0 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bytes"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
Expand Down Expand Up @@ -192,6 +193,23 @@ func NewConfig(cfgfile string) *Config {
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 {
c.RLock()
defer c.RUnlock()
Expand Down
9 changes: 0 additions & 9 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/hashicorp/golang-lru"
"github.com/peterhellberg/emojilib"
"net/http"
"reflect"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -450,14 +449,6 @@ func getChannelID(msg config.Message) string {
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 {
return strings.HasPrefix(account, "api.")
}
30 changes: 10 additions & 20 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package gateway
import (
"fmt"
"github.com/42wim/matterbridge/bridge/config"
"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
"strconv"

"testing"
)

var testconfig = `
var testconfig = []byte(`
[irc.freenode]
[mattermost.test]
[gitter.42wim]
Expand All @@ -37,9 +36,9 @@ var testconfig = `
[[gateway.inout]]
account="slack.test"
channel="testing"
`
`)

var testconfig2 = `
var testconfig2 = []byte(`
[irc.freenode]
[mattermost.test]
[gitter.42wim]
Expand Down Expand Up @@ -80,8 +79,9 @@ var testconfig2 = `
[[gateway.out]]
account = "discord.test"
channel = "general2"
`
var testconfig3 = `
`)

var testconfig3 = []byte(`
[irc.zzz]
[telegram.zzz]
[slack.zzz]
Expand Down Expand Up @@ -149,28 +149,18 @@ enable=true
[[gateway.inout]]
account="telegram.zzz"
channel="--333333333333"
`
`)

func maketestRouter(input string) *Router {
var cfg *config.Config
if _, err := toml.Decode(input, &cfg); err != nil {
fmt.Println(err)
}
func maketestRouter(input []byte) *Router {
cfg := config.NewConfigFromString(input)
r, err := NewRouter(cfg)
if err != nil {
fmt.Println(err)
}
return r
}
func TestNewRouter(t *testing.T) {
var cfg *config.Config
if _, err := toml.Decode(testconfig, &cfg); err != nil {
fmt.Println(err)
}
r, err := NewRouter(cfg)
if err != nil {
fmt.Println(err)
}
r := maketestRouter(testconfig)
assert.Equal(t, 1, len(r.Gateways))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))
Expand Down

0 comments on commit 9802744

Please sign in to comment.