- 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_*)
99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
package formatters
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"codeberg.org/danjones000/my-log/config"
|
|
"codeberg.org/danjones000/my-log/internal/testutil/bep"
|
|
"codeberg.org/danjones000/my-log/models"
|
|
"github.com/nalgeon/be"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func setupJsonTestContext(t *testing.T) context.Context {
|
|
t.Helper()
|
|
v := viper.New()
|
|
v.SetConfigType("toml")
|
|
v.Set("formatters.json.pretty_print", false)
|
|
return config.AddToContext(t.Context(), v)
|
|
}
|
|
|
|
func TestJsonName(t *testing.T) {
|
|
ctx := setupJsonTestContext(t)
|
|
f, _ := New(ctx, "json")
|
|
be.Equal(t, f.Name(), "json")
|
|
}
|
|
|
|
func TestJsonMeta(t *testing.T) {
|
|
ctx := setupJsonTestContext(t)
|
|
f, _ := New(ctx, "json")
|
|
m := models.Meta{Key: "foo", Value: 42}
|
|
exp := `{"foo":42}`
|
|
o, err := f.Meta(m)
|
|
be.Err(t, err, nil)
|
|
bep.JSON(t, o, []byte(exp))
|
|
}
|
|
|
|
func TestJsonEntry(t *testing.T) {
|
|
ctx := setupJsonTestContext(t)
|
|
when := time.Now()
|
|
f, _ := New(ctx, "json")
|
|
m := models.Meta{Key: "foo", Value: 42}
|
|
e := models.Entry{
|
|
Title: "Homer",
|
|
Date: when,
|
|
Fields: []models.Meta{m},
|
|
}
|
|
exp := fmt.Sprintf(`{"title":"%s","date":"%s","foo":42}`, e.Title, when.Format(time.RFC3339))
|
|
o, err := f.Entry(e)
|
|
be.Err(t, err, nil)
|
|
bep.JSON(t, o, []byte(exp))
|
|
}
|
|
|
|
func TestJsonLog(t *testing.T) {
|
|
ctx := setupJsonTestContext(t)
|
|
when := time.Now()
|
|
f, _ := New(ctx, "json")
|
|
m := models.Meta{Key: "foo", Value: 42}
|
|
e := models.Entry{
|
|
Title: "Homer",
|
|
Date: when,
|
|
Fields: []models.Meta{m},
|
|
}
|
|
l := models.Log{Name: "stuff", Entries: []models.Entry{e}}
|
|
exp := fmt.Sprintf(`{"%s":[{"title":"%s","date":"%s","foo":42}]}`, l.Name, e.Title, when.Format(time.RFC3339))
|
|
o, err := f.Log(l)
|
|
be.Err(t, err, nil)
|
|
bep.JSON(t, o, []byte(exp))
|
|
}
|
|
|
|
func TestJsonNoLogs(t *testing.T) {
|
|
ctx := setupJsonTestContext(t)
|
|
f, _ := New(ctx, "json")
|
|
o, err := f.Logs([]models.Log{})
|
|
var exp []byte
|
|
be.Err(t, err, nil)
|
|
be.Equal(t, o, exp)
|
|
}
|
|
|
|
func TestJsonErr(t *testing.T) {
|
|
ctx := setupJsonTestContext(t)
|
|
f, _ := New(ctx, "json")
|
|
o, err := f.Meta(models.Meta{Key: "foo", Value: make(chan bool)})
|
|
var exp []byte
|
|
be.Err(t, err)
|
|
be.Equal(t, o, exp)
|
|
}
|
|
|
|
func TestJsonPretty(t *testing.T) {
|
|
f := Json{true}
|
|
o, err := f.Meta(models.Meta{Key: "foo", Value: 42})
|
|
exp := `{
|
|
"foo": 42
|
|
}`
|
|
be.Err(t, err, nil)
|
|
be.Equal(t, string(o), exp)
|
|
}
|