my-log/formatters/new_test.go
Dan Jones 9f05f933dd ♲ Refactor configuration to use viper with context propagation
- Replace global ConfigPath and Overrides with viper-based configuration
- Add viper.New() to create configurable viper instances
- Store viper and unmarshaled Config struct in context for testability
- Add RetrieveFromContext and AddToContext helper functions
- Update files.Append to accept context and retrieve config from it
- Update formatters.Preferred and formatters.New to accept context
- Add PersistentPreRunE in CLI to create and configure viper instance
- Support -c flag for custom config file path
- Support -v flag for config value overrides
- Update all test files to create viper and add to context
- Remove unused config types and load functions
- Add viper as dependency with automatic env var support (MYLOG_*)
2026-03-09 13:46:01 -05:00

41 lines
827 B
Go

package formatters
import (
"context"
"slices"
"testing"
"codeberg.org/danjones000/my-log/config"
"github.com/nalgeon/be"
"github.com/spf13/viper"
)
func TestKinds(t *testing.T) {
kinds := Kinds()
for _, kind := range []string{"plain", "json", "zero"} {
be.True(t, slices.Contains(kinds, kind))
}
}
func setupNewTest(t *testing.T) context.Context {
t.Helper()
v := viper.New()
v.SetConfigType("toml")
v.Set("output.stdout.config.format", "plain")
v.Set("formatters.json.pretty_print", false)
return config.AddToContext(t.Context(), v)
}
func TestNewUnsupported(t *testing.T) {
ctx := setupNewTest(t)
f, err := New(ctx, "nope")
be.Equal(t, f, nil)
be.Err(t, err)
}
func TestPreferred(t *testing.T) {
ctx := setupNewTest(t)
form, err := Preferred(ctx)
be.Err(t, err, nil)
be.True(t, form != nil)
}