mirror of
https://github.com/livekit/livekit.git
synced 2026-10-22 15:22:30 +00:00
Refactor getConfigString (#133)
* Add test for getConfigString * Refactor getConfigString * Increase connect timeout in tests
This commit is contained in:
1 parent
fb91dce30e
commit
4915692f7c
3 files changed
+59
-14
No files matched your search
+11
-13
@@ -136,7 +136,7 @@ func main() {
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -208,17 +208,15 @@ func startServer(c *cli.Context) error {
|
||||
return server.Start()
|
||||
}
|
||||
|
||||
func getConfigString(c *cli.Context) (string, error) {
|
||||
configFile := c.String("config")
|
||||
configBody := c.String("config-body")
|
||||
if configBody == "" {
|
||||
if configFile != "" {
|
||||
content, err := ioutil.ReadFile(configFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
configBody = string(content)
|
||||
}
|
||||
func getConfigString(configFile string, inConfigBody string) (string, error) {
|
||||
if inConfigBody != "" || configFile == "" {
|
||||
return inConfigBody, nil
|
||||
}
|
||||
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 (
|
||||
SyncDelay = 100 * time.Millisecond
|
||||
ConnectTimeout = 5 * time.Second
|
||||
ConnectTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
func WithTimeout(t *testing.T, description string, f func() bool) bool {
|
||||
|
||||
Reference in new issue
Block a user