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

Commit 9ead247

Browse files
authored
Add unit tests for clog (#167)
1 parent 9477995 commit 9ead247

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: test
3939
uses: ./ci/image
4040
with:
41-
args: go test -v -cover -covermode=count ./internal/... ./cmd/...
41+
args: ./ci/steps/unit_test.sh
4242
gendocs:
4343
runs-on: ubuntu-latest
4444
steps:

ci/image/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ ENV CI=true
55

66
RUN go get golang.org/x/tools/cmd/goimports
77
RUN go get github.com/mattn/goveralls
8+
RUN apt update && apt install grep

ci/steps/unit_test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
cd "$(git rev-parse --show-toplevel)"
6+
7+
echo "--- go test..."
8+
9+
go test $(go list ./... | grep -v pkg/tcli | grep -v ci/integration)
File renamed without changes.

pkg/clog/clog_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package clog
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"testing"
8+
9+
"cdr.dev/slog/sloggers/slogtest/assert"
10+
"golang.org/x/xerrors"
11+
)
12+
13+
func TestError(t *testing.T) {
14+
t.Run("oneline", func(t *testing.T) {
15+
var mockErr error = Error("fake error")
16+
mockErr = xerrors.Errorf("wrap 1: %w", mockErr)
17+
mockErr = fmt.Errorf("wrap 2: %w", mockErr)
18+
19+
reader, writer, err := os.Pipe()
20+
assert.Success(t, "create pipe", err)
21+
22+
//! clearly not thread safe
23+
os.Stderr = writer
24+
25+
Log(mockErr)
26+
writer.Close()
27+
28+
output, err := ioutil.ReadAll(reader)
29+
assert.Success(t, "read all stderr output", err)
30+
31+
assert.Equal(t, "output is as expected", "error: fake error\n\n", string(output))
32+
})
33+
34+
t.Run("plain-error", func(t *testing.T) {
35+
mockErr := xerrors.Errorf("base error")
36+
mockErr = fmt.Errorf("wrap 1: %w", mockErr)
37+
38+
reader, writer, err := os.Pipe()
39+
assert.Success(t, "create pipe", err)
40+
41+
//! clearly not thread safe
42+
os.Stderr = writer
43+
44+
Log(mockErr)
45+
writer.Close()
46+
47+
output, err := ioutil.ReadAll(reader)
48+
assert.Success(t, "read all stderr output", err)
49+
50+
assert.Equal(t, "output is as expected", "fatal: wrap 1: base error\n\n", string(output))
51+
})
52+
53+
t.Run("multi-line", func(t *testing.T) {
54+
var mockErr error = Error("fake header", "next line", BlankLine, Tipf("content of fake tip"))
55+
mockErr = xerrors.Errorf("wrap 1: %w", mockErr)
56+
mockErr = fmt.Errorf("wrap 1: %w", mockErr)
57+
58+
reader, writer, err := os.Pipe()
59+
assert.Success(t, "create pipe", err)
60+
61+
//! clearly not thread safe
62+
os.Stderr = writer
63+
64+
Log(mockErr)
65+
writer.Close()
66+
67+
output, err := ioutil.ReadAll(reader)
68+
assert.Success(t, "read all stderr output", err)
69+
70+
assert.Equal(t,
71+
"output is as expected",
72+
"error: fake header\n | next line\n | \n | tip: content of fake tip\n\n",
73+
string(output),
74+
)
75+
})
76+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy