Skip to content

Commit

Permalink
Use same way to load config as in service (#1908)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Oct 6, 2020
1 parent 31b70a7 commit c8aac9e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
14 changes: 2 additions & 12 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package config
import (
"os"
"path"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -560,19 +559,10 @@ func TestLoadEmptyConfig(t *testing.T) {
}

func loadConfigFile(t *testing.T, fileName string, factories component.Factories) (*configmodels.Config, error) {
// Open the file for reading.
file, err := os.Open(filepath.Clean(fileName))
require.NoErrorf(t, err, "unable to open the file %v", fileName)
require.NotNil(t, file)

defer func() {
require.NoErrorf(t, file.Close(), "unable to close the file %v", fileName)
}()

// Read yaml config from file
v := NewViper()
v.SetConfigType("yaml")
require.NoErrorf(t, v.ReadConfig(file), "unable to read the file %v", fileName)
v.SetConfigFile(fileName)
require.NoErrorf(t, v.ReadInConfig(), "unable to read the file %v", fileName)

// Load the config from viper using the given factories.
cfg, err := Load(v, factories)
Expand Down
15 changes: 2 additions & 13 deletions config/configtest/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package configtest

import (
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
Expand All @@ -32,19 +30,10 @@ import (
// and can then be used to unmarshal the file contents to objects.
// Example usage for testing can be found in configtest_test.go
func NewViperFromYamlFile(t *testing.T, fileName string) *viper.Viper {
// Open the file for reading.
file, err := os.Open(filepath.Clean(fileName))
require.NoErrorf(t, err, "unable to open the file %v", fileName)
require.NotNil(t, file)

defer func() {
require.NoErrorf(t, file.Close(), "unable to close the file %v", fileName)
}()

// Read yaml config from file
v := config.NewViper()
v.SetConfigType("yaml")
require.NoErrorf(t, v.ReadConfig(file), "unable to read the file %v", fileName)
v.SetConfigFile(fileName)
require.NoErrorf(t, v.ReadInConfig(), "unable to read the file %v", fileName)

return v
}
Expand Down

0 comments on commit c8aac9e

Please sign in to comment.