file_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. package clog
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "testing"
  19. . "github.com/smartystreets/goconvey/convey"
  20. )
  21. func Test_file_Init(t *testing.T) {
  22. Convey("Init file logger", t, func() {
  23. Convey("With mismatched config object", func() {
  24. err := New(FILE, struct{}{})
  25. So(err, ShouldNotBeNil)
  26. _, ok := err.(ErrConfigObject)
  27. So(ok, ShouldBeTrue)
  28. })
  29. Convey("With valid config object", func() {
  30. So(New(FILE, FileConfig{
  31. Filename: "test/test.log",
  32. }), ShouldBeNil)
  33. Convey("Incorrect level", func() {
  34. err := New(FILE, FileConfig{
  35. Level: LEVEL(-1),
  36. })
  37. So(err, ShouldNotBeNil)
  38. _, ok := err.(ErrInvalidLevel)
  39. So(ok, ShouldBeTrue)
  40. })
  41. Convey("Empty file name", func() {
  42. err := New(FILE, FileConfig{})
  43. So(err, ShouldNotBeNil)
  44. So(err.Error(), ShouldEqual, "initFile: OpenFile '': open : no such file or directory")
  45. })
  46. })
  47. })
  48. }
  49. func Test_file_rotateFilename(t *testing.T) {
  50. Convey("Get rotate filename", t, func() {
  51. f := &file{
  52. filename: "test/test.log",
  53. }
  54. os.Remove("test/test.log.2017-03-05")
  55. So(f.rotateFilename("2017-03-05"), ShouldEqual, "test/test.log.2017-03-05")
  56. // Pretend one log file already exists
  57. ioutil.WriteFile("test/test.log.2017-03-05", []byte(""), os.ModePerm)
  58. So(f.rotateFilename("2017-03-05"), ShouldEqual, "test/test.log.2017-03-05.001")
  59. os.Remove("test/test.log.2017-03-05")
  60. })
  61. }