File: testFile.lua

package info (click to toggle)
lua-logging 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 512 kB
  • sloc: makefile: 16; sh: 3
file content (81 lines) | stat: -rw-r--r-- 2,322 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local GLOBAL_OS_DATE = os.date
local GLOBAL_IO_OPEN = io.open

local buffer_mode do
  local dir_separator = _G.package.config:sub(1,1)
  local is_windows = dir_separator == '\\'
  if is_windows then
    -- Windows does not support "line" buffered mode, see
    -- https://github.com/lunarmodules/lualogging/pull/9
    buffer_mode = "no"
  else
    buffer_mode = "line"
  end
end

local mock = {
  date = nil,
  handle = {}
}

io.open = function (file, mode)  --luacheck: ignore
  if (not string.find(file, "^__TEST*")) then
    return GLOBAL_IO_OPEN(file, mode)
  end

  mock.handle[file] = {}
  mock.handle[file].lines = {}
  mock.handle[file].mode = mode
  return {
    setvbuf = function (_, s)
      mock.handle[file].setvbuf = s
    end,
    write = function (_, s)
      table.insert(mock.handle[file].lines, s)
    end,
  }
end

os.date = function (...)  --luacheck: ignore
  return mock.date
end

local log_file = require "logging.file"

mock.date = "2008-01-01"
local logger = log_file("__TEST%s.log", "%Y-%m-%d")

assert(mock.handle["__TEST"..mock.date..".log"] == nil)

logger:info("logging.file test")

assert(mock.handle["__TEST"..mock.date..".log"].mode == "a")
assert(#mock.handle["__TEST"..mock.date..".log"].lines == 1)
assert(mock.handle["__TEST"..mock.date..".log"].setvbuf == buffer_mode)
assert(mock.handle["__TEST"..mock.date..".log"].lines[1] == "2008-01-01 INFO logging.file test\n")

mock.date = "2008-01-02"

logger:debug("debugging...")
logger:error("error!")

assert(mock.handle["__TEST"..mock.date..".log"].mode == "a")
assert(#mock.handle["__TEST"..mock.date..".log"].lines == 2)
assert(mock.handle["__TEST"..mock.date..".log"].setvbuf == buffer_mode)
assert(mock.handle["__TEST"..mock.date..".log"].lines[1] == "2008-01-02 DEBUG debugging...\n")
assert(mock.handle["__TEST"..mock.date..".log"].lines[2] == "2008-01-02 ERROR error!\n")

mock.date = "2008-01-03"

logger:info({id = "1"})

assert(mock.handle["__TEST"..mock.date..".log"].mode == "a")
assert(#mock.handle["__TEST"..mock.date..".log"].lines == 1)
assert(mock.handle["__TEST"..mock.date..".log"].setvbuf == buffer_mode)
assert(mock.handle["__TEST"..mock.date..".log"].lines[1] == '2008-01-03 INFO {id = "1"}\n')

os.date = GLOBAL_OS_DATE  --luacheck: ignore
io.open = GLOBAL_IO_OPEN  --luacheck: ignore

print("File Logging OK")