[#493] node/config: Export useful functions into a separate test package

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-21 16:46:15 +03:00 committed by Leonard Lyubich
parent c645781b98
commit b8a5f09174
4 changed files with 39 additions and 29 deletions

View file

@ -4,11 +4,12 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestConfigCommon(t *testing.T) { func TestConfigCommon(t *testing.T) {
forEachFileType("test/config", func(c *config.Config) { configtest.ForEachFileType("test/config", func(c *config.Config) {
val := c.Value("value") val := c.Value("value")
require.NotNil(t, val) require.NotNil(t, val)

View file

@ -4,11 +4,12 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestStringSlice(t *testing.T) { func TestStringSlice(t *testing.T) {
forEachFileType("test/config", func(c *config.Config) { configtest.ForEachFileType("test/config", func(c *config.Config) {
cStringSlice := c.Sub("string_slice") cStringSlice := c.Sub("string_slice")
val := config.StringSlice(cStringSlice, "empty") val := config.StringSlice(cStringSlice, "empty")
@ -30,7 +31,7 @@ func TestStringSlice(t *testing.T) {
} }
func TestString(t *testing.T) { func TestString(t *testing.T) {
forEachFileType("test/config", func(c *config.Config) { configtest.ForEachFileType("test/config", func(c *config.Config) {
c = c.Sub("string") c = c.Sub("string")
val := config.String(c, "correct") val := config.String(c, "correct")

View file

@ -1,26 +0,0 @@
package config_test
import (
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
)
func fromFile(path string) *config.Config {
var p config.Prm
return config.New(p,
config.WithConfigFile(path),
)
}
func forEachFile(paths []string, f func(*config.Config)) {
for i := range paths {
f(fromFile(paths[i]))
}
}
func forEachFileType(pref string, f func(*config.Config)) {
forEachFile([]string{
pref + ".yaml",
pref + ".json",
}, f)
}

View file

@ -0,0 +1,34 @@
package configtest
import "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
func fromFile(path string) *config.Config {
var p config.Prm
return config.New(p,
config.WithConfigFile(path),
)
}
func forEachFile(paths []string, f func(*config.Config)) {
for i := range paths {
f(fromFile(paths[i]))
}
}
// ForEachFileType passes configs read from next files:
// - `<pref>.yaml`;
// - `<pref>.json`.
func ForEachFileType(pref string, f func(*config.Config)) {
forEachFile([]string{
pref + ".yaml",
pref + ".json",
}, f)
}
// EmptyConfig returns config without any values and sections.
func EmptyConfig() *config.Config {
var p config.Prm
return config.New(p)
}