my-log/config/load_test.go

60 lines
1.2 KiB
Go
Raw Normal View History

package config
import (
"fmt"
"os"
//fp "path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoad(t *testing.T) {
f, _ := os.CreateTemp("", "test")
ConfigPath = f.Name()
2024-02-10 11:06:00 -06:00
defer f.Close()
fmt.Fprint(f, `[input]
ext = "log"`)
c, err := Load()
require.NoError(t, err)
assert.Equal(t, "log", c.Input.Ext)
}
2024-02-10 11:06:00 -06:00
func TestLoadBadFile(t *testing.T) {
f, _ := os.CreateTemp("", "test")
ConfigPath = f.Name()
defer f.Close()
fmt.Fprint(f, `{"not":"toml"}`)
_, err := Load()
assert.Error(t, err)
}
func TestLoadIgnoreMissingFile(t *testing.T) {
def, _ := DefaultConfig()
ConfigPath = "/not/a/real/file"
c, err := Load()
require.NoError(t, err)
assert.Equal(t, def, c)
}
2024-02-10 11:06:00 -06:00
func TestOverride(t *testing.T) {
Overrides = map[string]string{
"input.path": "/path/to/it",
"input.ext": "~",
}
c, err := Load()
require.NoError(t, err)
assert.Equal(t, Overrides["input.path"], c.Input.Path)
assert.Equal(t, "txt", c.Input.Ext)
}
func TestOverrideJson(t *testing.T) {
Overrides = map[string]string{"input.ext": `{"a":"b"}`}
c, err := Load()
require.NoError(t, err)
assert.Equal(t, "txt", c.Input.Ext)
}
// @todo test time