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 all commits
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
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: ./ci/steps/unit_test.sh
gendocs:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions ci/image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ENV CI=true

RUN go get golang.org/x/tools/cmd/goimports
RUN go get github.com/mattn/goveralls
RUN apt update && apt install grep
9 changes: 9 additions & 0 deletions ci/steps/unit_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

echo "--- go test..."

go test $(go list ./... | grep -v pkg/tcli | grep -v ci/integration)
File renamed without changes.
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),
)
})
}