scansion/console.lua

Thu, 23 Mar 2023 15:12:30 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 15:12:30 +0000
changeset 174
662bd8c5ae28
parent 148
e2c85e095a4c
child 181
3a9b9c98304a
permissions
-rw-r--r--

Serialize XML in a consistent order by default

This overrides all XML serialization to emit attributes in an ordered form, so
the XML will match across multiple runs. This can be useful for comparing
different runs, or even two stanzas printed in the same run (e.g. if there is
a mismatch).

115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
145
df4faaa2d36f scansion.console: Support for indentation in lines() helper
Matthew Wild <mwild1@gmail.com>
parents: 133
diff changeset
2 local function lines(l, indent)
df4faaa2d36f scansion.console: Support for indentation in lines() helper
Matthew Wild <mwild1@gmail.com>
parents: 133
diff changeset
3 return table.concat(l, "\n"..string.rep(" ", indent or 0));
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 end
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
131
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
6 local quiet_handlers = { "test-failed", "test-error" };
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
7
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
8 local function new(config)
174
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
9 local pretty = require "scansion.pretty".new({
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
10 sorted = config.ordered;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
11 });
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
12
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
13 local handlers = {
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
14 ["script"] = function (data)
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
15 return "TEST: "..(data.title or data.filename or "untitled");
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
16 end;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
17 ["test-passed"] = function ()
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
18 return "PASS";
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
19 end;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
20 ["test-failed"] = function (data)
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
21 local error_text;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
22 if data.reason and data.reason.type == "unexpected-stanza" then
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
23 error_text = "Received unexpected stanza:\n\n"..pretty(data.reason.data.stanza, 4);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
24 if data.reason.data.expected then
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
25 error_text = error_text.."\n\nExpected:\n\n"..pretty(data.reason.data.expected, 4);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
26 end
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
27 else
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
28 error_text = tostring(data.reason);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
29 end
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
30 return lines({
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
31 "FAILED: "..data.name;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
32 "";
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
33 (error_text:gsub("\n", "\n "));
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
34 "";
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
35 }, 4);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
36 end;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
37 ["test-error"] = function (data)
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
38 return lines({
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
39 "ERROR: "..data.name;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
40 "";
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
41 (tostring(data.reason):gsub("\n", "\n "));
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
42 "";
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
43 }, 4);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
44 end;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
45
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
46 ["action"] = function (data)
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
47 local action = data.action;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
48 local obj_type = data.object_type;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
49 local l = {};
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
50 if data.annotation then
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
51 table.insert(l, action.annotation);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
52 end
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
53 table.insert(l, data.object.." "..action);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
54 if data.extra and obj_type == "client" and (action == "sends" or action == "receives") then
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
55 table.insert(l, "\n"..pretty(lines(data.extra), 4).."\n");
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
56 end
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
57 return lines(l);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
58 end;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
59
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
60 ["end"] = function (data)
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
61 local r = {};
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
62
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
63 local all_results = {};
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
64 for _, v in pairs(data.summary.all) do
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
65 table.insert(all_results, v);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
66 end
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
67 table.sort(all_results, function (a, b) return a.name < b.name end);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
68
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
69 print("");
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
70 print("Summary");
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
71 print("-------");
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
72
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
73 for _, test_result in ipairs(all_results) do
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
74 print("", test_result.status, test_result.name);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
75 end
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
76
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
77 print("");
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
78
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
79 for _, test_result in ipairs{ "ok", "fail", "error", "skipped", "total" } do
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
80 local count = data.summary[test_result] and #data.summary[test_result] or 0;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
81 table.insert(r, tostring(count).." "..test_result);
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
82 end
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
83 return table.concat(r, " / ");
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
84 end;
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
85 };
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
86
662bd8c5ae28 Serialize XML in a consistent order by default
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
87
131
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
88 local h = {};
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
89 if config.quiet then
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
90 for _, handler_name in ipairs(quiet_handlers) do
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
91 h[handler_name] = handlers[handler_name];
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
92 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
93 if config.summary then
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
94 h["end"] = handlers["end"];
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
95 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
96 else
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
97 h = handlers;
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
98 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
99 return h;
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
100 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
101
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 return {
131
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
103 new = new;
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 }

mercurial