-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_luacov_stats.lua
executable file
·103 lines (97 loc) · 2.24 KB
/
merge_luacov_stats.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
#!/usr/bin/env lua
local function luacov_stats_load(statsfile, data)
data = data or {}
local fd, err = io.open(statsfile, "r")
if not fd then
return nil, err
end
while true do
local max = fd:read("*n")
if not max then
break
end
if fd:read(1) ~= ":" then
break
end
local filename = fd:read("*l")
if not filename then
break
end
if not data[filename] then
data[filename] = {
max = max,
max_hits = 0
}
end
for i = 1, max do
local hits = fd:read("*n")
if not hits then
break
end
if fd:read(1) ~= " " then
break
end
if hits > 0 then
data[filename][i] = data[filename][i] or 0 + hits
data[filename].max_hits = math.max(data[filename].max_hits, data[filename][i])
end
end
end
fd:close()
return data
end
local function luacov_stats_save(statsfile, data, fd)
fd = fd or assert(io.open(statsfile, "w"))
local filenames = {}
for filename in pairs(data) do
table.insert(filenames, filename)
end
table.sort(filenames)
for _, filename in ipairs(filenames) do
local filedata = data[filename]
fd:write(filedata.max, ":", filename, "\n")
for i = 1, filedata.max do
fd:write(tostring(filedata[i] or 0), " ")
end
fd:write("\n")
end
fd:close()
end
local outfile
local data = {}
local infiles={}
for _, v in ipairs({...}) do
local out = v:match("^--out=(.*)")
if out then
outfile = out
else
table.insert(infiles, v)
end
end
if #infiles == 0 then
io.stderr:write("No stats files to load.\n")
os.exit(1)
end
for _, f in ipairs(infiles) do
local err
data, err = luacov_stats_load(f, data)
if not data then
io.stderr:write("Failed to open stats file "..(err or "").."\n")
os.exit(1)
end
end
if outfile then
print(("Loaded %s stats file%s."):format(#infiles, #infiles == 1 and "" or "s"))
luacov_stats_save(outfile, data)
for _,f in ipairs(infiles) do
local ok, err = os.remove(f)
if not ok then
io.stderr:write("Failed to remove merged stats file " .. f..": "..err.."\n")
else
end
end
print(("Removed %d merged stats file%s."):format(#infiles, #infiles==1 and "" or "s"))
print(("Saved merged stats to file %s"):format(outfile))
else
luacov_stats_save(nil, data, io.stdout)
end