main.lua

Thu, 17 Dec 2015 14:25:09 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 17 Dec 2015 14:25:09 +0000
changeset 59
909305ae72ac
parent 58
8fa0a464c727
child 61
21871fb2db99
permissions
-rwxr-xr-x

main.lua: Whitespace fix

54
09a754d5fcc1 main: Change shebang to LuaJIT
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
1 #!/usr/bin/env luajit
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
2
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
3 local json = require "cjson";
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
4
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
5 local result_log_filename = nil;
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
6 local property_rules = {};
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
7
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
8 local function apply_object_properties(class, name, object)
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
9 for _, rule in ipairs(property_rules) do
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
10 if (not(rule.class) or tostring(rule.class):lower() == tostring(class):lower())
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
11 and (not(rule.name) or tostring(rule.name):lower() == tostring(name):lower()) then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
12 for prop_key, prop_value in pairs(rule.properties) do
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
13 object[prop_key] = prop_value;
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
14 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
15 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
16 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
17 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
18
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
19 function process_options()
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
20 local function get_value()
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
21 return (assert(table.remove(arg, 1), "unexpected end of command-line options"));
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
22 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
23 while arg[1] and arg[1]:sub(1,1) == "-" do
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
24 local opt = arg[1];
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
25 if opt == "--" then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
26 table.remove(arg, 1);
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
27 break;
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
28 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
29 table.remove(arg, 1);
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
30 if opt == "--port" or opt == "-p" then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
31 local port = assert(tonumber(get_value()), "port number must be a number");
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
32 table.insert(property_rules, { class = "client", properties = { connect_port = port } });
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
33 elseif opt == "--host" or opt == "-h" then
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
34 local host = get_value();
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
35 table.insert(property_rules, { class = "client", properties = { connect_host = host } });
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
36 elseif opt == "--out" or opt == "-o" then
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
37 result_log_filename = get_value();
58
8fa0a464c727 main.lua: Error on unknown command-line options
Matthew Wild <mwild1@gmail.com>
parents: 57
diff changeset
38 else
8fa0a464c727 main.lua: Error on unknown command-line options
Matthew Wild <mwild1@gmail.com>
parents: 57
diff changeset
39 error("Unhandled command-line option: "..opt);
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
40 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
41 end
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
42 assert(#arg > 0, "No test script provided");
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
43 assert(#arg < 2, "Too many parameters");
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
44 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
46 function read_script()
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
47 io.input(arg[1]);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
48 return io.read("*a");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
49
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
50 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
52 function parse_script(data)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
53 local parser = require "scansion.parser";
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
54 return assert(parser.parse(data));
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
55 end
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
57 function initialize_script(script)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
58 local c = 0;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
59 for name, object in pairs(script.objects) do
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
60 local o = require("scansion.objects."..object.type);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
61 object.handler = o;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
62 object.script = script;
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
63 apply_object_properties(object.type, object.name, object);
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
64 o._validate(object);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
65 c = c + 1;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
66 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
67
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
68 --print("Script defines "..c.." objects, and "..#script.actions.." actions");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
69 return script;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
70 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
71
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
72 function initialize_verse(errcb)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
73 local verse = require "verse";
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
74
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
75 verse.set_log_handler(verse._default_log_handler, { "debug", "info", "warn", "error" });
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
76
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
77 local function error_handler(err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
78 verse.log("error", "Error: %s", err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
79 verse.log("error", "Traceback: %s", debug.traceback());
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
80 errcb(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
81 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
82 verse.set_error_handler(error_handler);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
83 return verse;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
86 function main(log_data)
56
5eda634ea15b main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line
Matthew Wild <mwild1@gmail.com>
parents: 55
diff changeset
87
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
88 local ok, err = true;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
90 local script = initialize_script(parse_script(read_script()));
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
91
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
92 local verse = initialize_verse(function (_err) ok, err = false, _err end);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
93 local async = require "scansion.async";
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
94
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
95 local runner = async.runner(function (d)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
96 for _, action in pairs(script.actions) do
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
97 local object = script.objects[action.object_name];
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
98 local handler = object.handler;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
99 assert(handler[action.action], "Objects of type '"..object.type.."' do not support action '"..action.action.."'");
55
996f30d43a88 main.lua: Print blank line between actions for easier reading
Matthew Wild <mwild1@gmail.com>
parents: 54
diff changeset
100 print("");
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
101 log_data("action", {
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
102 action = action.action;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
103 object = object.name;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
104 object_type = object.type;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
105 extra = action.extra;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
106 annotation = action.annotation;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
107 });
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
108 if action.annotation then
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
109 print(action.annotation);
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
110 end
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
111 print(object.name, action.action.."...");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
112 local ok, err = pcall(handler[action.action], object, action.extra);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
113 if not ok then
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
114 log_data("error", { message = err });
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
115 error(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
116 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
117 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
118 verse.log("info", "Completed script!");
30
3c2489e79074 main: Remove 5s delay on quitting (was a hack for debug purposes)
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
119 verse.quit();
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
120 end, {
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
121 error = function (runner, _err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
122 verse.log("error", "Runner caught error: %s", _err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
123 ok, err = false, _err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
124 verse.quit();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
125 end;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
126 });
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
127
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
128 runner:run(true);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
129 verse.log("debug", "runner paused")
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
130
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
131 verse.loop();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
132 return ok, err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
133 end
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
134
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
135 -- Process command-line options
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
136 process_options();
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
137
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
138 -- Dummy logging function, used if no log file set
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
139 local log_data = function () end;
3
3cc860a893d2 main: verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
140
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
141 local result_log;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
142 if result_log_filename then
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
143 result_log = assert(io.open(result_log_filename, "w+"));
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
144 result_log:write("[\n");
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
145 function log_data(type, data)
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
146 local entry = { type = type, data = data, time = os.time() };
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
147 result_log:write(" ", json.encode(entry), ",\n");
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
148 result_log:flush();
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
149 end
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
150 end
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
151
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
152 log_data("start");
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
153 local ok, result, err = pcall(main, log_data);
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
154
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
155 local exit_code = 0;
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
156 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
157 print("TEST ERROR:", result);
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
158 exit_code = 2;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
159 log_data("test-error", { message = result });
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
160 elseif not result then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
161 print("TEST FAILED", err);
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
162 exit_code = 1;
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
163 log_data("test-failed", { message = err });
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
164 else
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
165 print("TEST PASSED");
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
166 log_data("test-passed");
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
167 end
57
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
168 log_data("end");
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
169
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
170 if result_log then
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
171 result_log:write("]\n");
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
172 result_log:close();
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
173 end
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
174
6407a0157518 main.lua: Add support for JSON log output to a file, for a machine-readable transcript of the test results
Matthew Wild <mwild1@gmail.com>
parents: 56
diff changeset
175 os.exit(exit_code);

mercurial