Skip to content

Commit

Permalink
combine multiple small test, minor lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 19, 2023
1 parent 1d72ca5 commit 15cccea
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: set up go 1.15
uses: actions/setup-go@v1
- name: set up go 1.19
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.19
id: go

- name: checkout
Expand All @@ -32,7 +32,7 @@ jobs:

- name: install golangci-lint and goveralls
run: |
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GITHUB_WORKSPACE v1.25.0
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GITHUB_WORKSPACE v1.50.1
GO111MODULE=off go get -u -v github.com/mattn/goveralls
- name: run linters
Expand Down
9 changes: 3 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,21 @@ linters-settings:
linters:
enable:
- megacheck
- golint
- revive
- govet
- unconvert
- megacheck
- structcheck
- gas
- gocyclo
- dupl
- misspell
- unparam
- varcheck
- deadcode
- unused
- typecheck
- ineffassign
- varcheck
- stylecheck
- gochecknoinits
- scopelint
- exportloopref
- gocritic
- nakedret
- gosimple
Expand Down
3 changes: 1 addition & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ func isStreamsSame(s1, s2 io.Writer) bool {
return false
}
return os.SameFile(outStat, errStat)
} else {
return s1 == s2
}
return s1 == s2
}
85 changes: 45 additions & 40 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoggerNoDbg(t *testing.T) {
Expand Down Expand Up @@ -53,22 +54,22 @@ func TestLoggerWithDbg(t *testing.T) {
rout, rerr string
}{
{"aaa", []interface{}{},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) aaa\n", ""},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) aaa\n", ""},
{"DEBUG something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
"2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"[DEBUG] something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
"2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"INFO something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"[INFO] something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"blah something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) blah something 123 aaa\n", ""},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) blah something 123 aaa\n", ""},
{"WARN something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 WARN (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
"2018/01/07 13:02:34.123 WARN (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"ERROR something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 ERROR (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) something 123 aaa\n",
"2018/01/07 13:02:34.123 ERROR (lgr/logger_test.go:82 lgr.TestLoggerWithDbg.func2) something 123 aaa\n"},
"2018/01/07 13:02:34.123 ERROR (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n",
"2018/01/07 13:02:34.123 ERROR (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n"},
}

rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestLoggerWithDbg(t *testing.T) {
rout.Reset()
rerr.Reset()
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.000 DEBUG (lgr/logger_test.go:100) something 123 err\n", rout.String())
assert.Equal(t, "2018/01/07 13:02:34.000 DEBUG (lgr/logger_test.go:101) something 123 err\n", rout.String())

f := `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFunc}}) {{.Message}}`
l = New(Debug, Out(rout), Err(rerr), Format(f)) // caller func only
Expand Down Expand Up @@ -127,15 +128,15 @@ func TestLoggerWithCallerDepth(t *testing.T) {
}
f(l1)

assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:128 lgr.TestLoggerWithCallerDepth) something 123 err\n",
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:129 lgr.TestLoggerWithCallerDepth) something 123 err\n",
rout.String())

rout.Reset()
rerr.Reset()
l2 := New(Debug, Out(rout), Err(rerr), Format(FullDebug), CallerDepth(0))
l2.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
f(l2)
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:126 lgr.TestLoggerWithCallerDepth."+
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:127 lgr.TestLoggerWithCallerDepth."+
"func2) something 123 err\n", rout.String())
}

Expand Down Expand Up @@ -364,7 +365,7 @@ func TestLoggerErrorWithDump(t *testing.T) {
assert.Equal(t, "2018/01/07 13:02:34.000 ERROR (lgr.TestLoggerErrorWithDump) oh my, error now! bad thing happened", lines[0])
assert.Equal(t, ">>> stack trace:", lines[1])
assert.Contains(t, lines[2], "github.com/go-pkgz/lgr.TestLoggerErrorWithDump(")
assert.Contains(t, lines[3], "lgr/logger_test.go:362")
assert.Contains(t, lines[3], "lgr/logger_test.go:363")
}

func TestLoggerWithErrorSameOutputs(t *testing.T) {
Expand Down Expand Up @@ -517,33 +518,37 @@ func TestLoggerHidden(t *testing.T) {
assert.Equal(t, "2018/01/07 13:02:34 INFO something ****** 123 ****** xyz\n", rout.String(), "secrets secrets")
}

func TestIsStreamsSameWithStd(t *testing.T) {
sout, serr := os.Stdout, os.Stderr
assert.True(t, isStreamsSame(sout, serr))
}

func TestIsStreamsSameWithSameFile(t *testing.T) {
dir := t.TempDir()
f, _ := os.CreateTemp(dir, "test")
assert.True(t, isStreamsSame(f, f))
}

func TestIsStreamsSameWithDiffFiles(t *testing.T) {
dir := t.TempDir()
f1, _ := os.CreateTemp(dir, "test")
f2, _ := os.CreateTemp(dir, "test")
assert.False(t, isStreamsSame(f1, f2))
}

func TestIsStreamsSameWithSameBuffer(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
assert.True(t, isStreamsSame(buf, buf))
}

func TestIsStreamsSameWithDiffBuffer(t *testing.T) {
buf1, buf2 := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
sout, serr := buf1, buf2
assert.False(t, isStreamsSame(sout, serr))
func TestIsStreamsSame(t *testing.T) {
{ // with stdout and stderr
sout, serr := os.Stdout, os.Stderr
assert.True(t, isStreamsSame(sout, serr))
}
{ // with same file
dir := t.TempDir()
f, err := os.CreateTemp(dir, "test")
require.NoError(t, err)
defer os.Remove(f.Name())
assert.True(t, isStreamsSame(f, f))
}
{ // with different files
dir := t.TempDir()
f1, err := os.CreateTemp(dir, "test")
require.NoError(t, err)
defer os.Remove(f1.Name())
f2, err := os.CreateTemp(dir, "test")
require.NoError(t, err)
defer os.Remove(f2.Name())
assert.False(t, isStreamsSame(f1, f2))
}
{ // with same buffer
buf := bytes.NewBuffer([]byte{})
assert.True(t, isStreamsSame(buf, buf))
}
{ // with different buffers
buf1, buf2 := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
sout, serr := buf1, buf2
assert.False(t, isStreamsSame(sout, serr))
}
}

func BenchmarkNoDbgNoFormat(b *testing.B) {
Expand Down

0 comments on commit 15cccea

Please sign in to comment.