Revamp console output to be driven by log_data()

Mon, 10 Sep 2018 13:52:35 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 10 Sep 2018 13:52:35 +0100
changeset 115
0f8d0906af6e
parent 114
48aab6c9e9d3
child 116
b8296f44a9d1

Revamp console output to be driven by log_data()

main.lua file | annotate | diff | comparison | revisions
scansion/console.lua file | annotate | diff | comparison | revisions
--- a/main.lua	Mon Sep 10 13:48:25 2018 +0100
+++ b/main.lua	Mon Sep 10 13:52:35 2018 +0100
@@ -5,6 +5,7 @@
 local sleep = require "socket".sleep;
 
 local is_scansion_error = require "scansion.error".is;
+local console_handlers = require "scansion.console".handlers;
 
 local result_log_filename = nil;
 local server_log_reader = nil;
@@ -168,12 +169,13 @@
 				extra = action.extra;
 				annotation = action.annotation;
 			});
+			--[[
 			if not quiet then
 				if action.annotation then
 					print(action.annotation);
 				end
 				print(object.name.." "..action.action);
-			end
+			end]]
 			if action_timeout and action_timeout > 0 then
 				local action_number = i;
 				verse.add_task(action_timeout, function ()
@@ -220,35 +222,43 @@
 -- Process command-line options
 process_options();
 
--- Dummy logging function, used if no log file set
-local log_data = function () end;
+local function console_logger(event, data)
+	if not quiet or type == "test-failed" or type == "test-error" then
+		local h = console_handlers[event];
+		if h then
+			io.write(h(data), "\n");
+		end
+	end
+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)
+end
+
+local function log_data(type, data)
+	console_logger(type, data);
+
+	if result_log then
 		local entry = { type = type, data = data, time = time() };
 		result_log:write("  ", json.encode(entry), ",\n");
 		result_log:flush();
 	end
-end
+end;
 
 local function run_test_script(script_name)
-
 	local ok, result, err = pcall(main, log_data, script_name);
 
 	local exit_code = 0;
 	if not ok then
-		print("TEST ERROR:", result);
 		exit_code = 2;
 		log_data("test-error", { error = result });
 	elseif not result then
-		print("TEST FAILED", err);
 		exit_code = 1;
 		log_data("test-failed", { error = err });
 	elseif not quiet then
-		print("TEST PASSED");
 		log_data("test-passed");
 	end
 	return exit_code;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scansion/console.lua	Mon Sep 10 13:52:35 2018 +0100
@@ -0,0 +1,47 @@
+local pretty = require "scansion.pretty".new({});
+
+local function lines(l)
+	return table.concat(l, "\n");
+end
+
+local handlers = {
+	["script"] = function (data)
+		return "TEST: "..(data.title or data.filename);
+	end;
+	["test-passed"] = function ()
+		return "PASS";
+	end;
+	["test-failed"] = function (data)
+		local error_text;
+		if data.error and data.error.type == "unexpected-stanza" then
+			error_text = "Received unexpected stanza:\n\n"..pretty(data.error.data.stanza, 4);
+			if data.error.data.expected then
+				error_text = error_text.."\n\nExpected:\n\n"..pretty(data.error.data.expected, 4);
+			end
+		else
+			error_text = tostring(data.error);
+		end
+		return "FAIL: "..error_text
+	end;
+	["test-error"] = function (data)
+		return "ERROR: "..tostring(data.error);
+	end;
+
+	["action"] = function (data)
+		local action = data.action;
+		local obj_type = data.object_type;
+		local l = {};
+		if data.annotation then
+			table.insert(l, action.annotation);
+		end
+		table.insert(l, data.object.." "..action);
+		if data.extra and obj_type == "client" and (action == "sends" or action == "receives") then
+			table.insert(l, "\n"..pretty(lines(data.extra), 4).."\n");
+		end
+		return lines(l);
+	end;
+};
+
+return {
+	handlers = handlers;
+}

mercurial