Skip to content

Commit 0efdf4a

Browse files
committed
Add basic tests (#2)
1 parent 9c41f8b commit 0efdf4a

10 files changed

+201
-10
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/.idea
2+
/test

Diff for: .travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go:
77
- 1.7.x
88
- master
99

10-
# TODO: uncomment test commands
11-
#script:
12-
# - go get -v github.com/smartystreets/goconvey
13-
# - go test -v -cover -race
10+
script:
11+
- go get -v github.com/smartystreets/goconvey
12+
- make test
13+
- make vet

Diff for: Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.PHONY: build test vet
2+
3+
build: vet
4+
go install -v
5+
6+
test:
7+
mkdir -p test
8+
go test -v -cover -race -coverprofile=test/coverage.out
9+
10+
vet:
11+
go vet
12+
13+
coverage:
14+
go tool cover -html=test/coverage.out

Diff for: clog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
const (
27-
_VERSION = "0.2.2"
27+
_VERSION = "0.3.0"
2828
)
2929

3030
// Version returns current version of the package.

Diff for: clog_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2017 Unknwon
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package clog
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/smartystreets/goconvey/convey"
21+
)
22+
23+
func Test_Version(t *testing.T) {
24+
Convey("Get version", t, func() {
25+
So(Version(), ShouldEqual, _VERSION)
26+
})
27+
}
28+
29+
func Test_isValidLevel(t *testing.T) {
30+
Convey("Validate log level", t, func() {
31+
So(isValidLevel(LEVEL(-1)), ShouldBeFalse)
32+
So(isValidLevel(LEVEL(5)), ShouldBeFalse)
33+
So(isValidLevel(TRACE), ShouldBeTrue)
34+
So(isValidLevel(FATAL), ShouldBeTrue)
35+
})
36+
}

Diff for: console_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2017 Unknwon
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package clog
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/smartystreets/goconvey/convey"
21+
)
22+
23+
func Test_console_Init(t *testing.T) {
24+
Convey("Init console logger", t, func() {
25+
Convey("Mismatched config object", func() {
26+
err := NewLogger(CONSOLE, struct{}{})
27+
So(err, ShouldNotBeNil)
28+
_, ok := err.(ErrConfigObject)
29+
So(ok, ShouldBeTrue)
30+
})
31+
32+
Convey("Valid config object", func() {
33+
So(NewLogger(CONSOLE, ConsoleConfig{}), ShouldBeNil)
34+
35+
Convey("Incorrect level", func() {
36+
err := NewLogger(CONSOLE, ConsoleConfig{
37+
Level: LEVEL(-1),
38+
})
39+
So(err, ShouldNotBeNil)
40+
_, ok := err.(ErrInvalidLevel)
41+
So(ok, ShouldBeTrue)
42+
})
43+
})
44+
})
45+
}

Diff for: file.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
LAST_WRITE_FILENAME = ".clog-lastwrite"
3131
LAST_WRITE_FORMAT = "2006-01-02 MST"
3232
LAST_WRITE_FORMAT_SIMPLE = "2006-01-02"
33+
LOG_PREFIX_LENGTH = len("2017/02/06 21:20:08 ")
3334
)
3435

3536
// FileRotationConfig represents rotation related configurations for file mode logger.
@@ -39,7 +40,7 @@ type FileRotationConfig struct {
3940
Rotate bool
4041
// Rotate on daily basis.
4142
Daily bool
42-
// Maximum size of file for a rotation.
43+
// Maximum size in bytes of file for a rotation.
4344
MaxSize int64
4445
// Maximum number of lines for a rotation.
4546
MaxLines int64
@@ -222,8 +223,8 @@ func (f *file) write(msg *Message) {
222223
f.Logger.Print(msg.Body)
223224

224225
if f.rotate.Rotate {
225-
f.currentSize += 20 + int64(len(msg.Body)) // 20 for "2017/02/06 21:20:08 "
226-
f.currentLines++ // TODO: should I care if log message itself contains new lines?
226+
f.currentSize += int64(LOG_PREFIX_LENGTH + len(msg.Body))
227+
f.currentLines++ // TODO: should I care if log message itself contains new lines?
227228

228229
var (
229230
needsRotate = false
@@ -243,7 +244,6 @@ func (f *file) write(msg *Message) {
243244

244245
if needsRotate {
245246
f.file.Close()
246-
247247
if err := os.Rename(f.filename, f.rotateFilename(rotateDate.Format(LAST_WRITE_FORMAT_SIMPLE))); err != nil {
248248
f.errorChan <- fmt.Errorf("fail to rename rotate file '%s': %v", f.filename, err)
249249
}

Diff for: file_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2017 Unknwon
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package clog
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/smartystreets/goconvey/convey"
21+
)
22+
23+
func Test_file_Init(t *testing.T) {
24+
Convey("Init file logger", t, func() {
25+
Convey("With mismatched config object", func() {
26+
err := NewLogger(FILE, struct{}{})
27+
So(err, ShouldNotBeNil)
28+
_, ok := err.(ErrConfigObject)
29+
So(ok, ShouldBeTrue)
30+
})
31+
32+
Convey("With valid config object", func() {
33+
So(NewLogger(FILE, FileConfig{
34+
Filename: "test/test.log",
35+
}), ShouldBeNil)
36+
37+
Convey("Incorrect level", func() {
38+
err := NewLogger(FILE, FileConfig{
39+
Level: LEVEL(-1),
40+
})
41+
So(err, ShouldNotBeNil)
42+
_, ok := err.(ErrInvalidLevel)
43+
So(ok, ShouldBeTrue)
44+
})
45+
46+
Convey("Empty file name", func() {
47+
err := NewLogger(FILE, FileConfig{})
48+
So(err, ShouldNotBeNil)
49+
So(err.Error(), ShouldEqual, "initFile: OpenFile '': open : no such file or directory")
50+
})
51+
})
52+
})
53+
}

Diff for: logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func NewLogger(mode MODE, cfg interface{}) error {
9191

9292
logger := factory()
9393
if err := logger.Init(cfg); err != nil {
94-
return fmt.Errorf("fail to initialize: %v", err)
94+
return err
9595
}
9696
msgChan := logger.ExchangeChans(errorChan)
9797

Diff for: logger_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2017 Unknwon
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package clog
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/smartystreets/goconvey/convey"
21+
)
22+
23+
func Test_Register(t *testing.T) {
24+
Convey("Register with nil function", t, func() {
25+
defer func() {
26+
err := recover()
27+
So(err, ShouldNotBeNil)
28+
So(err, ShouldEqual, "clog: register function is nil")
29+
}()
30+
Register("test", nil)
31+
})
32+
33+
Convey("Register duplicated mode", t, func() {
34+
defer func() {
35+
err := recover()
36+
So(err, ShouldNotBeNil)
37+
So(err, ShouldEqual, "clog: register duplicated mode 'test'")
38+
}()
39+
Register("test", newConsole)
40+
Register("test", newConsole)
41+
})
42+
}

0 commit comments

Comments
 (0)