main.lua

Fri, 11 Dec 2015 17:19:30 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 11 Dec 2015 17:19:30 +0000
changeset 56
5eda634ea15b
parent 55
996f30d43a88
child 57
6407a0157518
permissions
-rwxr-xr-x

main.lua: Command-line processing, and a way to set connect_host/connect_port from the command-line

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
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
3 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
4
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
5 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
6 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
7 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
8 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
9 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
10 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
11 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
12 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
13 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
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
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 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
17 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
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
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 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
28 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
29 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
30 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
31 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
32 table.insert(property_rules, { class = "client", properties = { connect_host = host } });
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 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
34 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
35 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
36 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
37 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
38
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
39 function read_script()
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
40 io.input(arg[1]);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
41 return io.read("*a");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
42
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
43 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
44
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
45 function parse_script(data)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
46 local parser = require "scansion.parser";
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
47 return assert(parser.parse(data));
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
48 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
49
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
50 function initialize_script(script)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
51 local c = 0;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
52 for name, object in pairs(script.objects) do
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
53 local o = require("scansion.objects."..object.type);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
54 object.handler = o;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
55 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
56 apply_object_properties(object.type, object.name, object);
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
57 o._validate(object);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
58 c = c + 1;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
59 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
60
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
61 --print("Script defines "..c.." objects, and "..#script.actions.." actions");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
62 return script;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
63 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
64
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
65 function initialize_verse(errcb)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
66 local verse = require "verse";
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 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
69
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
70 local function error_handler(err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
71 verse.log("error", "Error: %s", err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
72 verse.log("error", "Traceback: %s", debug.traceback());
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
73 errcb(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
74 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
75 verse.set_error_handler(error_handler);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
76 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
77 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
78
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
79 function main()
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
80 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
81
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
82 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
83
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
84 local script = initialize_script(parse_script(read_script()));
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
85
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
86 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
87 local async = require "scansion.async";
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
88
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
89
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
90 local runner = async.runner(function (d)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
91 for _, action in pairs(script.actions) do
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
92 local object = script.objects[action.object_name];
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
93 local handler = object.handler;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
94 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
95 print("");
43
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
96 if action.annotation then
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
97 print(action.annotation);
b37504fa3031 Add annotations to actions (by grabbing the preceding comment)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
98 end
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
99 print(object.name, action.action.."...");
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
100 local ok, err = pcall(handler[action.action], object, action.extra);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
101 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
102 error(err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
103 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
104 end
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
105 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
106 verse.quit();
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
107 end, {
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
108 error = function (runner, _err)
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
109 verse.log("error", "Runner caught error: %s", _err);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
110 ok, err = false, _err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
111 verse.quit();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
112 end;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
113 });
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
114
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
115 runner:run(true);
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
116 verse.log("debug", "runner paused")
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
117
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
118 verse.loop();
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
119 return ok, err;
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
120 end
7
ecac723bb6e1 main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
121
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
122 local ok, result, err = pcall(main);
3
3cc860a893d2 main: verse.loop()
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
123
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
124 if not ok then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
125 print("TEST ERROR:", result);
35
90219526e942 main: Exit with status codes, allows running directly from bisect
Kim Alvefur <zash@zash.se>
parents: 30
diff changeset
126 os.exit(2);
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
127 elseif not result then
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
128 print("TEST FAILED", err);
35
90219526e942 main: Exit with status codes, allows running directly from bisect
Kim Alvefur <zash@zash.se>
parents: 30
diff changeset
129 os.exit(1);
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
130 else
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
131 print("TEST PASSED");
35
90219526e942 main: Exit with status codes, allows running directly from bisect
Kim Alvefur <zash@zash.se>
parents: 30
diff changeset
132 os.exit(0);
21
8ac80da35408 main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
133 end

mercurial