my-log/config/load_test.go
Dan Jones 1110288d84 Migrate from testify to nalgeon/be testing library
- Replace all testify/assert and testify/require with be library
- Update go.mod to use be v0.3.0 instead of testify
- Simplify test assertions using be.Equal, be.Err, and be.True
- Refactor append_test, entry_test, meta_test, log_test, and formatter tests
2026-02-13 16:02:38 -06:00

93 lines
1.9 KiB
Go

package config
import (
"fmt"
"os"
"testing"
"github.com/nalgeon/be"
)
func TestLoad(t *testing.T) {
f, _ := os.CreateTemp("", "test")
ConfigPath = f.Name()
defer f.Close()
fmt.Fprint(f, `[input]
ext = "log"`)
c, err := Load()
be.Err(t, err, nil)
be.Equal(t, c.Input.Ext, "log")
}
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)
}
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)
}
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")
}
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")
}
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")
}
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")
}
func TestFormatJson(t *testing.T) {
ff := Formatters{
"json": map[string]any{"pretty_print": true},
}
js := ff.Json()
be.True(t, js.PrettyPrint)
ff = Formatters{}
js = ff.Json()
be.True(t, !js.PrettyPrint)
}