Skip to content

Commit

Permalink
fix(test): pass all tests using shampoo.Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Aug 8, 2024
1 parent 7671c0f commit 95e061c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 28 deletions.
6 changes: 4 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"context"
"io"
"os"
"testing"

"github.com/charmbracelet/shampoo"

Check failure on line 10 in context_test.go

View workflow job for this annotation

GitHub Actions / coverage / coverage

github.com/charmbracelet/[email protected]: invalid version: git ls-remote -q origin in /home/runner/go/pkg/mod/cache/vcs/3c0d5518b6014b97394ab619cdd475efa0c9f3e728db8238ff1cc3bb5fee91e7: exit status 128:

Check failure on line 10 in context_test.go

View workflow job for this annotation

GitHub Actions / coverage / coverage

github.com/charmbracelet/[email protected]: invalid version: git ls-remote -q origin in /home/runner/go/pkg/mod/cache/vcs/3c0d5518b6014b97394ab619cdd475efa0c9f3e728db8238ff1cc3bb5fee91e7: exit status 128:

Check failure on line 10 in context_test.go

View workflow job for this annotation

GitHub Actions / coverage / coverage

github.com/charmbracelet/[email protected]: invalid version: git ls-remote -q origin in /home/runner/go/pkg/mod/cache/vcs/3c0d5518b6014b97394ab619cdd475efa0c9f3e728db8238ff1cc3bb5fee91e7: exit status 128:
"github.com/stretchr/testify/require"
)

Expand All @@ -14,14 +16,14 @@ func TestLogContext_empty(t *testing.T) {
}

func TestLogContext_simple(t *testing.T) {
l := New(io.Discard)
l := New(shampoo.NewWriter(io.Discard, os.Environ()))
ctx := WithContext(context.Background(), l)
require.Equal(t, l, FromContext(ctx))
}

func TestLogContext_fields(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
l.SetLevel(DebugLevel)
ctx := WithContext(context.Background(), l.With("foo", "bar"))
l = FromContext(ctx)
Expand Down
12 changes: 7 additions & 5 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/charmbracelet/shampoo"
"github.com/stretchr/testify/require"
)

func TestJson(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
l.SetFormatter(JSONFormatter)
cases := []struct {
name string
Expand Down Expand Up @@ -125,7 +127,7 @@ func TestJson(t *testing.T) {

func TestJsonCaller(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
l.SetFormatter(JSONFormatter)
l.SetReportCaller(true)
l.SetLevel(DebugLevel)
Expand Down Expand Up @@ -167,7 +169,7 @@ func TestJsonCaller(t *testing.T) {

func TestJsonTime(t *testing.T) {
var buf bytes.Buffer
logger := New(&buf)
logger := New(shampoo.NewWriter(&buf, os.Environ()))
logger.SetTimeFunction(_zeroTime)
logger.SetFormatter(JSONFormatter)
logger.SetReportTimestamp(true)
Expand All @@ -177,7 +179,7 @@ func TestJsonTime(t *testing.T) {

func TestJsonPrefix(t *testing.T) {
var buf bytes.Buffer
logger := New(&buf)
logger := New(shampoo.NewWriter(&buf, os.Environ()))
logger.SetFormatter(JSONFormatter)
logger.SetPrefix("my-prefix")
logger.Info("info")
Expand All @@ -191,7 +193,7 @@ func TestJsonCustomKey(t *testing.T) {
TimestampKey = oldTsKey
}()
TimestampKey = "other-time"
logger := New(&buf)
logger := New(shampoo.NewWriter(&buf, os.Environ()))
logger.SetTimeFunction(_zeroTime)
logger.SetFormatter(JSONFormatter)
logger.SetReportTimestamp(true)
Expand Down
4 changes: 3 additions & 1 deletion logfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package log
import (
"bytes"
"errors"
"os"
"testing"

"github.com/charmbracelet/shampoo"
"github.com/stretchr/testify/assert"
)

func TestLogfmt(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
l.SetFormatter(LogfmtFormatter)
cases := []struct {
name string
Expand Down
8 changes: 5 additions & 3 deletions logger_121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
"bytes"
"context"
"log/slog"
"os"
"testing"
"time"

"github.com/charmbracelet/shampoo"
"github.com/stretchr/testify/assert"
)

func TestSlogSimple(t *testing.T) {
var buf bytes.Buffer
h := New(&buf)
h := New(shampoo.NewWriter(&buf, os.Environ()))
h.SetLevel(DebugLevel)
l := slog.New(h)
cases := []struct {
Expand Down Expand Up @@ -73,7 +75,7 @@ func TestSlogSimple(t *testing.T) {

func TestSlogWith(t *testing.T) {
var buf bytes.Buffer
h := New(&buf)
h := New(shampoo.NewWriter(&buf, os.Environ()))
h.SetLevel(DebugLevel)
l := slog.New(h).With("a", "b")
cases := []struct {
Expand Down Expand Up @@ -124,7 +126,7 @@ func TestSlogWith(t *testing.T) {

func TestSlogWithGroup(t *testing.T) {
var buf bytes.Buffer
h := New(&buf)
h := New(shampoo.NewWriter(&buf, os.Environ()))
l := slog.New(h).WithGroup("charm").WithGroup("bracelet")
cases := []struct {
name string
Expand Down
12 changes: 7 additions & 5 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"bytes"
"fmt"
"io"
"os"
"sync"
"testing"
"time"

"github.com/charmbracelet/shampoo"
"github.com/stretchr/testify/assert"
)

func TestSubLogger(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
cases := []struct {
name string
expected string
Expand Down Expand Up @@ -80,7 +82,7 @@ func TestWrongLevel(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
buf.Reset()
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
l.SetLevel(c.level)
l.Info("info")
assert.Equal(t, c.expected, buf.String())
Expand All @@ -90,7 +92,7 @@ func TestWrongLevel(t *testing.T) {

func TestLogFormatter(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
l.SetLevel(DebugLevel)
cases := []struct {
name string
Expand Down Expand Up @@ -139,7 +141,7 @@ func TestLogFormatter(t *testing.T) {

func TestEmptyMessage(t *testing.T) {
var buf bytes.Buffer
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
cases := []struct {
name string
expected string
Expand Down Expand Up @@ -196,7 +198,7 @@ func TestLogWithPrefix(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
buf.Reset()
l := New(&buf)
l := New(shampoo.NewWriter(&buf, os.Environ()))
l.SetPrefix(c.prefix)
l.Info(c.msg)
assert.Equal(t, c.expected, buf.String())
Expand Down
6 changes: 4 additions & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"fmt"
"io"
"os"
"testing"

"github.com/charmbracelet/shampoo"
"github.com/stretchr/testify/require"
)

Expand All @@ -15,7 +17,7 @@ func TestOptions(t *testing.T) {
ReportCaller: true,
Fields: []interface{}{"foo", "bar"},
}
logger := NewWithOptions(io.Discard, opts)
logger := NewWithOptions(shampoo.NewWriter(io.Discard, os.Environ()), opts)
require.Equal(t, ErrorLevel, logger.GetLevel())
require.True(t, logger.reportCaller)
require.False(t, logger.reportTimestamp)
Expand All @@ -27,7 +29,7 @@ func TestOptions(t *testing.T) {

func TestCallerFormatter(t *testing.T) {
var buf bytes.Buffer
l := NewWithOptions(&buf, Options{ReportCaller: true})
l := NewWithOptions(shampoo.NewWriter(&buf, os.Environ()), Options{ReportCaller: true})
frames := l.frames(0)
frame, _ := frames.Next()
file, line, fn := frame.File, frame.Line, frame.Function
Expand Down
18 changes: 9 additions & 9 deletions pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDefaultRace(t *testing.T) {

func TestGlobal(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetTimeFunction(_zeroTime)
cases := []struct {
name string
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestFatalf(t *testing.T) {

func TestDebugf(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(DebugLevel)
SetTimeFunction(_zeroTime)
SetReportTimestamp(true)
Expand All @@ -151,7 +151,7 @@ func TestDebugf(t *testing.T) {

func TestInfof(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(InfoLevel)
SetReportTimestamp(false)
SetReportCaller(false)
Expand All @@ -162,7 +162,7 @@ func TestInfof(t *testing.T) {

func TestWarnf(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(WarnLevel)
SetReportCaller(false)
SetReportTimestamp(true)
Expand All @@ -174,7 +174,7 @@ func TestWarnf(t *testing.T) {

func TestErrorf(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(ErrorLevel)
SetReportCaller(false)
SetReportTimestamp(true)
Expand All @@ -186,7 +186,7 @@ func TestErrorf(t *testing.T) {

func TestWith(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(InfoLevel)
SetReportCaller(false)
SetReportTimestamp(true)
Expand All @@ -203,7 +203,7 @@ func TestGetLevel(t *testing.T) {

func TestPrefix(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(WarnLevel)
SetReportCaller(false)
SetReportTimestamp(false)
Expand All @@ -216,7 +216,7 @@ func TestPrefix(t *testing.T) {

func TestFormatter(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(InfoLevel)
SetReportCaller(false)
SetReportTimestamp(false)
Expand All @@ -233,7 +233,7 @@ func TestWithPrefix(t *testing.T) {
func TestGlobalCustomLevel(t *testing.T) {
var buf bytes.Buffer
lvl := Level(-1)
SetOutput(&buf)
SetOutput(shampoo.NewWriter(&buf, os.Environ()))
SetLevel(lvl)
SetReportCaller(false)
SetReportTimestamp(false)
Expand Down
2 changes: 1 addition & 1 deletion stdlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestStdLog(t *testing.T) {
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
l.SetOutput(&buf)
l.SetOutput(shampoo.NewWriter(&buf, os.Environ()))
l.SetTimeFunction(_zeroTime)
c.f(l.StandardLog())
assert.Equal(t, c.expected, buf.String())
Expand Down

0 comments on commit 95e061c

Please sign in to comment.