[#180] node: Refactor panics in unit test

* Replace panics in unit tests by require.NoError and t.Fatalf

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2023-03-28 17:16:03 +03:00
parent 91717d4b98
commit 221203beeb
30 changed files with 76 additions and 79 deletions

View file

@ -32,6 +32,6 @@ func TestApiclientSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -56,6 +56,6 @@ func TestContractsSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -32,6 +32,6 @@ func TestControlSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -167,6 +167,6 @@ func TestEngineSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -49,6 +49,6 @@ func TestGRPCSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -25,6 +25,6 @@ func TestLoggerSection_Level(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -34,6 +34,6 @@ func TestMetricsSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -40,6 +40,6 @@ func TestMorphSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -162,6 +162,6 @@ func TestNodeSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -29,6 +29,6 @@ func TestObjectSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -26,6 +26,6 @@ func TestPolicerSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -34,6 +34,6 @@ func TestProfilerSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -28,6 +28,6 @@ func TestReplicatorSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}

View file

@ -4,8 +4,10 @@ import (
"bufio"
"os"
"strings"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config"
"github.com/stretchr/testify/require"
)
func fromFile(path string) *config.Config {
@ -18,10 +20,10 @@ func fromFile(path string) *config.Config {
)
}
func fromEnvFile(path string) *config.Config {
func fromEnvFile(t testing.TB, path string) *config.Config {
var p config.Prm
loadEnv(path) // github.com/joho/godotenv can do that as well
loadEnv(t, path) // github.com/joho/godotenv can do that as well
return config.New(p)
}
@ -43,8 +45,8 @@ func ForEachFileType(pref string, f func(*config.Config)) {
}
// ForEnvFileType creates config from `<pref>.env` file.
func ForEnvFileType(pref string, f func(*config.Config)) {
f(fromEnvFile(pref + ".env"))
func ForEnvFileType(t testing.TB, pref string, f func(*config.Config)) {
f(fromEnvFile(t, pref+".env"))
}
// EmptyConfig returns config without any values and sections.
@ -55,11 +57,9 @@ func EmptyConfig() *config.Config {
}
// loadEnv reads .env file, parses `X=Y` records and sets OS ENVs.
func loadEnv(path string) {
func loadEnv(t testing.TB, path string) {
f, err := os.Open(path)
if err != nil {
panic("can't open .env file")
}
require.NoError(t, err, "can't open .env file")
defer f.Close()
@ -73,8 +73,6 @@ func loadEnv(path string) {
v = strings.Trim(v, `"`)
err = os.Setenv(k, v)
if err != nil {
panic("can't set environment variable")
}
require.NoError(t, err, "can't set environment variable")
}
}

View file

@ -39,6 +39,6 @@ func TestTreeSection(t *testing.T) {
configtest.ForEachFileType(path, fileConfigTest)
t.Run("ENV", func(t *testing.T) {
configtest.ForEnvFileType(path, fileConfigTest)
configtest.ForEnvFileType(t, path, fileConfigTest)
})
}