- 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_*)
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package formatters
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"codeberg.org/danjones000/my-log/config"
|
|
"codeberg.org/danjones000/my-log/models"
|
|
"github.com/nalgeon/be"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var empty []byte
|
|
|
|
func setupNullTestContext(t *testing.T) context.Context {
|
|
t.Helper()
|
|
v := viper.New()
|
|
v.SetConfigType("toml")
|
|
return config.AddToContext(t.Context(), v)
|
|
}
|
|
|
|
func TestNullName(t *testing.T) {
|
|
ctx := setupNullTestContext(t)
|
|
f, err := New(ctx, "zero")
|
|
be.Err(t, err, nil)
|
|
be.Equal(t, f.Name(), "zero")
|
|
}
|
|
|
|
func TestNullMeta(t *testing.T) {
|
|
ctx := setupNullTestContext(t)
|
|
f, _ := New(ctx, "zero")
|
|
o, err := f.Meta(models.Meta{Key: "foo", Value: 42})
|
|
be.Err(t, err, nil)
|
|
be.Equal(t, o, empty)
|
|
}
|
|
|
|
func TestNullEntry(t *testing.T) {
|
|
ctx := setupNullTestContext(t)
|
|
f, _ := New(ctx, "zero")
|
|
o, err := f.Entry(models.Entry{Title: "title", Date: time.Now()})
|
|
be.Err(t, err, nil)
|
|
be.Equal(t, o, empty)
|
|
}
|
|
|
|
func TestNullLog(t *testing.T) {
|
|
ctx := setupNullTestContext(t)
|
|
f, _ := New(ctx, "zero")
|
|
o, err := f.Log(models.Log{Name: "jim", Entries: []models.Entry{{Title: "title", Date: time.Now()}}})
|
|
be.Err(t, err, nil)
|
|
be.Equal(t, o, empty)
|
|
}
|
|
|
|
func TestNullLogs(t *testing.T) {
|
|
ctx := setupNullTestContext(t)
|
|
f, _ := New(ctx, "zero")
|
|
o, err := f.Logs([]models.Log{{Name: "jim", Entries: []models.Entry{{Title: "title", Date: time.Now()}}}})
|
|
be.Err(t, err, nil)
|
|
be.Equal(t, o, empty)
|
|
}
|