forked from jamiejennings/rosie-pattern-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli-test.lua
112 lines (96 loc) · 4.48 KB
/
cli-test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---- -*- Mode: Lua; -*-
----
---- cli-test.lua sniff test for the CLI
----
---- © Copyright IBM Corporation 2016.
---- LICENSE: MIT License (https://opensource.org/licenses/mit-license.html)
---- AUTHOR: Jamie A. Jennings
util = require "util"
require "test-functions"
check = test.check
assert(rosie, "lua variable rosie not defined as the path to the rosie executable")
test.start(test.current_filename())
infilename = "/tmp/rosietestinputfile"
input = [[
#
# This file is automatically generated on OSX.
#
search nc.rr.com
nameserver 10.0.1.1
nameserver 2606:a000:1120:8152:2f7:6fff:fed4:dc1f
/usr/share/bin/foo
]]
f, msg = io.open(infilename, "w")
if (not f) then
error("Could not create the input file for this test: " .. tostring(msg))
end
f:write(input)
f:close()
print("Input file (" .. infilename .. ") created successfully")
function run(expression, grep_flag, expectations)
test.heading(expression)
test.subheading((grep_flag and "Using grep option") or "No grep option")
local verb = (grep_flag and "Grepping for") or "Matching"
print("\nSTART ----------------- " .. verb .. " '" .. expression .. "' against fixed input -----------------")
local grep = (grep_flag and " -grep") or ""
local cmd = rosie .. grep .. " '" .. expression .. "' " .. infilename
print(cmd)
local results, status, code = util.os_execute_capture(cmd, nil, "l")
if not results then error("Run failed: " .. tostring(status) .. ", " .. tostring(code)); end
local mismatch_flag = false;
for i=1, #results do
print(results[i])
if expectations then
if results[i]~=expectations[i] then print("Mismatch"); mismatch_flag = true; end
end
end -- for
if expectations then
if mismatch_flag then
print("********** SOME MISMATCHED OUTPUT WAS FOUND. **********");
else
print("END ----------------- All output matched expectations. -----------------");
end
if (not (#results==#expectations)) then
print(string.format("********** Mismatched number of results (%d) versus expectations (%d) **********", #results, #expectations))
end
check((not mismatch_flag), "Mismatched output compared to expectations", 1)
end -- if expectations
return results
end
results_basic_matchall =
{"\27[30m#\27[0m ",
"\27[30m#\27[0m \27[33mThis\27[0m \27[33mfile\27[0m \27[33mis\27[0m \27[33mautomatically\27[0m \27[33mgenerated\27[0m \27[33mon\27[0m \27[36mOSX\27[0m \27[30m.\27[0m ",
"\27[30m#\27[0m ",
"\27[33msearch\27[0m \27[31mnc.rr.com\27[0m ",
"\27[33mnameserver\27[0m \27[31m10.0.1.1\27[0m ",
"\27[33mnameserver\27[0m \27[4m2606\27[0m \27[30m:\27[0m \27[4ma000\27[0m \27[30m:\27[0m \27[4m1120\27[0m \27[30m:\27[0m \27[4m8152\27[0m \27[30m:\27[0m \27[4m2f7\27[0m \27[30m:\27[0m \27[4m6fff\27[0m \27[30m:\27[0m \27[4mfed4\27[0m \27[30m:\27[0m \27[4mdc1f\27[0m ",
"\27[32m/usr/share/bin/foo\27[0m ",
"\27[[email protected]\27[0m "}
results_common_word =
{"\27[33msearch\27[0m ",
"\27[33mnameserver\27[0m ",
"\27[33mnameserver\27[0m ",
"\27[33mjjennings\27[0m "}
results_common_word_grep =
{"\27[33mThis\27[0m \27[33mfile\27[0m \27[33mis\27[0m \27[33mautomatically\27[0m \27[33mgenerated\27[0m \27[33mon\27[0m \27[33mOSX\27[0m ",
"\27[33msearch\27[0m \27[33mnc\27[0m \27[33mrr\27[0m \27[33mcom\27[0m ",
"\27[33mnameserver\27[0m ",
"\27[33mnameserver\27[0m \27[33ma\27[0m \27[33mf\27[0m \27[33mfff\27[0m \27[33mfed\27[0m \27[33mdc\27[0m \27[33mf\27[0m ",
"\27[33musr\27[0m \27[33mshare\27[0m \27[33mbin\27[0m \27[33mfoo\27[0m ",
"\27[33mjjennings\27[0m \27[33mus\27[0m \27[33mibm\27[0m \27[33mcom\27[0m "}
results_word_network =
{"\27[33msearch\27[0m \27[31mnc.rr.com\27[0m ",
"\27[33mnameserver\27[0m \27[31m10.0.1.1\27[0m "}
results_common_number =
{"\27[4m10.0\27[0m \27[4m1.1\27[0m ",
"\27[4m2606\27[0m \27[4ma000\27[0m \27[4m1120\27[0m \27[4m8152\27[0m \27[4m2f7\27[0m \27[4m6fff\27[0m \27[4mfed4\27[0m \27[4mdc1f\27[0m "}
run("basic.matchall", nil, results_basic_matchall)
run("common.word", nil, results_common_word)
run("common.word", true, results_common_word_grep)
run("common.word basic.network_patterns", nil, results_word_network)
run("~common.number~", true, results_common_number)
ok, ignore = pcall(run, '"Gold"', nil, nil)
check(ok, [[testing for a shell quoting error in which rpl expressions containing double quotes
were not properly passed to lua in bin/run-rosie]])
return test.finish()