main: Refactor to support a more advanced summary at the end of a test run

Tue, 11 Sep 2018 21:02:02 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 11 Sep 2018 21:02:02 +0100
changeset 125
dfff8dfe8861
parent 124
168cc5aad639
child 126
87ea077acc21

main: Refactor to support a more advanced summary at the end of a test run

main.lua file | annotate | diff | comparison | revisions
--- a/main.lua	Tue Sep 11 21:01:15 2018 +0100
+++ b/main.lua	Tue Sep 11 21:02:02 2018 +0100
@@ -139,13 +139,11 @@
 	return verse;
 end
 
-local function main(log_data, script_name)
+local function main(log_data, script)
 
 	local ok, err = true;
 
-	local script = initialize_script(parse_script(read_script(script_name)));
-
-	log_data("script", { title = script.title, summary = script.summary, tags = script.tags, filename = script_name });
+	initialize_script(script);
 
 	if server_log_reader then
 		if skip_server_startup_log then
@@ -253,40 +251,48 @@
 
 	if result_log then
 		local entry = { type = type, data = data, time = time() };
-		result_log:write("  ", json.encode(entry), ",\n");
+		result_log:write("  ", json.encode(entry), type ~= "end" and ",\n" or "\n");
 		result_log:flush();
+		if type == "end" then
+			result_log:write("]\n");
+			result_log:close();
+			result_log = nil;
+		end
 	end
 end;
 
 local function run_test_script(script_name)
-	local ok, result, err = pcall(main, log_data, script_name);
+	local script = parse_script(read_script(script_name))
+	log_data("script", { title = script.title, summary = script.summary, tags = script.tags, filename = script_name });
 
-	local exit_code = 0;
+	local ok, result, err = pcall(main, log_data, script);
+
+	local status, reason = "ok";
 	if not ok then
-		exit_code = 2;
+		status, reason = "error", result;
 		log_data("test-error", { error = result });
 	elseif not result then
-		exit_code = 1;
+		status, reason = "fail", err;
 		log_data("test-failed", { error = err });
 	elseif not quiet then
 		log_data("test-passed");
 	end
-	return exit_code;
+	return { name = script.title or script_name, status = status, reason = reason };
 end
 
 log_data("start", { metadata = test_metadata });
 
-local exit_code = 0;
+local result_tally = { all = {} };
+
 for i = 1, #arg do
 	local ret = run_test_script(arg[i]);
-	if ret > exit_code then
-		exit_code = ret;
+	if not result_tally[ret.status] then
+		result_tally[ret.status] = {};
 	end
+	table.insert(result_tally[ret.status], ret);
+	table.insert(result_tally.all, ret);
 end
 
-if result_log then
-	result_log:write([[  {"type": "end", "time": ]]..time().."}\n]\n");
-	result_log:close();
-end
+log_data("end", { time = time(), summary = result_tally });
 
 os.exit(exit_code);

mercurial