|
| 1 | +local log = require("structlog") |
| 2 | +local RotatingFile = log.sinks.RotatingFile |
| 3 | + |
| 4 | +local stub = require("luassert.stub") |
| 5 | +local match = require("luassert.match") |
| 6 | + |
| 7 | +describe("RotatingFile", function() |
| 8 | + local fp = {} |
| 9 | + stub(fp, "write") |
| 10 | + stub(fp, "close") |
| 11 | + |
| 12 | + local iolib = { |
| 13 | + open = function() |
| 14 | + return fp |
| 15 | + end, |
| 16 | + } |
| 17 | + |
| 18 | + local stat = { |
| 19 | + atime = { |
| 20 | + nsec = 862184517, |
| 21 | + sec = 1629806034, |
| 22 | + }, |
| 23 | + birthtime = { |
| 24 | + nsec = 862184517, |
| 25 | + sec = 1629806034, |
| 26 | + }, |
| 27 | + blksize = 4096, |
| 28 | + blocks = 8, |
| 29 | + ctime = { |
| 30 | + nsec = 862184517, |
| 31 | + sec = 1629806034, |
| 32 | + }, |
| 33 | + dev = 2049, |
| 34 | + flags = 0, |
| 35 | + gen = 0, |
| 36 | + gid = 1000, |
| 37 | + ino = 11698217, |
| 38 | + mode = 33188, |
| 39 | + mtime = { |
| 40 | + nsec = 862184517, |
| 41 | + sec = 1629806034, |
| 42 | + }, |
| 43 | + nlink = 1, |
| 44 | + rdev = 0, |
| 45 | + size = 5, |
| 46 | + type = "file", |
| 47 | + uid = 1000, |
| 48 | + } |
| 49 | + |
| 50 | + local uv = { |
| 51 | + fs_stat = function(_) |
| 52 | + return stat |
| 53 | + end, |
| 54 | + } |
| 55 | + stub(uv, "fs_rename") |
| 56 | + |
| 57 | + local file_path = "./path.log" |
| 58 | + |
| 59 | + it("should use the io lib to write to the file sink", function() |
| 60 | + local file = RotatingFile(file_path, { iolib = iolib }) |
| 61 | + file:write("test") |
| 62 | + |
| 63 | + assert.stub(fp.write).was_called_with(match.is_ref(fp), "test") |
| 64 | + assert.stub(fp.write).was_called_with(match.is_ref(fp), "\n") |
| 65 | + assert.stub(fp.close).called() |
| 66 | + end) |
| 67 | + it("should not rotate the file if stat returns nil", function() |
| 68 | + local uv_cpy = { |
| 69 | + fs_stat = function(_) |
| 70 | + return nil |
| 71 | + end, |
| 72 | + } |
| 73 | + stub(uv_cpy, "fs_rename") |
| 74 | + local file = RotatingFile(file_path, { max_size = 0, iolib = iolib, uv = uv_cpy }) |
| 75 | + file:write("test") |
| 76 | + |
| 77 | + assert.stub(uv_cpy.fs_rename).was_not_called() |
| 78 | + end) |
| 79 | + it("should not rotate the file if max_size is not exceeded", function() |
| 80 | + local file = RotatingFile(file_path, { max_size = math.huge, iolib = iolib, uv = uv }) |
| 81 | + file:write("test") |
| 82 | + |
| 83 | + assert.stub(uv.fs_rename).was_not_called() |
| 84 | + end) |
| 85 | + it("should not rotate the file if max_age is not exceeded", function() |
| 86 | + local file = RotatingFile(file_path, { max_age = math.huge, iolib = iolib, uv = uv }) |
| 87 | + file:write("test") |
| 88 | + |
| 89 | + assert.stub(uv.fs_rename).was_not_called() |
| 90 | + end) |
| 91 | + it("should rotate the file if max_size is exceeded", function() |
| 92 | + local file = RotatingFile(file_path, { max_size = 0, iolib = iolib, uv = uv }) |
| 93 | + file:write("test") |
| 94 | + |
| 95 | + assert.stub(uv.fs_rename).was_called() |
| 96 | + end) |
| 97 | + it("should rotate the file if max_age is exceeded", function() |
| 98 | + local file = RotatingFile(file_path, { max_age = 0, iolib = iolib, uv = uv }) |
| 99 | + file:write("test") |
| 100 | + |
| 101 | + assert.stub(uv.fs_rename).was_called() |
| 102 | + end) |
| 103 | +end) |
0 commit comments