Skip to content

Commit

Permalink
Merge pull request #8 from helmwave/tests
Browse files Browse the repository at this point in the history
feat: add tests
  • Loading branch information
r3nic1e authored Aug 4, 2022
2 parents 46997d6 + 24cca8c commit d1c789c
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 4 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Go Test

on:
pull_request:
branches:
- main
paths:
- "**.go"
push:
branches:
- main
paths:
- "**.go"

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-go@v3
with:
go-version: 1.18

- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install go modules
run: go mod download

- name: Run tests
run: go test -race -coverprofile=./tests.cov -v -covermode=atomic ./...

- uses: codecov/[email protected]
with:
files: ./tests.cov
fail_ci_if_error: true
27 changes: 27 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
codecov:
require_ci_to_pass: true

coverage:
precision: 2
round: down
range: "30...100"

status:
project:
default:
informational: true # do not fail check
only_pulls: true
patch: false

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no

comment:
layout: "reach,diff,flags,files,footer"
behavior: default
require_changes: false
101 changes: 101 additions & 0 deletions func_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package formatter_test

import (
"bytes"
"fmt"
"strings"
"testing"
"time"

formatter "github.com/helmwave/logrus-emoji-formatter"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
)

type FormatterTestSuite struct {
suite.Suite
}

func (s *FormatterTestSuite) createLogger() (*logrus.Logger, *bytes.Buffer) {
logger := logrus.New()
var buf bytes.Buffer
logger.SetFormatter(&formatter.Config{Color: false})
logger.SetOutput(&buf)
logger.SetLevel(logrus.TraceLevel)

return logger, &buf
}

func (s *FormatterTestSuite) TestDefaultFormatting() {
logger, buf := s.createLogger()

msg := "testblabla"

s.Run("trace", func() {
expected := fmt.Sprintf("[🤮 aka TRACE]: %s\n", msg)
logger.Trace(msg)
defer buf.Reset()

s.Require().Equal(expected, buf.String())
})
s.Run("debug", func() {
expected := fmt.Sprintf("[🤷 aka DEBUG]: %s\n", msg)
logger.Debug(msg)
defer buf.Reset()

s.Require().Equal(expected, buf.String())
})
s.Run("info", func() {
expected := fmt.Sprintf("[🙃 aka INFO]: %s\n", msg)
logger.Info(msg)
defer buf.Reset()

s.Require().Equal(expected, buf.String())
})
s.Run("warn", func() {
expected := fmt.Sprintf("[🙈 aka WARNING]: %s\n", msg)
logger.Warn(msg)
defer buf.Reset()

s.Require().Equal(expected, buf.String())
})
s.Run("error", func() {
expected := fmt.Sprintf("[💩 aka ERROR]: %s\n", msg)
logger.Error(msg)
defer buf.Reset()

s.Require().Equal(expected, buf.String())
})
}

func (s *FormatterTestSuite) TestCustomFormat() {
logger, buf := s.createLogger()

c, _ := logger.Formatter.(*formatter.Config)
c.LogFormat = " "

msg := "testblabla"
logger.Info(msg)

s.Require().Equal(" \n", buf.String())
}

func (s *FormatterTestSuite) TestTimeFormat() {
logger, buf := s.createLogger()

c, _ := logger.Formatter.(*formatter.Config)
c.LogFormat = "%time%"

msg := "testblabla"
expected := time.Now()
logger.Info(msg)

t, err := time.Parse(time.RFC3339, strings.TrimSpace(buf.String()))
s.Require().NoError(err)
s.Require().WithinDuration(expected, t, time.Second)
}

func TestFormatterTestSuite(t *testing.T) {
t.Parallel()
suite.Run(t, new(FormatterTestSuite))
}
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ module github.com/helmwave/logrus-emoji-formatter

go 1.18

require github.com/sirupsen/logrus v1.9.0
require (
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.0
)

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit d1c789c

Please sign in to comment.