Merge pull request #2711 from psychon/improve_test_runner

Improve the test runner: allow "direct" tests (instead of only steps-based tests)
This commit is contained in:
Daniel Hahler 2019-03-03 11:12:04 +01:00 committed by GitHub
commit a802d0c2c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 93 deletions

View File

@ -43,77 +43,23 @@ runner.step_kill_clients = function(step)
end
end
runner.run_steps = function(steps, options)
-- Setup timer/timeout to limit waiting for signal and quitting awesome.
local t = timer({timeout=0})
local wait=20
local step=1
local step_count=0
options = options or {
kill_clients=true,
}
assert(not running, "run_steps() was called twice")
running = true
if options.kill_clients then
-- Add a final step to kill all clients and wait for them to finish.
-- Ref: https://github.com/awesomeWM/awesome/pull/1904#issuecomment-312793006
steps[#steps + 1] = runner.step_kill_clients
end
t:connect_signal("timeout", function() timer.delayed_call(function()
io.flush() -- for "tail -f".
step_count = step_count + 1
local step_as_string = step..'/'..#steps..' (@'..step_count..')'
--- Print a message if verbose mode is enabled.
-- @tparam string message The message to print.
function runner.verbose(message)
if verbose then
io.stderr:write(string.format('Running step %s..\n', step_as_string))
io.stderr:write(message .. "\n")
end
end
-- Call the current step's function.
local success, result = xpcall(function()
return steps[step](step_count)
end, debug.traceback)
if not success then
io.stderr:write('Error: running function for step '
..step_as_string..': '..tostring(result)..'!\n')
t:stop()
if not runner.quit_awesome_on_error then
io.stderr:write("Keeping awesome open...\n")
return -- keep awesome open on error.
end
elseif result then
-- true: test succeeded.
if step < #steps then
-- Next step.
step = step+1
step_count = 0
wait = 20
t.timeout = 0
t:again()
return
end
elseif result == false then
io.stderr:write("Step "..step_as_string.." failed (returned false).\n")
--- When using run_direct(), this function indicates that the test is now done.
-- @tparam[opt=nil] string message An error message explaining the test failure, if it failed.
function runner.done(message)
if message then
io.stderr:write("Error: " .. message .. "\n")
if not runner.quit_awesome_on_error then
io.stderr:write("Keeping awesome open...\n")
return
end
else
-- No result yet, run this step again.
wait = wait-1
if wait > 0 then
t.timeout = 0.1
t:again()
else
io.stderr:write("Error: timeout waiting for signal in step "
..step_as_string..".\n")
t:stop()
end
return
end
local client_count = #client.get()
@ -127,10 +73,82 @@ runner.run_steps = function(steps, options)
end
end
if success and result then
if not message then
io.stderr:write("Test finished successfully.\n")
end
awesome.quit()
end
--- This function is called to indicate that a test does not use the run_steps()
-- facility, but instead runs something else directly.
function runner.run_direct()
assert(not running, "API abuse: Test was started twice")
running = true
end
--- Start some step-wise tests. The given steps are called in order until all
-- succeeded. Each step is a function that can return true/false to indicate
-- success/failure, but can also return nothing if it needs to be called again
-- later.
function runner.run_steps(steps, options)
-- Setup timer/timeout to limit waiting for signal and quitting awesome.
local t = timer({timeout=0})
local wait=20
local step=1
local step_count=0
options = options or {
kill_clients=true,
}
runner.run_direct()
if options.kill_clients then
-- Add a final step to kill all clients and wait for them to finish.
-- Ref: https://github.com/awesomeWM/awesome/pull/1904#issuecomment-312793006
steps[#steps + 1] = runner.step_kill_clients
end
t:connect_signal("timeout", function() timer.delayed_call(function()
io.flush() -- for "tail -f".
step_count = step_count + 1
local step_as_string = step..'/'..#steps..' (@'..step_count..')'
runner.verbose(string.format('Running step %s..\n', step_as_string))
-- Call the current step's function.
local success, result = xpcall(function()
return steps[step](step_count)
end, debug.traceback)
if not success then
runner.done('running function for step '
..step_as_string..': '..tostring(result)..'!')
t:stop()
elseif result then
-- true: test succeeded.
if step < #steps then
-- Next step.
step = step+1
step_count = 0
wait = 20
t.timeout = 0
t:again()
else
-- All steps finished, we are done.
runner.done()
end
elseif result == false then
runner.done("Step "..step_as_string.." failed (returned false).")
else
-- No result yet, run this step again.
wait = wait-1
if wait > 0 then
t.timeout = 0.1
t:again()
else
runner.done("timeout waiting for signal in step "
..step_as_string..".")
t:stop()
end
end
end) end)
t:start()
end

View File

@ -3,23 +3,27 @@
local runner = require("_runner")
local spawn = require("awful.spawn")
local todo = 2
local had_exit, had_success
local had_error = false
local function wait_a_bit(count)
if todo == 0 or count == 5 then
return true
local function check_done()
if had_exit and had_success then
if had_error then
runner.done("Some error occurred, see above")
else
runner.done()
end
end
end
runner.run_steps({
function()
local err = spawn.with_line_callback(
local err = spawn.with_line_callback(
{ os.getenv("build_dir") .. "/test-gravity" },
{
exit = function(what, code)
assert(what == "exit", what)
assert(code == 0, "Exit code was " .. code)
todo = todo - 1
had_exit = true
check_done()
end,
stderr = function(line)
had_error = true
@ -27,38 +31,16 @@ runner.run_steps({
end,
stdout = function(line)
if line == "SUCCESS" then
todo = todo - 1
had_success = true
check_done()
elseif line:sub(1, 5) ~= "LOG: " then
had_error = true
print("Read on stdout: " .. line)
end
end
})
assert(type(err) ~= "string", err)
return true
end,
-- Buy the external program some time to finish
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
wait_a_bit,
function()
if todo == 0 then
assert(not had_error, "Some error occurred, see above")
return true
end
end
})
assert(type(err) ~= "string", err)
runner.run_direct()
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80