my-log/config/load_test.go

93 lines
1.9 KiB
Go
Raw Permalink Normal View History

package config
import (
"fmt"
"os"
"testing"
"github.com/nalgeon/be"
)
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()
be.Err(t, err, nil)
be.Equal(t, c.Input.Ext, "log")
}
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()
be.Err(t, err)
2024-02-10 11:06:00 -06:00
}
func TestLoadIgnoreMissingFile(t *testing.T) {
def, _ := DefaultConfig()
ConfigPath = "/not/a/real/file"
c, err := Load()
be.Err(t, err, nil)
be.Equal(t, c, def)
}
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()
be.Err(t, err, nil)
be.Equal(t, c.Input.Path, Overrides["input.path"])
be.Equal(t, c.Input.Ext, "txt")
2024-02-10 11:06:00 -06:00
}
func TestOverrideJson(t *testing.T) {
Overrides = map[string]string{"input.ext": `{"a":"b"}`}
c, err := Load()
be.Err(t, err, nil)
be.Equal(t, c.Input.Ext, "txt")
2024-02-10 11:06:00 -06:00
}
2024-03-07 21:50:51 -06:00
func TestTimeParse(t *testing.T) {
Overrides = map[string]string{"input.ext": "now"}
c, err := Load()
be.Err(t, err, "incompatible types: TOML value has type time.Time; destination has type string")
be.Equal(t, c.Input.Ext, "txt")
2024-03-07 21:50:51 -06:00
}
func TestStdoutMissing(t *testing.T) {
var oo Outputs = map[string]Output{}
std, en := oo.Stdout()
be.True(t, !en)
be.Equal(t, std, Stdout{})
}
func TestStdoutLoad(t *testing.T) {
os.Setenv("LOG_STDOUT_FORMAT", "json")
defer os.Unsetenv("LOG_STDOUT_FORMAT")
os.Setenv("LOG_STDOUT_ENABLED", "true")
defer os.Unsetenv("LOG_STDOUT_ENABLED")
c, _ := Load()
std, en := c.Outputs.Stdout()
be.True(t, en)
be.Equal(t, std.Format, "json")
2024-03-07 21:19:45 -06:00
}
func TestFormatJson(t *testing.T) {
ff := Formatters{
"json": map[string]any{"pretty_print": true},
}
js := ff.Json()
be.True(t, js.PrettyPrint)
2024-03-07 21:19:45 -06:00
ff = Formatters{}
js = ff.Json()
be.True(t, !js.PrettyPrint)
}