808a1e9deb
Took way longer than it should had because I was researching exisiting options. As it turns out, nothing nice exists that would handle: - Multiple overridiable config files - Defaults provided in a struct - Output in a struct License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDecodeNothing(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
{
|
|
cfg, err := FromFile(os.DevNull)
|
|
assert.Nil(err, "error should be nil")
|
|
assert.Equal(Default(), cfg,
|
|
"config from empty file should be the same as default")
|
|
}
|
|
|
|
{
|
|
cfg, err := FromFile("./does-not-exist.toml")
|
|
assert.Nil(err, "error should be nil")
|
|
assert.Equal(Default(), cfg,
|
|
"config from not exisiting file should be the same as default")
|
|
}
|
|
}
|
|
|
|
func TestParitalConfig(t *testing.T) {
|
|
assert := assert.New(t)
|
|
cfgString := `
|
|
[API]
|
|
Timeout = "10s"
|
|
`
|
|
expected := Default()
|
|
expected.API.Timeout = Duration(10 * time.Second)
|
|
|
|
{
|
|
cfg, err := FromReader(bytes.NewReader([]byte(cfgString)))
|
|
assert.NoError(err, "error should be nil")
|
|
assert.Equal(expected, cfg,
|
|
"config from reader should contain changes")
|
|
}
|
|
|
|
{
|
|
f, err := ioutil.TempFile("", "config-*.toml")
|
|
fname := f.Name()
|
|
|
|
assert.NoError(err, "tmp file shold not error")
|
|
_, err = f.WriteString(cfgString)
|
|
assert.NoError(err, "writing to tmp file should not error")
|
|
err = f.Close()
|
|
assert.NoError(err, "closing tmp file should not error")
|
|
defer os.Remove(fname) //nolint:errcheck
|
|
|
|
cfg, err := FromFile(fname)
|
|
assert.Nil(err, "error should be nil")
|
|
assert.Equal(expected, cfg,
|
|
"config from reader should contain changes")
|
|
}
|
|
}
|