59 lines
1.1 KiB
Go
59 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)
|
||
|
}
|
||
|
}
|