Thu, 17 Dec 2015 14:24:19 +0000
main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
main.lua | file | annotate | diff | comparison | revisions |
--- a/main.lua Fri Dec 11 17:19:30 2015 +0000 +++ b/main.lua Thu Dec 17 14:24:19 2015 +0000 @@ -1,5 +1,8 @@ #!/usr/bin/env luajit +local json = require "cjson"; + +local result_log_filename = nil; local property_rules = {}; local function apply_object_properties(class, name, object) @@ -30,6 +33,8 @@ elseif opt == "--host" or opt == "-h" then local host = get_value(); table.insert(property_rules, { class = "client", properties = { connect_host = host } }); + elseif opt == "--out" or opt == "-o" then + result_log_filename = get_value(); end end assert(#arg > 0, "No test script provided"); @@ -76,8 +81,7 @@ return verse; end -function main() - process_options(); +function main(log_data) local ok, err = true; @@ -93,12 +97,20 @@ local handler = object.handler; assert(handler[action.action], "Objects of type '"..object.type.."' do not support action '"..action.action.."'"); print(""); + log_data("action", { + action = action.action; + object = object.name; + object_type = object.type; + extra = action.extra; + annotation = action.annotation; + }); if action.annotation then print(action.annotation); end print(object.name, action.action.."..."); local ok, err = pcall(handler[action.action], object, action.extra); if not ok then + log_data("error", { message = err }); error(err); end end @@ -119,15 +131,44 @@ return ok, err; end -local ok, result, err = pcall(main); +-- Process command-line options +process_options(); + +-- Dummy logging function, used if no log file set +local log_data = function () end; +local result_log; +if result_log_filename then + result_log = assert(io.open(result_log_filename, "w+")); + result_log:write("[\n"); + function log_data(type, data) + local entry = { type = type, data = data, time = os.time() }; + result_log:write(" ", json.encode(entry), ",\n"); + result_log:flush(); + end +end + +log_data("start"); +local ok, result, err = pcall(main, log_data); + +local exit_code = 0; if not ok then print("TEST ERROR:", result); - os.exit(2); + exit_code = 2; + log_data("test-error", { message = result }); elseif not result then print("TEST FAILED", err); - os.exit(1); + exit_code = 1; + log_data("test-failed", { message = err }); else print("TEST PASSED"); - os.exit(0); + log_data("test-passed"); end +log_data("end"); + +if result_log then + result_log:write("]\n"); + result_log:close(); +end + +os.exit(exit_code);