Skip to content

Commit d1c789c

Browse files
authored
Merge pull request #8 from helmwave/tests
feat: add tests
2 parents 46997d6 + 24cca8c commit d1c789c

File tree

5 files changed

+188
-4
lines changed

5 files changed

+188
-4
lines changed

.github/workflows/go-tests.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Go Test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
paths:
8+
- "**.go"
9+
push:
10+
branches:
11+
- main
12+
paths:
13+
- "**.go"
14+
15+
jobs:
16+
tests:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v3
20+
with:
21+
fetch-depth: 0
22+
23+
- uses: actions/setup-go@v3
24+
with:
25+
go-version: 1.18
26+
27+
- uses: actions/cache@v3
28+
with:
29+
path: ~/go/pkg/mod
30+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
31+
restore-keys: |
32+
${{ runner.os }}-go-
33+
34+
- name: Install go modules
35+
run: go mod download
36+
37+
- name: Run tests
38+
run: go test -race -coverprofile=./tests.cov -v -covermode=atomic ./...
39+
40+
- uses: codecov/codecov-action@v3.1.0
41+
with:
42+
files: ./tests.cov
43+
fail_ci_if_error: true

codecov.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
codecov:
2+
require_ci_to_pass: true
3+
4+
coverage:
5+
precision: 2
6+
round: down
7+
range: "30...100"
8+
9+
status:
10+
project:
11+
default:
12+
informational: true # do not fail check
13+
only_pulls: true
14+
patch: false
15+
16+
parsers:
17+
gcov:
18+
branch_detection:
19+
conditional: yes
20+
loop: yes
21+
method: no
22+
macro: no
23+
24+
comment:
25+
layout: "reach,diff,flags,files,footer"
26+
behavior: default
27+
require_changes: false

func_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package formatter_test
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
"time"
9+
10+
formatter "github.com/helmwave/logrus-emoji-formatter"
11+
"github.com/sirupsen/logrus"
12+
"github.com/stretchr/testify/suite"
13+
)
14+
15+
type FormatterTestSuite struct {
16+
suite.Suite
17+
}
18+
19+
func (s *FormatterTestSuite) createLogger() (*logrus.Logger, *bytes.Buffer) {
20+
logger := logrus.New()
21+
var buf bytes.Buffer
22+
logger.SetFormatter(&formatter.Config{Color: false})
23+
logger.SetOutput(&buf)
24+
logger.SetLevel(logrus.TraceLevel)
25+
26+
return logger, &buf
27+
}
28+
29+
func (s *FormatterTestSuite) TestDefaultFormatting() {
30+
logger, buf := s.createLogger()
31+
32+
msg := "testblabla"
33+
34+
s.Run("trace", func() {
35+
expected := fmt.Sprintf("[🤮 aka TRACE]: %s\n", msg)
36+
logger.Trace(msg)
37+
defer buf.Reset()
38+
39+
s.Require().Equal(expected, buf.String())
40+
})
41+
s.Run("debug", func() {
42+
expected := fmt.Sprintf("[🤷 aka DEBUG]: %s\n", msg)
43+
logger.Debug(msg)
44+
defer buf.Reset()
45+
46+
s.Require().Equal(expected, buf.String())
47+
})
48+
s.Run("info", func() {
49+
expected := fmt.Sprintf("[🙃 aka INFO]: %s\n", msg)
50+
logger.Info(msg)
51+
defer buf.Reset()
52+
53+
s.Require().Equal(expected, buf.String())
54+
})
55+
s.Run("warn", func() {
56+
expected := fmt.Sprintf("[🙈 aka WARNING]: %s\n", msg)
57+
logger.Warn(msg)
58+
defer buf.Reset()
59+
60+
s.Require().Equal(expected, buf.String())
61+
})
62+
s.Run("error", func() {
63+
expected := fmt.Sprintf("[💩 aka ERROR]: %s\n", msg)
64+
logger.Error(msg)
65+
defer buf.Reset()
66+
67+
s.Require().Equal(expected, buf.String())
68+
})
69+
}
70+
71+
func (s *FormatterTestSuite) TestCustomFormat() {
72+
logger, buf := s.createLogger()
73+
74+
c, _ := logger.Formatter.(*formatter.Config)
75+
c.LogFormat = " "
76+
77+
msg := "testblabla"
78+
logger.Info(msg)
79+
80+
s.Require().Equal(" \n", buf.String())
81+
}
82+
83+
func (s *FormatterTestSuite) TestTimeFormat() {
84+
logger, buf := s.createLogger()
85+
86+
c, _ := logger.Formatter.(*formatter.Config)
87+
c.LogFormat = "%time%"
88+
89+
msg := "testblabla"
90+
expected := time.Now()
91+
logger.Info(msg)
92+
93+
t, err := time.Parse(time.RFC3339, strings.TrimSpace(buf.String()))
94+
s.Require().NoError(err)
95+
s.Require().WithinDuration(expected, t, time.Second)
96+
}
97+
98+
func TestFormatterTestSuite(t *testing.T) {
99+
t.Parallel()
100+
suite.Run(t, new(FormatterTestSuite))
101+
}

go.mod

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ module github.com/helmwave/logrus-emoji-formatter
22

33
go 1.18
44

5-
require github.com/sirupsen/logrus v1.9.0
5+
require (
6+
github.com/sirupsen/logrus v1.9.0
7+
github.com/stretchr/testify v1.8.0
8+
)
69

7-
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
14+
gopkg.in/yaml.v3 v3.0.1 // indirect
15+
)

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
66
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
77
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
88
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1010
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
13+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1114
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
1215
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1317
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1518
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
20+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)