file_writer_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package logger
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestFileWrite(t *testing.T) {
  11. fileName := "test_file"
  12. fileLog := fileName + ".log"
  13. testData := []byte(string("hello Dalton, how are you doing?"))
  14. defer func() {
  15. os.Remove(fileLog)
  16. }()
  17. w := NewFileRollingWriter("", fileName, 1000, 2)
  18. defer w.Close()
  19. l, err := w.Write(testData)
  20. assert.NoError(t, err)
  21. assert.Equal(t, l, len(testData), "expected write length and data length to match")
  22. d, err := ioutil.ReadFile(fileLog)
  23. assert.FileExists(t, fileLog, "file doesn't exist at expected path")
  24. assert.Equal(t, d, testData, "expected data in file to match test data")
  25. }
  26. func TestRolling(t *testing.T) {
  27. dirName := "testdir"
  28. err := os.Mkdir(dirName, 0755)
  29. assert.NoError(t, err)
  30. fileName := "test_file"
  31. firstFile := filepath.Join(dirName, fileName+".log")
  32. secondFile := filepath.Join(dirName, fileName+"-1.log")
  33. thirdFile := filepath.Join(dirName, fileName+"-2.log")
  34. defer func() {
  35. os.RemoveAll(dirName)
  36. os.Remove(firstFile)
  37. os.Remove(secondFile)
  38. os.Remove(thirdFile)
  39. }()
  40. w := NewFileRollingWriter(dirName, fileName, 1000, 2)
  41. defer w.Close()
  42. for i := 99; i >= 1; i-- {
  43. testData := []byte(fmt.Sprintf("%d bottles of beer on the wall...", i))
  44. w.Write(testData)
  45. }
  46. assert.FileExists(t, firstFile, "first file doesn't exist as expected")
  47. assert.FileExists(t, secondFile, "second file doesn't exist as expected")
  48. assert.FileExists(t, thirdFile, "third file doesn't exist as expected")
  49. assert.False(t, exists(filepath.Join(dirName, fileName+"-3.log")), "limited to two files and there is more")
  50. }
  51. func TestSingleFile(t *testing.T) {
  52. fileName := "test_file"
  53. testData := []byte(string("hello Dalton, how are you doing?"))
  54. defer func() {
  55. os.Remove(fileName)
  56. }()
  57. w := NewFileRollingWriter(fileName, fileName, 1000, 2)
  58. defer w.Close()
  59. l, err := w.Write(testData)
  60. assert.NoError(t, err)
  61. assert.Equal(t, l, len(testData), "expected write length and data length to match")
  62. d, err := ioutil.ReadFile(fileName)
  63. assert.FileExists(t, fileName, "file doesn't exist at expected path")
  64. assert.Equal(t, d, testData, "expected data in file to match test data")
  65. }
  66. func TestSingleFileInDirectory(t *testing.T) {
  67. dirName := "testdir"
  68. err := os.Mkdir(dirName, 0755)
  69. assert.NoError(t, err)
  70. fileName := "test_file"
  71. fullPath := filepath.Join(dirName, fileName+".log")
  72. testData := []byte(string("hello Dalton, how are you doing?"))
  73. defer func() {
  74. os.Remove(fullPath)
  75. os.RemoveAll(dirName)
  76. }()
  77. w := NewFileRollingWriter(fullPath, fileName, 1000, 2)
  78. defer w.Close()
  79. l, err := w.Write(testData)
  80. assert.NoError(t, err)
  81. assert.Equal(t, l, len(testData), "expected write length and data length to match")
  82. d, err := ioutil.ReadFile(fullPath)
  83. assert.FileExists(t, fullPath, "file doesn't exist at expected path")
  84. assert.Equal(t, d, testData, "expected data in file to match test data")
  85. }