Remove unused test files for lualogging

This commit is contained in:
Théophile Diot 2025-01-16 10:29:40 +01:00
parent bddb5f54fe
commit a4e550bf16
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
10 changed files with 0 additions and 594 deletions

View file

@ -1,249 +0,0 @@
local logging = require "logging"
local call_count
local last_msg
local msgs
function logging.test(params)
local logPatterns = logging.buildLogPatterns(params.logPatterns, params.logPattern)
local timestampPattern = params.timestampPattern or logging.defaultTimestampPattern()
return logging.new( function(self, level, message)
last_msg = logging.prepareLogMsg(logPatterns[level], os.date(timestampPattern), level, message)
msgs = msgs or {}
table.insert(msgs, last_msg)
--print("----->",last_msg)
call_count = call_count + 1
return true
end)
end
local function reset()
call_count = 0
last_msg = nil
msgs = nil
end
local tests = {}
tests.constants_for_lualogging = function()
local DEFAULT_LEVELS = { "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" }
for _, level in ipairs(DEFAULT_LEVELS) do
assert(logging[level], "constant logging."..level.." is not defined")
end
end
tests.deprecated_parameter_handling = function()
local list = { "param1", "next_one", "hello_world" }
local params = logging.getDeprecatedParams(list, {
param1 = 1,
next_one = nil,
hello_world = 3,
})
assert(params.param1 == 1)
assert(params.next_one == nil)
assert(params.hello_world == 3)
params = logging.getDeprecatedParams(list, 1, nil, 3)
assert(params.param1 == 1)
assert(params.next_one == nil)
assert(params.hello_world == 3)
end
tests.buildLogPatterns = function()
local check_patterns = function(t)
assert(type(t) == "table")
assert(t.DEBUG and t.INFO and t.WARN and t.ERROR and t.FATAL)
local i = 0
for k,v in pairs(t) do i = i + 1 end
assert(i == 5)
end
local t = logging.buildLogPatterns()
check_patterns(t)
local t = logging.buildLogPatterns(nil, "hello")
check_patterns(t)
assert(t.DEBUG == "hello")
assert(t.FATAL == "hello")
local t = logging.buildLogPatterns({}, "hello")
check_patterns(t)
assert(t.DEBUG == "hello")
assert(t.FATAL == "hello")
local t = logging.buildLogPatterns({
DEBUG = "bye"
}, "hello")
check_patterns(t)
assert(t.DEBUG == "bye")
assert(t.INFO == "hello")
assert(t.FATAL == "hello")
end
tests.log_levels = function()
local logger = logging.test { logPattern = "%message", timestampPattern = nil }
logger:setLevel(logger.DEBUG)
-- debug gets logged
logger:debug("message 1")
assert(last_msg == "message 1", "got: " .. tostring(last_msg))
assert(call_count == 1, "Got: " .. tostring(call_count))
-- fatal also gets logged at 'debug' setting
logger:fatal("message 2")
assert(last_msg == "message 2", "got: " .. tostring(last_msg))
assert(call_count == 2, "Got: " .. tostring(call_count))
logger:setLevel(logger.FATAL)
-- debug gets logged
logger:debug("message 3") -- should not change the last message
assert(last_msg == "message 2", "got: " .. tostring(last_msg))
assert(call_count == 2, "Got: " .. tostring(call_count))
-- fatal also gets logged at 'debug' setting
logger:fatal("message 4") -- should be logged as 3rd message
assert(last_msg == "message 4", "got: " .. tostring(last_msg))
assert(call_count == 3, "Got: " .. tostring(call_count))
logger:setLevel(logger.OFF)
-- debug gets logged
logger:debug("message 5") -- should not change the last message
assert(last_msg == "message 4", "got: " .. tostring(last_msg))
assert(call_count == 3, "Got: " .. tostring(call_count))
-- fatal also gets logged at 'debug' setting
logger:fatal("message 6") -- should not change the last message
assert(last_msg == "message 4", "got: " .. tostring(last_msg))
assert(call_count == 3, "Got: " .. tostring(call_count))
-- should never log "OFF", even if its set
logger:fatal("message 7") -- should not change the last message
assert(last_msg == "message 4", "got: " .. tostring(last_msg))
assert(call_count == 3, "Got: " .. tostring(call_count))
end
tests.logPatterns = function()
local logger = logging.test { logPattern = "%date", timestampPattern = nil }
logger:debug("hello")
assert(last_msg ~= "%date", "expected '%date' placeholder to be replaced, got: " .. tostring(last_msg))
assert(last_msg ~= "", "expected '%date' placeholder to be replaced by a value, got an empty string")
local logger = logging.test { logPattern = "%level", timestampPattern = nil }
logger:fatal("hello")
assert(last_msg ~= "%level", "expected '%level' placeholder to be replaced, got: " .. tostring(last_msg))
assert(last_msg == "FATAL", "expected '%level' placeholder to be replaced by 'FATAL', got: " .. tostring(last_msg))
local logger = logging.test { logPattern = "%message", timestampPattern = nil }
logger:debug("hello")
assert(last_msg ~= "%message", "expected '%message' placeholder to be replaced, got: " .. tostring(last_msg))
assert(last_msg == "hello", "expected '%message' placeholder to be replaced by 'hello', got: " .. tostring(last_msg))
local logger = logging.test { logPattern = "%source", timestampPattern = nil }
local function test_func()
logger:debug("hello")
end
test_func()
assert(last_msg ~= "%source", "expected '%source' placeholder to be replaced, got: " .. tostring(last_msg))
if debug then
assert(last_msg:find("'test_func'", 1, true), "expected function name in output, got: " .. tostring(last_msg))
assert(last_msg:find(":138 ", 1, true), "expected line number in output, got: " .. tostring(last_msg)) -- update hardcoded linenumber when this fails!
assert(last_msg:find("generic.lua:", 1, true), "expected filename in output, got: " .. tostring(last_msg))
else
-- debug library disabled
assert(last_msg:find("'unknown function'", 1, true), "expected 'unknwon function' in output, got: " .. tostring(last_msg))
assert(last_msg:find(":-1 ", 1, true), "expected line number (-1) in output, got: " .. tostring(last_msg)) -- update hardcoded linenumber when this fails!
assert(last_msg:find("?:", 1, true), "expected filename ('?') in output, got: " .. tostring(last_msg))
end
-- mutiple separate patterns
local logger = logging.test {
logPattern = "%message",
logPatterns = {
[logging.DEBUG] = "hello %message"
},
timestampPattern = nil,
}
logger:debug("world")
assert(last_msg == "hello world", "expected 'hello world', got: " .. tostring(last_msg))
logger:error("world")
assert(last_msg == "world", "expected 'world', got: " .. tostring(last_msg))
end
tests.table_serialization = function()
local logger = logging.test { logPattern = "%message", timestampPattern = nil }
logger:debug({1,2,3,4,5,6,7,8,9,10})
assert(last_msg == "{1, 10, 2, 3, 4, 5, 6, 7, 8, 9}", "got: " .. tostring(last_msg))
logger:debug({abc = "cde", "hello", "world", xyz = true, 1, 2, 3})
assert(last_msg == '{"hello", "world", 1, 2, 3, abc = "cde", xyz = true}', "got: " .. tostring(last_msg))
end
tests.print_function = function()
local logger = logging.test { logPattern = "%level %message" }
local print = logger:getPrint(logger.DEBUG)
print("hey", "there", "dude")
assert(msgs[1] == "DEBUG hey there dude")
print()
assert(msgs[2] == "DEBUG ")
print("hello\nthere")
assert(msgs[3] == "DEBUG hello")
assert(msgs[4] == "DEBUG there")
print({}, true, nil, 0)
assert(msgs[5]:find("table"))
assert(msgs[5]:find(" true nil 0$"))
end
tests.format_error_stacktrace = function()
local count = 0
local logger = logging.test { logPattern = "%level %message" }
logger:debug("%s-%s", 'abc', '007')
assert(last_msg == 'DEBUG abc-007')
logger:debug("%s=%s", nil)
assert(last_msg:find("bad argument #%d to '(.-)'"), "msg:'"..last_msg.."'")
if debug then
assert(last_msg:find("in main chunk"), "msg:'"..last_msg.."'")
assert(last_msg:find("in %w+ 'func'"), "msg:'"..last_msg.."'")
local _, levels = last_msg:gsub("(|)", function() count = count + 1 end)
assert(levels == 3, "got : " .. tostring(levels))
end
end
tests.defaultLogger = function()
-- returns a logger
assert(logging.defaultLogger(), "expected a logger object to be returned)")
local logger = logging.test {}
-- setting a default one
assert(logging.defaultLogger(logger), "expected a logger object to be returned)")
assert(logger == logging.defaultLogger(), "expected my previously set logger to be returned)")
end
tests.defaultLevel = function()
-- default level is 'debug'
local old_level = logging.defaultLevel()
assert(old_level == logging.DEBUG, "expected default to be 'debug'")
-- setting level
assert(logging.defaultLevel(logging.FATAL) == logging.FATAL, "expected updated log-level")
-- new logger uses new default level
local logger = logging.test {}
logger:error("less than 'fatal', should not be logged")
assert(call_count == 0)
logger:fatal("should be logged")
assert(call_count == 1)
-- errors on unknown level
assert(not pcall(logging.defaultLevel, "unknown level"), "expected an error to be thrown")
-- restore old default
logging.defaultLevel(old_level)
end
for name, func in pairs(tests) do
reset()
print("generic test: " .. name)
func()
end
print("[v] all generic tests succesful")

View file

@ -1,8 +0,0 @@
#!/bin/sh
set -e
LUA_PATH="../src/?.lua;$LUA_PATH" lua test.lua
rm -f test.db test.log*

View file

@ -1,18 +0,0 @@
local test = {
"generic.lua",
"testEnv.lua",
"testConsole.lua",
"testFile.lua",
"testMail.lua",
"testSocket.lua",
"testSQL.lua",
"testRollingFile.lua",
}
print ("Start of Logging tests")
for _, filename in ipairs(test) do
dofile(filename)
end
print ("End of Logging tests")

View file

@ -1,12 +0,0 @@
local log_console = require"logging.console"
local logger = log_console()
logger:info("logging.console test")
logger:debug("debugging...")
logger:error("error!")
logger:debug("string with %4")
logger:setLevel("INFO") -- test log level change warning.
print("Console Logging OK")

View file

@ -1,145 +0,0 @@
local logging
local logenv
local env
local old_getenv = os.getenv
function os.getenv(key) -- luacheck: ignore
assert(type(key) == "string", "expected env variable name to be a string")
return env[key]
end
local function reset()
env = {}
package.loaded["logging"] = nil
package.loaded["logging.envconfig"] = nil
logging = require "logging"
logenv = require "logging.envconfig"
end
local tests = {}
tests.returns_defaults = function()
local name, opts = logenv.get_default_settings()
-- print("title: ", require("pl.pretty").write(opts))
assert(name == "console", "expected 'console' to be the default logger")
assert(type(opts) == "table", "expected 'opts' to be a table")
assert(opts.destination == "stderr", "expected 'destination' to be 'stderr'")
assert(opts.logLevel == "DEBUG", "expected 'logLevel' to be 'DEBUG'")
assert(opts.logPatterns.DEBUG == "%date %level %message\n")
assert(opts.logPatterns.INFO == "%date %level %message\n")
assert(opts.logPatterns.WARN == "%date %level %message\n")
assert(opts.logPatterns.ERROR == "%date %level %message\n")
assert(opts.logPatterns.FATAL == "%date %level %message\n")
end
tests.returns_defaults_if_prefix_not_found = function()
assert(logenv.set_default_settings("not_a_real_prefix"))
local name, opts = logenv.get_default_settings()
-- print("title: ", require("pl.pretty").write(opts))
assert(name == "console", "expected 'console' to be the default logger")
assert(type(opts) == "table", "expected 'opts' to be a table")
assert(opts.destination == "stderr", "expected 'destination' to be 'stderr'")
assert(opts.logLevel == "DEBUG", "expected 'logLevel' to be 'DEBUG'")
assert(opts.logPatterns.DEBUG == "%date %level %message\n")
assert(opts.logPatterns.INFO == "%date %level %message\n")
assert(opts.logPatterns.WARN == "%date %level %message\n")
assert(opts.logPatterns.ERROR == "%date %level %message\n")
assert(opts.logPatterns.FATAL == "%date %level %message\n")
end
tests.fails_if_default_already_set = function()
assert(logenv.set_default_settings("prefix"))
local ok, err = logenv.set_default_settings("prefix")
assert(err == "already set a default")
assert(ok == nil)
end
tests.prefix_defaults_to_LL = function()
env.LL_LOGLEVEL = assert(logging.ERROR)
local _, opts = logenv.get_default_settings()
-- print("title: ", require("pl.pretty").write(opts))
assert(opts.logLevel == "ERROR", "expected 'logLevel' to be 'ERROR'")
end
tests.loads_patterns_object = function()
env.LL_LOGPATTERN = "not used"
env.LL_LOGPATTERNS_DEBUG = "debug"
env.LL_LOGPATTERNS_INFO = "info"
env.LL_LOGPATTERNS_WARN = "warn"
env.LL_LOGPATTERNS_ERROR = "error"
env.LL_LOGPATTERNS_FATAL = "fatal"
local _, opts = logenv.get_default_settings()
-- print("title: ", require("pl.pretty").write(opts))
assert(opts.logPattern == "not used")
assert(opts.logPatterns.DEBUG == "debug")
assert(opts.logPatterns.INFO == "info")
assert(opts.logPatterns.WARN == "warn")
assert(opts.logPatterns.ERROR == "error")
assert(opts.logPatterns.FATAL == "fatal")
end
tests.fills_patterns_object_from_logpattern = function()
env.LL_LOGPATTERN = "this one"
local _, opts = logenv.get_default_settings()
-- print("title: ", require("pl.pretty").write(opts))
assert(opts.logPattern == "this one")
assert(opts.logPatterns.DEBUG == "this one")
assert(opts.logPatterns.INFO == "this one")
assert(opts.logPatterns.WARN == "this one")
assert(opts.logPatterns.ERROR == "this one")
assert(opts.logPatterns.FATAL == "this one")
end
tests.bad_loglevel_not_accepted = function()
env.LL_LOGLEVEL = "something bad"
local ok, loggername, opts = pcall(logenv.get_default_settings)
-- print("title: ", require("pl.pretty").write(opts))
assert(not ok)
assert(opts == nil)
assert(type(loggername) == "string")
end
tests.does_dynamic_lookups_of_vars = function()
local _, opts = logenv.get_default_settings() -- "LL" is now prefix
-- print("title: ", require("pl.pretty").write(opts))
env.LL_SOME_VALUE = "hello"
assert(opts.some_value == "hello")
end
tests.converts_booleans = function()
local _, opts = logenv.get_default_settings() -- "LL" is now prefix
-- print("title: ", require("pl.pretty").write(opts))
env.LL_ONE = "TRUE"
env.LL_TWO = "true"
env.LL_THREE = "false"
env.LL_FOUR = "FALSE"
assert(opts.one == true)
assert(opts.two == true)
assert(opts.three == false)
assert(opts.four == false)
end
tests.converts_numbers = function()
local _, opts = logenv.get_default_settings() -- "LL" is now prefix
env.LL_ONE = "1"
env.LL_TWO = "-2"
env.LL_THREE = ".2"
env.LL_FOUR = "1.2"
assert(opts.one == 1, "got: "..tostring(opts.one).." ("..type(opts.one)..")")
assert(opts.two == -2, "got: "..tostring(opts.two).." ("..type(opts.two)..")")
assert(opts.three == .2, "got: "..tostring(opts.three).." ("..type(opts.three)..")")
assert(opts.four == 1.2, "got: "..tostring(opts.four).." ("..type(opts.four)..")")
end
for name, func in pairs(tests) do
reset()
print("env-config test: " .. name)
func()
end
print("[v] all env-config tests succesful")
os.getenv = old_getenv -- luacheck: ignore

View file

@ -1,81 +0,0 @@
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")

View file

@ -1,16 +0,0 @@
local log_email = require"logging.email"
local logger = log_email {
rcpt = "mail@host.com",
from = "mail@host.com",
{
subject = "[%level] logging.email test",
}, -- headers
}
logger:info("logging.email test")
logger:debug("debugging...")
logger:error("error!")
print("Mail Logging OK")

View file

@ -1,30 +0,0 @@
local log_file = require "logging.rolling_file"
local max_size = 1024 * 10 --10kb
local max_index = 5
local total_log_size = max_size * max_index --more than needed because of the log pattern
local log_filename = "test.log"
local logger = log_file(log_filename, max_size, max_index)
-- it will generate the log + max_index backup files
local size = 0
while size < total_log_size do
local data = string.format("Test actual size[%d]", size)
logger:debug(data)
size = size + #data
end
-- lets test if all files where created
for i = 1, max_index do
local file = assert(io.open(log_filename.."."..tostring(i), "r"))
-- since there is an exact precision on the rolling
-- (it can be a little less or a little more than the max_size)
-- lets just test if the file is empty.
assert(file:seek("end", 0) > 0)
file:close()
end
print("RollingFile Logging OK")

View file

@ -1,25 +0,0 @@
local log_sql = require "logging.sql"
local _, luasql = pcall(require, "luasql")
local has_module = pcall(require, "luasql.sqlite3")
if not has_module then
print("SQLite 3 Logging SKIP (missing luasql.sqlite3)")
else
if not luasql or not luasql.sqlite3 then
print("Missing LuaSQL SQLite 3 driver!")
else
local env = luasql.sqlite3()
local logger = log_sql{
connectionfactory = function()
return assert(env:connect("test.db"))
end,
keepalive = true,
}
logger:info("logging.sql test")
logger:debug("debugging...")
logger:error("error!")
print("SQLite 3 Logging OK")
end
end

View file

@ -1,10 +0,0 @@
local log_sock = require"logging.socket"
local logger = log_sock("localhost", "5000")
logger:info("logging.socket test")
logger:debug("debugging...")
logger:error("error!")
print("Socket Logging OK")