Aleksey Savchuk
5fac4058e8
All checks were successful
DCO action / DCO (pull_request) Successful in 51s
Tests and linters / Run gofumpt (pull_request) Successful in 1m41s
Vulncheck / Vulncheck (pull_request) Successful in 1m54s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m13s
Build / Build Components (pull_request) Successful in 2m21s
Tests and linters / gopls check (pull_request) Successful in 2m31s
Tests and linters / Staticcheck (pull_request) Successful in 2m34s
Tests and linters / Lint (pull_request) Successful in 3m30s
Tests and linters / Tests (pull_request) Successful in 3m35s
Tests and linters / Tests with -race (pull_request) Successful in 3m37s
Add tests for `CreateViper` and `ReloadViper` to ensure that no extra files, except *.yaml, *.yml, *.json, are loaded from config directory. Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package configtest
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type MarshalFunc = func(any) ([]byte, error)
|
|
|
|
type ConfigFile struct {
|
|
filename string
|
|
content map[string]any
|
|
marshal func(any) ([]byte, error)
|
|
}
|
|
|
|
type DummyFile struct {
|
|
filename string
|
|
size int
|
|
}
|
|
|
|
func NewConfigFile(filename string, content map[string]any, marshal MarshalFunc) ConfigFile {
|
|
return ConfigFile{
|
|
filename: filename,
|
|
content: content,
|
|
marshal: marshal,
|
|
}
|
|
}
|
|
|
|
func NewDummyFile(filename string, size int) DummyFile {
|
|
return DummyFile{
|
|
filename: filename,
|
|
size: size,
|
|
}
|
|
}
|
|
|
|
func PrepareConfigFiles(t *testing.T, dir string, files []ConfigFile) {
|
|
for _, file := range files {
|
|
data, err := file.marshal(file.content)
|
|
require.NoError(t, err)
|
|
|
|
err = os.WriteFile(path.Join(dir, file.filename), data, 0o600)
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
func PrepareDummyFiles(t *testing.T, dir string, files []DummyFile) {
|
|
for _, file := range files {
|
|
data := make([]byte, file.size)
|
|
_, _ = rand.Read(data)
|
|
|
|
err := os.WriteFile(path.Join(dir, file.filename), data, 0o600)
|
|
require.NoError(t, err)
|
|
}
|
|
}
|