Refactor getConfigString (#133)
* Add test for getConfigString * Refactor getConfigString * Increase connect timeout in tests
This commit is contained in:
+11
-13
@@ -136,7 +136,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfig(c *cli.Context) (*config.Config, error) {
|
func getConfig(c *cli.Context) (*config.Config, error) {
|
||||||
confString, err := getConfigString(c)
|
confString, err := getConfigString(c.String("config"), c.String("config-body"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -208,17 +208,15 @@ func startServer(c *cli.Context) error {
|
|||||||
return server.Start()
|
return server.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfigString(c *cli.Context) (string, error) {
|
func getConfigString(configFile string, inConfigBody string) (string, error) {
|
||||||
configFile := c.String("config")
|
if inConfigBody != "" || configFile == "" {
|
||||||
configBody := c.String("config-body")
|
return inConfigBody, nil
|
||||||
if configBody == "" {
|
|
||||||
if configFile != "" {
|
|
||||||
content, err := ioutil.ReadFile(configFile)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
configBody = string(content)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return configBody, nil
|
|
||||||
|
outConfigBody, err := ioutil.ReadFile(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(outConfigBody), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testStruct struct {
|
||||||
|
configFileName string
|
||||||
|
configBody string
|
||||||
|
|
||||||
|
expectedError error
|
||||||
|
expectedConfigBody string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetConfigString(t *testing.T) {
|
||||||
|
tests := []testStruct{
|
||||||
|
{"", "", nil, ""},
|
||||||
|
{"", "configBody", nil, "configBody"},
|
||||||
|
{"file", "configBody", nil, "configBody"},
|
||||||
|
{"file", "", nil, "fileContent"},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
writeConfigFile(test, t)
|
||||||
|
defer os.Remove(test.configFileName)
|
||||||
|
|
||||||
|
configBody, err := getConfigString(test.configFileName, test.configBody)
|
||||||
|
require.Equal(t, test.expectedError, err)
|
||||||
|
require.Equal(t, test.expectedConfigBody, configBody)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShouldReturnErrorIfConfigFileDoesNotExist(t *testing.T) {
|
||||||
|
configBody, err := getConfigString("notExistingFile", "")
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Empty(t, configBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeConfigFile(test testStruct, t *testing.T) {
|
||||||
|
if test.configFileName != "" {
|
||||||
|
d1 := []byte(test.expectedConfigBody)
|
||||||
|
err := os.WriteFile(test.configFileName, d1, 0644)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
SyncDelay = 100 * time.Millisecond
|
SyncDelay = 100 * time.Millisecond
|
||||||
ConnectTimeout = 5 * time.Second
|
ConnectTimeout = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func WithTimeout(t *testing.T, description string, f func() bool) bool {
|
func WithTimeout(t *testing.T, description string, f func() bool) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user