Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Add unit tests for clog #167

Merged
merged 4 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add unit tests for clog
  • Loading branch information
cmoog committed Nov 2, 2020
commit 6e7513f32c9c12428757fb0a50282f41620845fd
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: test
uses: ./ci/image
with:
args: go test -v -cover -covermode=count ./internal/... ./cmd/...
args: go test $(go list ./... | grep -v pkg/tcli | grep -v ci/integration)
gendocs:
runs-on: ubuntu-latest
steps:
Expand Down
76 changes: 76 additions & 0 deletions pkg/clog/clog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package clog

import (
"fmt"
"io/ioutil"
"os"
"testing"

"cdr.dev/slog/sloggers/slogtest/assert"
"golang.org/x/xerrors"
)

func TestError(t *testing.T) {
t.Run("oneline", func(t *testing.T) {
var mockErr error = Error("fake error")
mockErr = xerrors.Errorf("wrap 1: %w", mockErr)
mockErr = fmt.Errorf("wrap 2: %w", mockErr)

reader, writer, err := os.Pipe()
assert.Success(t, "create pipe", err)

//! clearly not thread safe
os.Stderr = writer

Log(mockErr)
writer.Close()

output, err := ioutil.ReadAll(reader)
assert.Success(t, "read all stderr output", err)

assert.Equal(t, "output is as expected", "error: fake error\n\n", string(output))
})

t.Run("plain-error", func(t *testing.T) {
mockErr := xerrors.Errorf("base error")
mockErr = fmt.Errorf("wrap 1: %w", mockErr)

reader, writer, err := os.Pipe()
assert.Success(t, "create pipe", err)

//! clearly not thread safe
os.Stderr = writer

Log(mockErr)
writer.Close()

output, err := ioutil.ReadAll(reader)
assert.Success(t, "read all stderr output", err)

assert.Equal(t, "output is as expected", "fatal: wrap 1: base error\n\n", string(output))
})

t.Run("multi-line", func(t *testing.T) {
var mockErr error = Error("fake header", "next line", BlankLine, Tipf("content of fake tip"))
mockErr = xerrors.Errorf("wrap 1: %w", mockErr)
mockErr = fmt.Errorf("wrap 1: %w", mockErr)

reader, writer, err := os.Pipe()
assert.Success(t, "create pipe", err)

//! clearly not thread safe
os.Stderr = writer

Log(mockErr)
writer.Close()

output, err := ioutil.ReadAll(reader)
assert.Success(t, "read all stderr output", err)

assert.Equal(t,
"output is as expected",
"error: fake header\n | next line\n | \n | tip: content of fake tip\n\n",
string(output),
)
})
}