Skip to content

Commit

Permalink
tests(*): refactor spec/busted-ci-helper.lua
Browse files Browse the repository at this point in the history
Code like this can get unwieldy fast. This refactors
spec/busted-ci-helper.lua so that the different tasks it performs are
kept distinct.
  • Loading branch information
flrgh committed Dec 10, 2024
1 parent c01e93d commit ff03381
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions spec/busted-ci-helper.lua
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
-- busted-ci-helper.lua
local busted = require 'busted'

do
local shutdown_timers = require("kong.cmd.utils.timer").shutdown
assert(type(shutdown_timers) == "function")

-- needed before requiring 'socket.unix'
require 'socket'
-- shutdown lua-resty-timer-ng to allow the nginx worker to stop quickly
busted.subscribe({ 'exit' }, shutdown_timers)
end

local busted = require 'busted'
local cjson = require 'cjson'
local socket_unix = require 'socket.unix'
local BUSTED_EVENT_PATH = os.getenv("BUSTED_EVENT_PATH")
if BUSTED_EVENT_PATH then
-- needed before requiring 'socket.unix'
require 'socket'

local busted_event_path = os.getenv("BUSTED_EVENT_PATH")
local cjson = require 'cjson'
local socket_unix = require 'socket.unix'

-- Function to recursively copy a table, skipping keys associated with functions
local function copyTable(original, copied, cache, max_depth, current_depth)
copied = copied or {}
cache = cache or {}
max_depth = max_depth or 5
current_depth = current_depth or 1
-- Function to recursively copy a table, skipping keys associated with functions
local function copyTable(original, copied, cache, max_depth, current_depth)
copied = copied or {}
cache = cache or {}
max_depth = max_depth or 5
current_depth = current_depth or 1

if cache[original] then return cache[original] end
cache[original] = copied
if cache[original] then return cache[original] end
cache[original] = copied

for key, value in pairs(original) do
if type(value) == "table" then
if current_depth < max_depth then
copied[key] = copyTable(value, {}, cache, max_depth, current_depth + 1)
for key, value in pairs(original) do
if type(value) == "table" then
if current_depth < max_depth then
copied[key] = copyTable(value, {}, cache, max_depth, current_depth + 1)
end
elseif type(value) == "userdata" then
copied[key] = tostring(value)
elseif type(value) ~= "function" then
copied[key] = value
end
elseif type(value) == "userdata" then
copied[key] = tostring(value)
elseif type(value) ~= "function" then
copied[key] = value
end
end

return copied
end
return copied
end

if busted_event_path then
local sock = assert(socket_unix())
assert(sock:connect(busted_event_path))
assert(sock:connect(BUSTED_EVENT_PATH))

local events = {{ 'suite', 'reset' },
{ 'suite', 'start' },
Expand All @@ -51,6 +57,7 @@ if busted_event_path then
{ 'error', 'it' },
{ 'failure' },
{ 'error' }}

for _, event in ipairs(events) do
busted.subscribe(event, function (...)
local args = {}
Expand All @@ -69,6 +76,3 @@ if busted_event_path then
end)
end
end

-- shutdown lua-resty-timer-ng to allow the nginx worker to stop quickly
busted.subscribe({ 'exit' }, require("kong.cmd.utils.timer").shutdown)

0 comments on commit ff03381

Please sign in to comment.