-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.lua
executable file
·74 lines (59 loc) · 1.87 KB
/
run.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
#! /usr/bin/luajit
--[[ run.lua usage
- Run with no arguments to run unit tests and generate coverage reports.
- Run with one argument to only test and generate coverage reports for files where the argument is a
substring of the file name.
]]
local STATS_FILE_NAME = "luacov.stats.out"
local REPORT_FILE_NAME = "luacov.report.out"
local DEBUG = os.getenv("DEBUG") == "true" and true or false
local function print_debug(...)
if DEBUG then print(...) end
end
local function exec(cmd)
print("> " .. cmd)
os.execute(cmd)
end
local function ends_with(str, ending)
print_debug(str .. " ends with " .. ending .. "? Ending: " .. str:sub(-(#ending + 4), -5))
return str:sub(-(#ending + 4), -5) == ending
end
os.remove(STATS_FILE_NAME)
local filter = arg[1]
if filter == nil then filter = "" end
local pattern = ""
if filter ~= "" then
pattern = ("--pattern='%s_spec'"):format(filter)
end
local debug_env = "DEBUG=FALSE"
if os.getenv("DEBUG") ~= "FALSE" then
debug_env = "DEBUG=TRUE"
end
local unit_test_env = "UNIT_TEST=FALSE"
if os.getenv("UNIT_TEST") ~= "FALSE" then
unit_test_env = "UNIT_TEST=TRUE"
end
exec(("env %s %s busted --lua=luajit --coverage %s ."):format(debug_env, unit_test_env, pattern))
local file = io.open(STATS_FILE_NAME, "r")
local lines = {}
local retain = 0
for line in file:lines() do
-- if line is a header and the entry should be retained, retain 2 lines (this and the next)
if line:find(".lua") and not (line:find("/usr/") or line:find("_spec"))
and (filter == "" or ends_with(line, filter)) then
retain = 2
end
-- if a line should be retained, insert it into lines and decrement retainer
if retain > 0 then
table.insert(lines, line)
retain = retain - 1
end
end
file = io.open(STATS_FILE_NAME, "w")
for _, line in ipairs(lines) do
file:write(line .. "\n")
end
file:close()
os.execute("sleep 1")
os.execute("luacov")
os.execute("less " .. REPORT_FILE_NAME)